Interpreter Environment Change Diff

-18 Removals
+16 Additions
# Top level function that interprets an entire program. It creates the
# Top level function that interprets an entire program. It creates the
# initial environment that's used for storing variables.
# initial environment that's used for storing variables.
def interpret_program(model):
def interpret_program(model):
# Make the initial environment (a dict). The environment is
# Make the initial environment (a dict). The environment is
# where you will create and store variables.
# where you will create and store variables.
env = {'constants':{}, 'variables':{}}
env = {}
for structure in model.statements:
for structure in model.statements:
interpret(structure, env)
interpret(structure, env)
# Internal function to interpret a node in the environment. You need
# Internal function to interpret a node in the environment. You need
# to expand to cover all of the classes in the model.py file.
# to expand to cover all of the classes in the model.py file.
def interpret(node, env):
def interpret(node, env):
# Expand to check for different node types
# Expand to check for different node types
if isinstance(node, Integer):
if isinstance(node, Integer):
return int(node.value)
return int(node.value)
elif isinstance(node, Float):
elif isinstance(node, Float):
return node.value
return node.value
elif isinstance(node, BinaryOperator):
elif isinstance(node, BinaryOperator):
if node.op == '*':
if node.op == '*':
return interpret(node.left, env) * interpret(node.right, env)
return interpret(node.left, env) * interpret(node.right, env)
elif node.op == '/':
elif node.op == '/':
return interpret(node.left, env) / interpret(node.right, env)
return interpret(node.left, env) / interpret(node.right, env)
elif node.op == '+':
elif node.op == '+':
return interpret(node.left, env) + interpret(node.right, env)
return interpret(node.left, env) + interpret(node.right, env)
elif node.op == '-':
elif node.op == '-':
return interpret(node.left, env) - interpret(node.right, env)
return interpret(node.left, env) - interpret(node.right, env)
elif isinstance(node, Comparison):
elif isinstance(node, Comparison):
if node.op == '>':
if node.op == '>':
return interpret(node.left, env) > interpret(node.right, env)
return interpret(node.left, env) > interpret(node.right, env)
elif node.op == '<':
elif node.op == '<':
return interpret(node.left, env) < interpret(node.right, env)
return interpret(node.left, env) < interpret(node.right, env)
elif node.op == '==':
elif node.op == '==':
return interpret(node.left, env) == interpret(node.right, env)
return interpret(node.left, env) == interpret(node.right, env)
elif node.op == '>=':
elif node.op == '>=':
return interpret(node.left, env) >= interpret(node.right, env)
return interpret(node.left, env) >= interpret(node.right, env)
elif node.op == '<=':
elif node.op == '<=':
return interpret(node.left, env) <= interpret(node.right, env)
return interpret(node.left, env) <= interpret(node.right, env)
elif node.op == '!=':
elif node.op == '!=':
return interpret(node.left, env) != interpret(node.right, env)
return interpret(node.left, env) != interpret(node.right, env)
elif isinstance(node, Print):
elif isinstance(node, Print):
print(interpret(node.value, env))
print(interpret(node.value, env))
elif isinstance(node, VariableAssignment):
elif isinstance(node, VariableAssignment):
# if node.type.string_representation == the start of type checking
env[str(node.name)] = {"value": interpret(node.value, env), "mutable": True}
env['variables'][str(node.name)] = interpret(node.value, env)
return Unit()
return Unit()
elif isinstance(node, ConstantAssignment):
elif isinstance(node, ConstantAssignment):
env['constants'][str(node.name)] = interpret(node.value, env)
env[str(node.name)] = {"value": interpret(node.value, env), "mutable": False}
return Unit()
return Unit()
elif isinstance(node, VariableDeclaration):
elif isinstance(node, VariableDeclaration):
env['variables'][str(node.name)] = "WABBITASSIGNMENTINWAITING"
env[str(node.name)] = {"value": None, "mutable": True}
return Unit()
return Unit()
elif isinstance(node, VariableReAssignment):
elif isinstance(node, VariableReAssignment):
if str(node.name) in env['variables'].keys():
if str(node.name) in env.keys():
env['variables'][str(node.name)] = interpret(node.value, env)
if not env[str(node.name)]['mutable']:
elif str(node.name) in env['constants'].keys():
raise RuntimeError("Cannot reassign the value of a constant")
raise RuntimeError("Cannot reassign the value of a constant")
else:
env[str(node.name)] = interpret(node.value, env)
else:
else:
raise RuntimeError(f"{node.name} not found for reassignment")
raise RuntimeError(f"{node.name} not found for reassignment")
return Unit()
return Unit()
elif isinstance(node, Name):
elif isinstance(node, Name):
if node.name in env['variables'].keys():
if node.name in env.keys():
return env['variables'].get(node.name)
return env.get(node.name).get('value')
elif node.name in env['constants'].keys():
return env['constants'].get(str(node.name))
else:
else:
raise RuntimeError(f"'{node.name}' not defined")
raise RuntimeError(f"'{node.name}' not defined")
elif isinstance(node, Negative):
elif isinstance(node, Negative):
# gotta check if its type that can be negative
# gotta check if its type that can be negative
return interpret(node.value, env) * -1
return interpret(node.value, env) * -1
elif isinstance(node, Conditional):
elif isinstance(node, Conditional):
if interpret(node.if_condition, env):
if interpret(node.if_condition, env):
result = interpret(node.if_block, env)
result = interpret(node.if_block, env)
if isinstance(result, Break):
if isinstance(result, Break):
return Break()
return Break()
else:
else:
if node.else_block:
if node.else_block:
result = interpret(node.else_block, env)
result = interpret(node.else_block, env)
if isinstance(result, Break):
if isinstance(result, Break):
return Break()
return Break()
elif isinstance(node, WhileLoop):
elif isinstance(node, WhileLoop):
if interpret(node.while_condition, env):
if interpret(node.while_condition, env):
result = interpret(node.while_block, env)
result = interpret(node.while_block, env)
if not isinstance(result, Break):
if not isinstance(result, Break):
interpret(node, env)
interpret(node, env)
else:
else:
return Unit()
return Unit()
elif isinstance(node, Block):
elif isinstance(node, Block):
for statement in node.statements:
for statement in node.statements:
maybe_flow_interruptor = interpret(statement, env)
maybe_flow_interruptor = interpret(statement, env)
if isinstance(maybe_flow_interruptor, Continue):
if isinstance(maybe_flow_interruptor, Continue):
return Unit()
return Unit()
elif isinstance(maybe_flow_interruptor, Break):
elif isinstance(maybe_flow_interruptor, Break):
return Break()
return Break()
return Unit()
return Unit()
elif isinstance(node, CompoundExpression):
elif isinstance(node, CompoundExpression):
final_statement = None
final_statement = None
for statement in node.statements:
for statement in node.statements:
final_statement = interpret(statement, env)
final_statement = interpret(statement, env)
return final_statement
return final_statement
elif isinstance(node, Break):
elif isinstance(node, Break):
return Break()
return Break()
elif isinstance(node, Continue):
elif isinstance(node, Continue):
return Continue()
return Continue()
elif isinstance(node, WabbitType):
elif isinstance(node, WabbitType):
return Unit()
return Unit()
elif isinstance(node, WabbitBoolean):
elif isinstance(node, WabbitBoolean):
return node.value
return node.value
elif isinstance(node, Character):
elif isinstance(node, Character):
return str(node.value)
return str(node.value)
else:
else:
raise RuntimeError(f"Can't interpret {node}")
raise RuntimeError(f"Can't interpret {node}")
Editor
Clear
Export as PDF
Original Text
Changed Text