def handle_while(block, instance_vars, stack): validate_while_loop_syntax(block) tokens = clean_up_list_elems(flatten_list(re.split("while\s*\(", block))) tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\)\s*\{", x), tokens)))) tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\}", x), tokens)))) print("tokens: " + str(tokens)) assert len(tokens) == 2, "There should be 2 tokens, but there are " + str(len(tokens)) condition, statements = tokens[0], tokens[1] while evaluate_expression(condition): parse_eval(statements, instance_vars, stack) # We will NOT support different scoping for variables inside. return # Call parse_eval with instance_vars
def handle_for(block, instance_vars, stack): validate_for_loop_syntax(block) tokens = clean_up_list_elems(flatten_list(re.split("for\s*\(", block))) tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\)\s*\{", x), tokens)))) tokens = clean_up_list_elems(flatten_list(list(map(lambda x: re.split("\}", x), tokens)))) print("tokens: " + str(tokens)) tokens = tokens[0].split(";") + [tokens[1]] print("tokens: " + str(tokens)) initialize, condition, update, statements = tokens print("tokens: " + str(tokens)) assign_variable(initialize, instance_vars, stack) evaluated_condition = evaluate_expression(condition) if type(evaluated_condition) is not bool: raise InvalidForLoopException("Boolean condition is of wrong type") while evaluated_condition: parse_eval(statements, instance_vars, stack) assign_variable(update, instance_vars, stack) return