1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-26 22:28:26 +03:00
mal/impls/io/step2_eval.io
Nicolas Boulenguez 03b6cfd45c Stop evaluating map keys
Neither keywords nor strings are modified by evaluation, so evaluating
map keys is a no-op.  Document this in the guide.
2021-12-11 09:46:35 -06:00

52 lines
1.1 KiB
Io

MalTypes
MalReader
READ := method(str, MalReader read_str(str))
eval_ast := method(ast, env,
(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))),
"MalMap",
m := MalMap clone
ast foreach(k, v,
m atPut(k, EVAL(v, env))
)
m,
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)
args := el rest
f callWithArgList(args)
)
PRINT := method(exp, exp malPrint(true))
repl_env := Map with(
"+", block(a, b, a + b),
"-", block(a, b, a - b),
"*", block(a, b, a * b),
"/", block(a, b, a / b)
)
RE := method(str, EVAL(READ(str), repl_env))
REP := method(str, PRINT(RE(str)))
loop(
line := MalReadline readLine("user> ")
if(line isNil, break)
if(line isEmpty, continue)
e := try(REP(line) println)
e catch(Exception,
("Error: " .. (e error)) println
)
)