Skip to content

Commit

Permalink
rpython: complete the eval_ast merge, add DEBUG-EVAL
Browse files Browse the repository at this point in the history
Commit 0338927 had only updated stepA.
  • Loading branch information
asarhaddon committed Oct 15, 2024
1 parent 9bf894c commit 13abb6d
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 171 deletions.
6 changes: 6 additions & 0 deletions impls/rpython/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ def get(self, key):
env = self.find(key)
if not env: throw_str("'" + str(key.value) + "' not found")
return env.data[key.value]

def get_or_None:
assert isinstance(key, MalSym)
env = self.find(key)
if not env: return None
return env.data[key.value]
22 changes: 10 additions & 12 deletions impls/rpython/step2_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ def READ(str):

# eval
def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
# print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
if ast.value in env:
return env[ast.value]
else:
raise Exception(u"'" + ast.value + u"' not found")
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -33,14 +36,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
el = eval_ast(ast, env)
Expand Down
24 changes: 12 additions & 12 deletions impls/rpython/step3_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mal_types as types
from mal_types import (MalSym, MalInt, MalStr,
_symbol, _keywordu,
nil, false, throw_str,
MalList, _list, MalVector, MalHashMap, MalFunc)
import reader, printer
from env import Env
Expand All @@ -13,14 +14,18 @@ def READ(str):

# eval
def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
if env.get_or_None(MalSym(u"DEBUG-EVAL")) not in (None, nil, false):
print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
return env.get(ast)
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -31,14 +36,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
Expand Down
24 changes: 12 additions & 12 deletions impls/rpython/step4_if_fn_do.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mal_types as types
from mal_types import (MalSym, MalInt, MalStr,
nil, true, false, _symbol, _keywordu,
throw_str,
MalList, _list, MalVector, MalHashMap, MalFunc)
import reader, printer
from env import Env
Expand All @@ -14,14 +15,18 @@ def READ(str):

# eval
def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
if env.get_or_None(MalSym(u"DEBUG-EVAL")) not in (None, nil, false):
print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
return env.get(ast)
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -32,14 +37,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
Expand Down
26 changes: 13 additions & 13 deletions impls/rpython/step5_tco.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mal_types as types
from mal_types import (MalSym, MalInt, MalStr,
nil, true, false, _symbol, _keywordu,
throw_str,
MalList, _list, MalVector, MalHashMap, MalFunc)
import reader, printer
from env import Env
Expand All @@ -14,14 +15,19 @@ def READ(str):

# eval
def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
while True:
if env.get_or_None(MalSym(u"DEBUG-EVAL")) not in (None, nil, false):
print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
return env.get(ast)
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -32,15 +38,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
while True:
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
Expand Down
26 changes: 13 additions & 13 deletions impls/rpython/step6_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mal_types as types
from mal_types import (MalSym, MalInt, MalStr,
nil, true, false, _symbol, _keywordu,
throw_str,
MalList, _list, MalVector, MalHashMap, MalFunc)
import reader, printer
from env import Env
Expand All @@ -14,14 +15,19 @@ def READ(str):

# eval
def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
while True:
if env.get_or_None(MalSym(u"DEBUG-EVAL")) not in (None, nil, false):
print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
return env.get(ast)
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -32,15 +38,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
while True:
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
Expand Down
28 changes: 13 additions & 15 deletions impls/rpython/step7_quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mal_types as types
from mal_types import (MalSym, MalInt, MalStr,
nil, true, false, _symbol, _keywordu,
throw_str,
MalList, _list, MalVector, MalHashMap, MalFunc)
import reader, printer
from env import Env
Expand Down Expand Up @@ -41,14 +42,19 @@ def quasiquote(ast):
return ast

def eval_ast(ast, env):
assert isinstance(ast, MalList)
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)

def EVAL(ast, env):
while True:
if env.get_or_None(MalSym(u"DEBUG-EVAL")) not in (None, nil, false):
print(u"EVAL " + printer._pr_str(ast))
if types._symbol_Q(ast):
assert isinstance(ast, MalSym)
return env.get(ast)
elif types._list_Q(ast):
res = []
for a in ast.values:
res.append(EVAL(a, env))
return MalList(res)
elif types._vector_Q(ast):
res = []
for a in ast.values:
Expand All @@ -59,15 +65,9 @@ def eval_ast(ast, env):
for k in ast.dct.keys():
new_dct[k] = EVAL(ast.dct[k], env)
return MalHashMap(new_dct)
else:
elif not types._list_Q(ast):
return ast # primitive value, return unchanged

def EVAL(ast, env):
while True:
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)

else:
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
Expand All @@ -89,8 +89,6 @@ def EVAL(ast, env):
env = let_env # Continue loop (TCO)
elif u"quote" == a0sym:
return ast[1]
elif u"quasiquoteexpand" == a0sym:
return quasiquote(ast[1])
elif u"quasiquote" == a0sym:
ast = quasiquote(ast[1]) # Continue loop (TCO)
elif u"do" == a0sym:
Expand Down
Loading

0 comments on commit 13abb6d

Please sign in to comment.