blob: 6f2f8ceea3f3d85adb0317c7893b6c5f5bc38a6c (
plain)
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
|
from tokens import *
from std import STD, evaluate_expression
def peek(inp):
if len(inp) == 0:
return None
return inp[0]
def evaluate(items, pop):
nxt = peek(items)
if nxt is None:
return pop
elif isinstance(nxt, NebLiteral) or isinstance(nxt, NebList):
return evaluate(items[1:], nxt)
elif isinstance(nxt, NebSymbol):
if not nxt.name in STD:
raise Exception(f"no such symbol: '{nxt.name}'")
this_func = STD[nxt.name]
return evaluate(items[1:], this_func[0].impl) # TODO show all
elif isinstance(nxt, NebExpression):
return evaluate(items[1:], evaluate_expression(nxt))
else:
raise Exception("expected a literal or an expression")
|