-
Notifications
You must be signed in to change notification settings - Fork 11
/
evaluator.py
34 lines (30 loc) · 1.31 KB
/
evaluator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Kimi language interpreter in Python 3
# Anjana Vakil
# http://www.github.com/vakila/kimi
import special_forms as sf
from environments import Environment
from errors import *
SPECIALS = sf.special_forms()
def evaluate(expression, environment):
'''Take an expression and environment as dictionaries.
Evaluate the expression in the context of the environment, and return the result.
>>> evaluate(parse(tokenize("(+ 1 2)")), standard_env())
3
'''
# print("EVALUATING:", expression)
expr_type = expression['type']
# print("EXPR_TYPE:", expr_type)
if expr_type == 'literal':
return expression['value']
elif expr_type == 'symbol':
symbol = expression['value']
return environment.get(symbol)
elif expr_type == 'apply':
operator = expression['operator']
if operator['type'] == 'symbol' and operator['value'] in SPECIALS:
return SPECIALS[operator['value']](expression['arguments'], environment)
fn = evaluate(operator, environment)
assert_or_throw(callable(fn), "type", 'Trying to call a non-function. Did you use parentheses correctly?')
return fn(*[evaluate(arg, environment) for arg in expression['arguments']])
else:
complain_and_die("PARSING ERROR! Unexpected expression type: " + str(expression) + ".")