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.
master
BodgeMaster 2022-11-06 06:21:01 +01:00
parent fbdf2e42f2
commit b8b23249a0
1 changed files with 25 additions and 3 deletions

View File

@ -75,6 +75,19 @@ def draw_field():
print("") 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): def condition_facing_north(inverted):
if inverted: if inverted:
return not cursor_current == cursor_north 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 [[], [], parse_position, "Syntax error: Unknown control structure: "+control_structure, code]
return parsed_code return parsed_code
# A wrapper for run_code that deals with potential errors produced by the parser.
# returns success or error message # returns success or error message
# Parsed code is the result of running the parser. def evaluate_parser_result(parsed_code):
def run_code(parsed_code): if not parsed_code[2]==-1:
return "" # 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__": if __name__ == "__main__":
pass pass