php: Fix exception on literal empty list
[jackhill/mal.git] / io / step9_try.io
CommitLineData
a7353735
DM
1MalTypes
2MalReader
3
4READ := method(str, MalReader read_str(str))
5
6isPair := method(obj,
7 obj ?isSequential and(obj isEmpty not)
8)
9
10quasiquote := method(ast,
11 if(isPair(ast) not, return(MalList with(list(MalSymbol with("quote"), ast))))
12 a0 := ast at(0)
13 if(a0 == MalSymbol with("unquote"), return(ast at(1)))
14 if(isPair(a0) and (a0 at(0) == MalSymbol with("splice-unquote")),
15 return(MalList with(list(MalSymbol with("concat"), a0 at(1), quasiquote(ast rest)))),
16 return(MalList with(list(MalSymbol with("cons"), quasiquote(a0), quasiquote(ast rest)))))
17)
18
19isMacroCall := method(ast, env,
9f78a1b9
DM
20 if(ast type != "MalList", return false)
21 a0 := ast first
22 if(a0 type != "MalSymbol", return false)
23 if(env find(a0) isNil, return false)
24 f := env get(a0)
25 (f type == "MalFunc") and (f isMacro)
a7353735
DM
26)
27
28macroexpand := method(ast, env,
29 while(isMacroCall(ast, env),
30 macro := env get(ast at(0))
31 ast = macro blk call(ast rest)
32 )
33 ast
34)
35
36eval_ast := method(ast, env,
37 (ast type) switch(
38 "MalSymbol", env get(ast),
39 "MalList", MalList with(ast map(a, EVAL(a, env))),
40 "MalVector", MalVector with(ast map(a, EVAL(a, env))),
41 "MalMap",
42 m := MalMap clone
43 ast foreach(k, v,
44 keyObj := MalMap keyToObj(k)
45 m atPut(MalMap objToKey(EVAL(keyObj, env)), EVAL(v, env))
46 )
47 m,
48 ast
49 )
50)
51
52EVAL := method(ast, env,
53 loop(
54 if(ast type != "MalList", return(eval_ast(ast, env)))
55
56 ast = macroexpand(ast, env)
57 if(ast type != "MalList", return(eval_ast(ast, env)))
58
59 if(ast at(0) type == "MalSymbol",
60 ast at(0) val switch(
61 "def!",
62 return(env set(ast at(1), EVAL(ast at(2), env))),
63 "do",
64 eval_ast(ast slice(1,-1), env)
65 ast = ast last
66 continue, // TCO
67 "if",
68 ast = if(EVAL(ast at(1), env), ast at(2), ast at(3))
69 continue, // TCO
70 "fn*",
71 return(MalFunc with(ast at(2), ast at(1), env, block(a, EVAL(ast at(2), Env with(env, ast at(1), a))))),
72 "let*",
73 letEnv := Env with(env)
74 varName := nil
75 ast at(1) foreach(i, e,
76 if(i % 2 == 0,
77 varName := e,
78 letEnv set(varName, EVAL(e, letEnv))
79 )
80 )
81 ast = ast at(2)
82 env = letEnv
83 continue, // TCO
84 "quote",
85 return(ast at(1)),
86 "quasiquote",
87 ast = quasiquote(ast at(1))
88 continue, // TCO
89 "defmacro!",
90 return(env set(ast at(1), EVAL(ast at(2), env) setIsMacro(true))),
91 "macroexpand",
92 return(macroexpand(ast at(1), env)),
93 "try*",
94 e := try(result := EVAL(ast at(1), env))
95 e catch(Exception,
9f78a1b9 96 exc := if(e type == "MalException", e val, e error)
a7353735
DM
97 catchAst := ast at(2)
98 catchEnv := Env with(env)
99 catchEnv set(catchAst at(1), exc)
100 result := EVAL(catchAst at(2), catchEnv)
101 )
102 return(result)
103 )
104 )
105
106 // Apply
107 el := eval_ast(ast, env)
108 f := el at(0)
109 args := el rest
110 f type switch(
111 "Block",
112 return(f call(args)),
113 "MalFunc",
114 ast = f ast
115 env = Env with(f env, f params, args)
116 continue, // TCO
117 Exception raise("Unknown function type")
118 )
119 )
120)
121
122PRINT := method(exp, exp malPrint(true))
123
124repl_env := Env with(nil)
125
126RE := method(str, EVAL(READ(str), repl_env))
127
128REP := method(str, PRINT(RE(str)))
129
130MalCore NS foreach(k, v, repl_env set(MalSymbol with(k), v))
131repl_env set(MalSymbol with("eval"), block(a, EVAL(a at(0), repl_env)))
132repl_env set(MalSymbol with("*ARGV*"), MalList with(System args slice(2)))
133
134// core.mal: defined using the language itself
135RE("(def! not (fn* (a) (if a false true)))")
136RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
137RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
138RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
139
140if(System args size > 1,
141 REP("(load-file \"" .. (System args at(1)) .. "\")")
142 System exit(0)
143)
144
145loop(
146 line := MalReadline readLine("user> ")
147 if(line isNil, break)
148 if(line isEmpty, continue)
149 e := try(REP(line) println)
150 e catch(Exception,
151 ("Error: " .. (e error)) println
152 )
153)