prepare for later
[jackhill/mal.git] / yorick / step9_try.i
CommitLineData
21986733
DM
1set_path, get_env("YORICK_MAL_PATH") + ":" + get_path()
2require, "reader.i"
3require, "printer.i"
4require, "core.i"
5require, "env.i"
6
7func READ(str)
8{
9 return read_str(str)
10}
11
12func is_pair(ast)
13{
14 type = structof(ast)
15 return ((type == MalList) || (type == MalVector)) && count(ast) > 0
16}
17
18func quasiquote(ast)
19{
20 if (!is_pair(ast)) return MalList(val=&[&MalSymbol(val="quote"), &ast])
21 lst = *ast.val
22 ast1 = *lst(1)
23 if (structof(ast1) == MalSymbol && ast1.val == "unquote") return *lst(2)
24 if (is_pair(ast1)) {
25 ast11 = *((*ast1.val)(1))
26 if (structof(ast11) == MalSymbol && ast11.val == "splice-unquote") {
27 return MalList(val=&[&MalSymbol(val="concat"), (*ast1.val)(2), &quasiquote(rest(ast))])
28 }
29 }
30 return MalList(val=&[&MalSymbol(val="cons"), &quasiquote(ast1), &quasiquote(rest(ast))])
31}
32
33func is_macro_call(ast, env)
34{
35 if (structof(ast) != MalList) return 0
36 if (count(ast) == 0) return 0
37 a1 = *((*ast.val)(1))
38 if (structof(a1) != MalSymbol) return 0
39 var_name = a1.val
40 found_env = env_find(env, var_name)
41 if (is_void(found_env)) return 0
42 obj = env_get(env, var_name)
43 return is_macro(obj)
44}
45
46func macroexpand(ast, env)
47{
48 while (is_macro_call(ast, env)) {
49 macro_name = (*ast.val)(1)->val
50 macro_obj = env_get(env, macro_name)
51 macro_args = *rest(ast).val
52 fn_env = env_new(macro_obj.env, binds=*macro_obj.binds, exprs=macro_args)
53 ast = EVAL(*macro_obj.ast, fn_env)
54 }
55 return ast
56}
57
58func eval_ast(ast, env)
59{
60 type = structof(ast)
61 if (type == MalSymbol) {
62 return env_get(env, ast.val)
63 } else if (type == MalList) {
64 seq = *(ast.val)
65 if (numberof(seq) == 0) return ast
66 res = array(pointer, numberof(seq))
67 for (i = 1; i <= numberof(seq); ++i) {
68 e = EVAL(*seq(i), env)
69 if (structof(e) == MalError) return e
70 res(i) = &e
71 }
72 return MalList(val=&res)
73 } else if (type == MalVector) {
74 seq = *(ast.val)
75 if (numberof(seq) == 0) return ast
76 res = array(pointer, numberof(seq))
77 for (i = 1; i <= numberof(seq); ++i) {
78 e = EVAL(*seq(i), env)
79 if (structof(e) == MalError) return e
80 res(i) = &e
81 }
82 return MalVector(val=&res)
83 } else if (type == MalHashmap) {
84 h = *(ast.val)
85 if (numberof(*h.keys) == 0) return ast
86 res = hash_new()
87 for (i = 1; i <= numberof(*h.keys); ++i) {
88 new_key = EVAL(hashmap_key_to_obj((*h.keys)(i)), env)
89 if (structof(new_key) == MalError) return new_key
90 new_val = EVAL(*((*h.vals)(i)), env)
91 if (structof(new_val) == MalError) return new_val
92 hash_set, res, hashmap_obj_to_key(new_key), new_val
93 }
94 return MalHashmap(val=&res)
95 } else return ast
96}
97
98func EVAL(ast, env)
99{
100 while (1) {
101 if (structof(ast) == MalError) return ast
102 if (structof(ast) != MalList) return eval_ast(ast, env)
103 ast = macroexpand(ast, env)
104 if (structof(ast) != MalList) return eval_ast(ast, env)
105 lst = *ast.val
106 if (numberof(lst) == 0) return ast
107 a1 = lst(1)->val
108 if (a1 == "def!") {
109 new_value = EVAL(*lst(3), env)
110 if (structof(new_value) == MalError) return new_value
111 return env_set(env, lst(2)->val, new_value)
112 } else if (a1 == "let*") {
113 let_env = env_new(&env)
114 args_lst = *(lst(2)->val)
115 for (i = 1; i <= numberof(args_lst); i += 2) {
116 var_name = args_lst(i)->val
117 var_value = EVAL(*args_lst(i + 1), let_env)
118 if (structof(var_value) == MalError) return var_value
119 env_set, let_env, var_name, var_value
120 }
121 ast = *lst(3)
122 env = let_env
123 // TCO
124 } else if (a1 == "quote") {
125 return *lst(2)
126 } else if (a1 == "quasiquote") {
127 ast = quasiquote(*lst(2)) // TCO
128 } else if (a1 == "defmacro!") {
129 new_value = EVAL(*lst(3), env)
130 if (structof(new_value) == MalError) return new_value
131 new_value.macro = 1
132 return env_set(env, lst(2)->val, new_value)
133 } else if (a1 == "macroexpand") {
134 return macroexpand(*lst(2), env)
135 } else if (a1 == "try*") {
136 ret = EVAL(*lst(2), env)
dd7a4f55 137 if (structof(ret) == MalError && numberof(lst) > 2) {
21986733
DM
138 exc = *ret.obj
139 if (is_void(exc)) {
140 exc = MalString(val=ret.message)
141 }
142 catch_lst = *(lst(3)->val)
143 catch_env = env_new(&env)
144 env_set, catch_env, catch_lst(2)->val, exc
145 return EVAL(*catch_lst(3), catch_env)
146 } else {
147 return ret
148 }
149 } else if (a1 == "do") {
150 for (i = 2; i < numberof(lst); ++i) {
151 ret = EVAL(*lst(i), env)
152 if (structof(ret) == MalError) return ret
153 }
154 ast = *lst(numberof(lst))
155 // TCO
156 } else if (a1 == "if") {
157 cond_val = EVAL(*lst(2), env)
158 if (structof(cond_val) == MalError) return cond_val
159 if ((structof(cond_val) == MalNil) || (structof(cond_val) == MalFalse)) {
160 if (numberof(lst) > 3) {
161 ast = *lst(4)
162 } else {
163 return MAL_NIL
164 }
165 } else {
166 ast = *lst(3)
167 }
168 // TCO
169 } else if (a1 == "fn*") {
170 return MalFunction(env=&env, binds=lst(2)->val, ast=lst(3), macro=0)
171 } else {
172 el = eval_ast(ast, env)
173 if (structof(el) == MalError) return el
174 seq = *el.val
175 if (structof(*seq(1)) == MalNativeFunction) {
176 args = (numberof(seq) > 1) ? seq(2:) : []
177 return call_core_fn(seq(1)->val, args)
178 } else if (structof(*seq(1)) == MalFunction) {
179 fn = *seq(1)
180 exprs = numberof(seq) > 1 ? seq(2:) : []
181 fn_env = env_new(fn.env, binds=*fn.binds, exprs=exprs)
182 ast = *fn.ast
183 env = fn_env
184 // TCO
185 } else {
186 return MalError(message="Unknown function type")
187 }
188 }
189 }
190}
191
192func PRINT(exp)
193{
194 if (structof(exp) == MalError) return exp
195 return pr_str(exp, 1)
196}
197
198func RE(str, env)
199{
200 return EVAL(READ(str), env)
201}
202
203func REP(str, env)
204{
205 return PRINT(EVAL(READ(str), env))
206}
207
208func get_command_line(void)
209// Force quiet mode (-q) to prevent Yorick from printing its banner
210{
211 argv = get_argv()
212 return numberof(argv) > 1 ? grow([argv(1), "-q"], argv(2:)) : [argv(1), "-q"]
213}
214
215func prepare_argv_list(args)
216{
217 if (numberof(args) <= 1) return MalList(val=&[])
218 str_lst = array(pointer, numberof(args) - 1)
219 for (i = 2; i <= numberof(args); ++i) {
220 str_lst(i - 1) = &MalString(val=args(i))
221 }
222 return MalList(val=&str_lst)
223}
224
225repl_env = nil
226
227func main(void)
228{
229 extern repl_env
230 repl_env = env_new(pointer(0))
231
232 // core.i: defined using Yorick
233 core_symbols = h_keys(core_ns)
234 for (i = 1; i <= numberof(core_symbols); ++i) {
235 env_set, repl_env, core_symbols(i), MalNativeFunction(val=core_symbols(i))
236 }
237 command_line_args = process_argv()
238 env_set, repl_env, "*ARGV*", prepare_argv_list(command_line_args)
239
240 // core.mal: defined using the language itself
241 RE, "(def! not (fn* (a) (if a false true)))", repl_env
e6d41de4 242 RE, "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\\nnil)\")))))", repl_env
21986733 243 RE, "(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)))))))", repl_env
21986733
DM
244
245 if (numberof(command_line_args) > 0) {
246 RE, "(load-file \"" + command_line_args(1) + "\")", repl_env
247 return 0
248 }
249
250 stdin_file = open("/dev/stdin", "r")
251 while (1) {
252 write, format="%s", "user> "
253 line = rdline(stdin_file, prompt="")
254 if (!line) break
255 if (strlen(line) > 0) {
256 result = REP(line, repl_env)
dd7a4f55
JM
257 if (structof(result) == MalError) {
258 exc = *result.obj
259 if (is_void(exc)) {
260 write, format="Error: %s\n", result.message
261 } else {
262 write, format="Error: %s\n", pr_str(exc, 1)
263 }
264 }
21986733
DM
265 else write, format="%s\n", result
266 }
267 }
268 write, ""
269}
270
271main;