Merge pull request #345 from asarhaddon/ada.2
[jackhill/mal.git] / yorick / step8_macros.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 == "do") {
136 for (i = 2; i < numberof(lst); ++i) {
137 ret = EVAL(*lst(i), env)
138 if (structof(ret) == MalError) return ret
139 }
140 ast = *lst(numberof(lst))
141 // TCO
142 } else if (a1 == "if") {
143 cond_val = EVAL(*lst(2), env)
144 if (structof(cond_val) == MalError) return cond_val
145 if ((structof(cond_val) == MalNil) || (structof(cond_val) == MalFalse)) {
146 if (numberof(lst) > 3) {
147 ast = *lst(4)
148 } else {
149 return MAL_NIL
150 }
151 } else {
152 ast = *lst(3)
153 }
154 // TCO
155 } else if (a1 == "fn*") {
156 return MalFunction(env=&env, binds=lst(2)->val, ast=lst(3), macro=0)
157 } else {
158 el = eval_ast(ast, env)
159 if (structof(el) == MalError) return el
160 seq = *el.val
161 if (structof(*seq(1)) == MalNativeFunction) {
162 args = (numberof(seq) > 1) ? seq(2:) : []
163 return call_core_fn(seq(1)->val, args)
164 } else if (structof(*seq(1)) == MalFunction) {
165 fn = *seq(1)
166 exprs = numberof(seq) > 1 ? seq(2:) : []
167 fn_env = env_new(fn.env, binds=*fn.binds, exprs=exprs)
168 ast = *fn.ast
169 env = fn_env
170 // TCO
171 } else {
172 return MalError(message="Unknown function type")
173 }
174 }
175 }
176}
177
178func PRINT(exp)
179{
180 if (structof(exp) == MalError) return exp
181 return pr_str(exp, 1)
182}
183
184func RE(str, env)
185{
186 return EVAL(READ(str), env)
187}
188
189func REP(str, env)
190{
191 return PRINT(EVAL(READ(str), env))
192}
193
194func get_command_line(void)
195// Force quiet mode (-q) to prevent Yorick from printing its banner
196{
197 argv = get_argv()
198 return numberof(argv) > 1 ? grow([argv(1), "-q"], argv(2:)) : [argv(1), "-q"]
199}
200
201func prepare_argv_list(args)
202{
203 if (numberof(args) <= 1) return MalList(val=&[])
204 str_lst = array(pointer, numberof(args) - 1)
205 for (i = 2; i <= numberof(args); ++i) {
206 str_lst(i - 1) = &MalString(val=args(i))
207 }
208 return MalList(val=&str_lst)
209}
210
211repl_env = nil
212
213func main(void)
214{
215 extern repl_env
216 repl_env = env_new(pointer(0))
217
218 // core.i: defined using Yorick
219 core_symbols = h_keys(core_ns)
220 for (i = 1; i <= numberof(core_symbols); ++i) {
221 env_set, repl_env, core_symbols(i), MalNativeFunction(val=core_symbols(i))
222 }
223 command_line_args = process_argv()
224 env_set, repl_env, "*ARGV*", prepare_argv_list(command_line_args)
225
226 // core.mal: defined using the language itself
227 RE, "(def! not (fn* (a) (if a false true)))", repl_env
228 RE, "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env
229 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
230 RE, "(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))))))))", repl_env
231
232 if (numberof(command_line_args) > 0) {
233 RE, "(load-file \"" + command_line_args(1) + "\")", repl_env
234 return 0
235 }
236
237 stdin_file = open("/dev/stdin", "r")
238 while (1) {
239 write, format="%s", "user> "
240 line = rdline(stdin_file, prompt="")
241 if (!line) break
242 if (strlen(line) > 0) {
243 result = REP(line, repl_env)
244 if (structof(result) == MalError) write, format="Error: %s\n", result.message
245 else write, format="%s\n", result
246 }
247 }
248 write, ""
249}
250
251main;