Skip to content

Commit

Permalink
io: merge eval_ast into eval, add DEBUG-EVAL
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Oct 16, 2024
1 parent b463373 commit 57521d4
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 159 deletions.
19 changes: 11 additions & 8 deletions impls/io/step2_eval.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ MalReader
READ := method(str, MalReader read_str(str))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

EVAL := method(ast, env,

// "EVAL: " .. PRINT(ast) println

(ast type) switch(
"MalSymbol", env at(ast val) ifNil(Exception raise("'" .. (ast val) "' not found")),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env at(ast val) ifNil(Exception raise("'" .. (ast val) "' not found"))),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
el := eval_ast(ast, env)
f := el at(0)
Expand Down
24 changes: 16 additions & 8 deletions impls/io/step3_env.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ MalReader
READ := method(str, MalReader read_str(str))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
if(ast at(0) type == "MalSymbol",
ast at(0) val switch(
Expand Down
24 changes: 16 additions & 8 deletions impls/io/step4_if_fn_do.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ MalReader
READ := method(str, MalReader read_str(str))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
if(ast at(0) type == "MalSymbol",
ast at(0) val switch(
Expand Down
26 changes: 17 additions & 9 deletions impls/io/step5_tco.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ MalReader
READ := method(str, MalReader read_str(str))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,
loop(

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
loop(
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
if(ast at(0) type == "MalSymbol",
ast at(0) val switch(
Expand Down
26 changes: 17 additions & 9 deletions impls/io/step6_file.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ MalReader
READ := method(str, MalReader read_str(str))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,
loop(

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
loop(
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
if(ast at(0) type == "MalSymbol",
ast at(0) val switch(
Expand Down
28 changes: 17 additions & 11 deletions impls/io/step7_quote.io
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ quasiquote := method(ast,
ast))

eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,
loop(

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
loop(
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)
if(ast at(0) type == "MalSymbol",
ast at(0) val switch(
Expand Down Expand Up @@ -66,8 +74,6 @@ EVAL := method(ast, env,
continue, // TCO
"quote",
return(ast at(1)),
"quasiquoteexpand",
return quasiquote(ast at(1)),
"quasiquote",
ast = quasiquote(ast at(1))
continue // TCO
Expand Down
59 changes: 23 additions & 36 deletions impls/io/step8_macros.io
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,32 @@ quasiquote := method(ast,
qq_foldr(ast)),
ast))

isMacroCall := method(ast, env,
if(ast type != "MalList", return false)
a0 := ast first
if(a0 type != "MalSymbol", return false)
if(env find(a0) isNil, return false)
f := env get(a0)
(f type == "MalFunc") and (f isMacro)
)
eval_ast := method(ast, env,
MalList with(ast map(a, EVAL(a, env))))

macroexpand := method(ast, env,
while(isMacroCall(ast, env),
macro := env get(ast at(0))
ast = macro blk call(ast rest)
)
ast
)
debugEvalSymbol := MalSymbol with("DEBUG-EVAL")

EVAL := method(ast, env,
loop(

debugEvalEnv := env find(debugEvalSymbol)
if(debugEvalEnv isNil,,
if(debugEvalEnv.get(debugEvalSymbol),
("EVAL: " .. PRINT(ast)) println))

eval_ast := method(ast, env,
(ast type) switch(
"MalSymbol", env get(ast),
"MalList", MalList with(ast map(a, EVAL(a, env))),
"MalVector", MalVector with(ast map(a, EVAL(a, env))),
"MalSymbol", return(env get(ast)),
"MalList",,
"MalVector", return(MalVector with(ast map(a, EVAL(a, env)))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
ast
return(m),
return(ast)
)
)

EVAL := method(ast, env,
loop(
if(ast type != "MalList", return(eval_ast(ast, env)))

ast = macroexpand(ast, env)
if(ast type != "MalList", return(eval_ast(ast, env)))
if(ast isEmpty, return ast)

if(ast at(0) type == "MalSymbol",
Expand Down Expand Up @@ -87,29 +75,28 @@ EVAL := method(ast, env,
continue, // TCO
"quote",
return(ast at(1)),
"quasiquoteexpand",
return quasiquote(ast at(1)),
"quasiquote",
ast = quasiquote(ast at(1))
continue, // TCO
"defmacro!",
return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true))),
"macroexpand",
return(macroexpand(ast at(1), env))
return(env set(ast at(1), EVAL(ast at(2), env) clone setIsMacro(true)))
)
)

// Apply
el := eval_ast(ast, env)
f := el at(0)
args := el rest
f := EVAL(ast at(0), env)
raw_args := ast rest
f type switch(
"Block",
args := eval_ast(raw_args, env)
return(f call(args)),
"MalFunc",
if(f isMacro, ast = f blk call(raw_args),
args := eval_ast(raw_args, env)
ast = f ast
env = Env with(f env, f params, args)
continue, // TCO
)
Exception raise("Unknown function type")
)
)
Expand Down
Loading

0 comments on commit 57521d4

Please sign in to comment.