aboutsummaryrefslogtreecommitdiff
path: root/runner.py
blob: 69112c594d5c6d100b0c6211eee1bdf75611a355 (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):
        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[list(this_func.keys())[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")