Created
April 20, 2021 05:09
-
-
Save dhilst/33715316e622ef5dcc52f0f61b547d88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class sym: | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
return f":{self.name}" | |
def builtin_func(name): | |
def inner(f): | |
environment[name] = f | |
return inner | |
def evl(lst, environment): | |
print("evl", lst) | |
if type(lst) in (int, str, float, list) or callable(lst): | |
return lst | |
elif type(lst) is sym: | |
return environment[lst.name] | |
elif type(lst) is tuple: | |
if lst == (): | |
return () | |
car, *cdr = tuple(evl(e, environment) for e in lst) | |
if callable(car): | |
print("calling", car, cdr) | |
return car(*cdr) | |
else: | |
return car, *cdr | |
environment: dict[str, any] = {} | |
@builtin_func("if") | |
def if_(c, a, b): | |
if c: | |
return evl(tuple(a), environment) | |
else: | |
return evl(tuple(b), environment) | |
@builtin_func(">") | |
def gt(a, b): | |
return evl(a, environment) > evl(b, environment) | |
evl( | |
(sym("if"), (sym(">"), 1, 0), [print, "Hello world"], [print, "never called"]), | |
environment, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment