Skip to content

Commit

Permalink
io: merge macroexpand into eval
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon authored and kanaka committed Oct 29, 2024
1 parent 78b0d7a commit dfc67ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 69 deletions.
30 changes: 7 additions & 23 deletions impls/io/step8_macros.io
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ 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)
)

macroexpand := method(ast, env,
while(isMacroCall(ast, env),
macro := env get(ast at(0))
ast = macro blk call(ast rest)
)
ast
)

eval_ast := method(ast, env,
(ast type) switch(
"MalSymbol", env get(ast),
Expand All @@ -62,9 +45,6 @@ EVAL := method(ast, env,
("EVAL: " .. PRINT(ast)) println)

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 @@ -103,13 +83,17 @@ EVAL := method(ast, env,
)

// 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)
continue) // TCO
args := eval_ast(raw_args, env)
ast = f ast
env = Env with(f env, f params, args)
continue, // TCO
Expand Down
30 changes: 7 additions & 23 deletions impls/io/step9_try.io
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ 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)
)

macroexpand := method(ast, env,
while(isMacroCall(ast, env),
macro := env get(ast at(0))
ast = macro blk call(ast rest)
)
ast
)

eval_ast := method(ast, env,
(ast type) switch(
"MalSymbol", env get(ast),
Expand All @@ -62,9 +45,6 @@ EVAL := method(ast, env,
("EVAL: " .. PRINT(ast)) println)

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 @@ -114,13 +94,17 @@ EVAL := method(ast, env,
)

// 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)
continue) // TCO
args := eval_ast(raw_args, env)
ast = f ast
env = Env with(f env, f params, args)
continue, // TCO
Expand Down
30 changes: 7 additions & 23 deletions impls/io/stepA_mal.io
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,6 @@ 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)
)

macroexpand := method(ast, env,
while(isMacroCall(ast, env),
macro := env get(ast at(0))
ast = macro blk call(ast rest)
)
ast
)

eval_ast := method(ast, env,
(ast type) switch(
"MalSymbol", env get(ast),
Expand All @@ -62,9 +45,6 @@ EVAL := method(ast, env,
("EVAL: " .. PRINT(ast)) println)

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 @@ -114,13 +94,17 @@ EVAL := method(ast, env,
)

// 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)
continue) // TCO
args := eval_ast(raw_args, env)
ast = f ast
env = Env with(f env, f params, args)
continue, // TCO
Expand Down

0 comments on commit dfc67ee

Please sign in to comment.