Compare commits

..

No commits in common. "31981b541ef8b91e22efbb95422173e7f7f8306e" and "c0c99890d390769c09ac7908bcc58d2165448a67" have entirely different histories.

1 changed files with 11 additions and 45 deletions

View File

@ -53,7 +53,8 @@ cursor_position = [0, 0]
command_delay = 0.3
empty_field = [
def clear_field():
field = [
" _________________ ",
"| |",
"| |",
@ -303,13 +304,9 @@ def parse_condition(condition_code, allowed_conditions):
else:
debug_message(" Unknown condition: "+condition_code)
return [condition, inverted, "Unknown condition: "+condition_code]
if not condition_code in allowed_conditions:
return [condition, inverted, "Condition is not allowed here: "+condition_code]
return [condition, inverted, ""]
# returns [[functions], [arguments], -1, "", "formatted code"] or [[], [], error_position, "error message", "formatted code"]
# returns [[functions], [arguments], -1, ""] or [[], [], error_position, "error message"]
def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True):
if unformatted_code:
code = format_code(code)
@ -331,22 +328,15 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True
debug_message("Next command is: "+next_command)
parsed_code[1].append(())
#TODO: add function to to the function list
if next_command == "step":
parsed_code[0].append(command_step)
if not next_command in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+next_command, code]
elif next_command == "left":
parsed_code[0].append(command_left)
if not next_command in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+next_command, code]
elif next_command == "right":
parsed_code[0].append(command_right)
if not next_command in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+next_command, code]
elif next_command == "take":
parsed_code[0].append(command_take)
if not next_command in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+next_command, code]
elif next_command == "repeat":
return [[], [], next_space, "Syntax error: Number of repetitions missing", code]
elif next_command == "while":
@ -364,10 +354,6 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True
control_structure = code[parse_position:next_condition]
if control_structure == "repeat":
debug_message(" Type: repeat loop")
# This is checked here because otherwise it would throw a bogus error message for unknown control structures.
if not control_structure in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+control_structure, code]
parse_position = next_condition
repetitions_length = get_length_of_condition(code[parse_position:])
if repetitions_length == -2:
@ -401,10 +387,6 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True
continue
elif control_structure == "if":
debug_message(" Type: if statement")
# This is checked here because otherwise it would throw a bogus error message for unknown control structures.
if not control_structure in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+control_structure, code]
parse_position = next_condition
condition_length = get_length_of_condition(code[parse_position:])
if condition_length == -2:
@ -450,10 +432,6 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True
continue
elif control_structure == "while":
debug_message(" Type: while loop")
# This is checked here because otherwise it would throw a bogus error message for unknown control structures.
if not control_structure in allowed_commands:
return [[], [], parse_position, "Command is not allowed here: "+control_structure, code]
parse_position = next_condition
condition_length = get_length_of_condition(code[parse_position:])
if condition_length == -2:
@ -499,28 +477,16 @@ def evaluate_parser_result(parsed_code):
# runtime error
# TODO: pass back information about where the error occurred
return outcome
if condition_goal_reached(False):
return "Success!"
else:
return "Goal not reached!"
#TODO: check if goal reached
return "Success!"
def setup_and_run_task(start_field, start_position, start_heading, text, allowed_commands, allowed_conditions):
global cursor_position
cursor_position = start_position
global cursor_current
cursor_current = start_heading
global field
field = start_field
draw_field()
print("\n"+text)
print("\nAllowed commands: "+" ,".join(allowed_commands))
print("\nAvailable conditions: "+" ,".join(allowed_conditions))
print(evaluate_parser_result(parse_code(input("\nCode: "), allowed_commands, allowed_conditions)))
#TODO: return information about success or failure
def debug_setup():
setup_and_run_task(empty_field, [1,1], cursor_east, "Debug mode", ["step", "left", "right", "take", "repeat", "while", "if"], ["facing north", "facing south", "facing east", "facing west", "in front of wall", "goal reached", "on apple"])
global cursor_position
cursor_position = [1,1]
global cursor_current
cursor_current = cursor_south
draw_field()
if __name__ == "__main__":
pass