4 READ
:= method(str
, MalReader
read_str(str
))
6 eval_ast
:= method(ast
, env
,
8 "MalSymbol", env
get(ast
),
9 "MalList", MalList
with(ast
map(a
, EVAL(a
, env
))),
10 "MalVector", MalVector
with(ast
map(a
, EVAL(a
, env
))),
14 keyObj
:= MalMap
keyToObj(k
)
15 m
atPut(MalMap
objToKey(EVAL(keyObj
, env
)), EVAL(v
, env
))
22 EVAL
:= method(ast
, env
,
24 if(ast
type != "MalList", return(eval_ast(ast
, env
)))
25 if(ast isEmpty
, return ast
)
26 if(ast
at(0) type == "MalSymbol",
29 return(env
set(ast
at(1), EVAL(ast
at(2), env
))),
31 eval_ast(ast
slice(1,-1), env
)
35 ast
= if(EVAL(ast
at(1), env
), ast
at(2), ast
at(3))
38 return(MalFunc
with(ast
at(2), ast
at(1), env
, block(a
, EVAL(ast
at(2), Env
with(env
, ast
at(1), a
))))),
40 letEnv
:= Env
with(env
)
42 ast
at(1) foreach(i
, e
,
45 letEnv
set(varName
, EVAL(e
, letEnv
))
55 el
:= eval_ast(ast
, env
)
63 env
= Env
with(f env
, f params
, args
)
65 Exception raise("Unknown function type")
70 PRINT
:= method(exp
, exp
malPrint(true
))
72 RE
:= method(str
, EVAL(READ(str
), repl_env
))
74 REP
:= method(str
, PRINT(RE(str
)))
76 repl_env
:= Env
with(nil
)
77 MalCore NS
foreach(k
, v
, repl_env
set(MalSymbol
with(k
), v
))
79 // core
.mal
: defined using the language itself
80 RE("(def! not (fn* (a) (if a false true)))")
83 line
:= MalReadline
readLine("user> ")
85 if(line isEmpty
, continue)
86 e
:= try(REP(line
) println
)
88 if(e
type == "MalException",
89 ("Error: " .. ((e val
) malPrint(true
))) println
,
90 ("Error: " .. (e error
)) println