From b8b23249a08c0849ba3db2e0866e36f1446d8129 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 6 Nov 2022 06:21:01 +0100 Subject: [PATCH] Add runner portion of the interpreter The interpreter is now complete. Apart from bugs that may be hiding in the code, it should work now. --- lambdaV.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lambdaV.py b/lambdaV.py index ce55c1a..e732f47 100644 --- a/lambdaV.py +++ b/lambdaV.py @@ -75,6 +75,19 @@ def draw_field(): print("") +# returns "" or error message +# Parsed code is the result of running the parser, +# though at this point the input is assumed to be valid +# and no additional checks are performed on what the parser produced. +def run_code(parsed_code): + for i in range(len(parsed_code[0])): + result = parsed_code[0][i](*parsed_code[1][i]) + if not result=="": + return result + draw_field() + return "" + + def condition_facing_north(inverted): if inverted: return not cursor_current == cursor_north @@ -449,10 +462,19 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True return [[], [], parse_position, "Syntax error: Unknown control structure: "+control_structure, code] return parsed_code +# A wrapper for run_code that deals with potential errors produced by the parser. # returns success or error message -# Parsed code is the result of running the parser. -def run_code(parsed_code): - return "" +def evaluate_parser_result(parsed_code): + if not parsed_code[2]==-1: + # parser error + return parsed_code[3] + "\n" + (" "*parsed_code[2]) + "↓\n" + parsed_code[4] + outcome = run_code(parsed_code) + if not outcome=="": + # runtime error + # TODO: pass back information about where the error occurred + return outcome + #TODO: check if goal reached + return "Success!" if __name__ == "__main__": pass