2 end entity step8_macros;
7 use WORK.pkg_readline.all;
14 architecture test of step8_macros is
16 shared variable repl_env: env_ptr;
18 procedure mal_READ(str: in string; ast: out mal_val_ptr; err: out mal_val_ptr) is
20 read_str(str, ast, err);
21 end procedure mal_READ;
23 procedure is_pair(ast: inout mal_val_ptr; pair: out boolean) is
25 pair := is_sequential_type(ast.val_type) and ast.seq_val'length > 0;
26 end procedure is_pair;
28 procedure quasiquote(ast: inout mal_val_ptr; result: out mal_val_ptr) is
29 variable ast_pair, a0_pair: boolean;
30 variable seq: mal_seq_ptr;
31 variable a0, rest: mal_val_ptr;
33 is_pair(ast, ast_pair);
35 seq := new mal_seq(0 to 1);
36 new_symbol("quote", seq(0));
38 new_seq_obj(mal_list, seq, result);
42 if a0.val_type = mal_symbol and a0.string_val.all = "unquote" then
43 result := ast.seq_val(1);
46 if a0_pair and a0.seq_val(0).val_type = mal_symbol and a0.seq_val(0).string_val.all = "splice-unquote" then
47 seq := new mal_seq(0 to 2);
48 new_symbol("concat", seq(0));
49 seq(1) := a0.seq_val(1);
50 seq_drop_prefix(ast, 1, rest);
51 quasiquote(rest, seq(2));
52 new_seq_obj(mal_list, seq, result);
54 seq := new mal_seq(0 to 2);
55 new_symbol("cons", seq(0));
56 quasiquote(a0, seq(1));
57 seq_drop_prefix(ast, 1, rest);
58 quasiquote(rest, seq(2));
59 new_seq_obj(mal_list, seq, result);
62 end procedure quasiquote;
64 -- Forward declaration
65 procedure EVAL(in_ast: inout mal_val_ptr; in_env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr);
67 procedure apply_func(fn: inout mal_val_ptr; args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr);
69 procedure is_macro_call(ast: inout mal_val_ptr; env: inout env_ptr; is_macro: out boolean) is
70 variable f, env_err: mal_val_ptr;
73 if ast.val_type = mal_list and
74 ast.seq_val'length > 0 and
75 ast.seq_val(0).val_type = mal_symbol then
76 env_get(env, ast.seq_val(0), f, env_err);
77 if env_err = null and f /= null and
78 f.val_type = mal_fn and f.func_val.f_is_macro then
82 end procedure is_macro_call;
84 procedure macroexpand(in_ast: inout mal_val_ptr; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
85 variable ast, macro_fn, call_args, macro_err: mal_val_ptr;
86 variable is_macro: boolean;
89 is_macro_call(ast, env, is_macro);
91 env_get(env, ast.seq_val(0), macro_fn, macro_err);
92 seq_drop_prefix(ast, 1, call_args);
93 apply_func(macro_fn, call_args, ast, macro_err);
94 if macro_err /= null then
98 is_macro_call(ast, env, is_macro);
101 end procedure macroexpand;
103 procedure fn_eval(args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
105 EVAL(args.seq_val(0), repl_env, result, err);
106 end procedure fn_eval;
108 procedure fn_swap(args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
109 variable atom: mal_val_ptr := args.seq_val(0);
110 variable fn: mal_val_ptr := args.seq_val(1);
111 variable call_args_seq: mal_seq_ptr;
112 variable call_args, eval_res, sub_err: mal_val_ptr;
114 call_args_seq := new mal_seq(0 to args.seq_val'length - 2);
115 call_args_seq(0) := atom.seq_val(0);
116 call_args_seq(1 to call_args_seq'length - 1) := args.seq_val(2 to args.seq_val'length - 1);
117 new_seq_obj(mal_list, call_args_seq, call_args);
118 apply_func(fn, call_args, eval_res, sub_err);
119 if sub_err /= null then
123 atom.seq_val(0) := eval_res;
125 end procedure fn_swap;
127 procedure apply_native_func(func_sym: inout mal_val_ptr; args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
129 if func_sym.string_val.all = "eval" then
130 fn_eval(args, result, err);
131 elsif func_sym.string_val.all = "swap!" then
132 fn_swap(args, result, err);
134 eval_native_func(func_sym, args, result, err);
136 end procedure apply_native_func;
138 procedure apply_func(fn: inout mal_val_ptr; args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
139 variable fn_env: env_ptr;
143 apply_native_func(fn, args, result, err);
145 new_env(fn_env, fn.func_val.f_env, fn.func_val.f_args, args);
146 EVAL(fn.func_val.f_body, fn_env, result, err);
148 new_string("not a function", err);
151 end procedure apply_func;
153 procedure eval_ast_seq(ast_seq: inout mal_seq_ptr; env: inout env_ptr; result: inout mal_seq_ptr; err: out mal_val_ptr) is
154 variable eval_err: mal_val_ptr;
156 result := new mal_seq(0 to ast_seq'length - 1);
157 for i in result'range loop
158 EVAL(ast_seq(i), env, result(i), eval_err);
159 if eval_err /= null then
164 end procedure eval_ast_seq;
166 procedure eval_ast(ast: inout mal_val_ptr; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
167 variable key, val, eval_err, env_err: mal_val_ptr;
168 variable new_seq: mal_seq_ptr;
173 env_get(env, ast, val, env_err);
174 if env_err /= null then
180 when mal_list | mal_vector | mal_hashmap =>
181 eval_ast_seq(ast.seq_val, env, new_seq, eval_err);
182 if eval_err /= null then
186 new_seq_obj(ast.val_type, new_seq, result);
192 end procedure eval_ast;
194 procedure EVAL(in_ast: inout mal_val_ptr; in_env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
196 variable ast, evaled_ast, a0, call_args, val, vars, sub_err, fn: mal_val_ptr;
197 variable env, let_env, fn_env: env_ptr;
202 if ast.val_type /= mal_list then
203 eval_ast(ast, env, result, err);
207 macroexpand(ast, env, ast, sub_err);
208 if sub_err /= null then
212 if ast.val_type /= mal_list then
213 eval_ast(ast, env, result, err);
216 if ast.seq_val'length = 0 then
221 a0 := ast.seq_val(0);
222 if a0.val_type = mal_symbol then
223 if a0.string_val.all = "def!" then
224 EVAL(ast.seq_val(2), env, val, sub_err);
225 if sub_err /= null then
229 env_set(env, ast.seq_val(1), val);
233 elsif a0.string_val.all = "let*" then
234 vars := ast.seq_val(1);
235 new_env(let_env, env);
237 while i < vars.seq_val'length loop
238 EVAL(vars.seq_val(i + 1), let_env, val, sub_err);
239 if sub_err /= null then
243 env_set(let_env, vars.seq_val(i), val);
247 ast := ast.seq_val(2);
250 elsif a0.string_val.all = "quote" then
251 result := ast.seq_val(1);
254 elsif a0.string_val.all = "quasiquote" then
255 quasiquote(ast.seq_val(1), ast);
258 elsif a0.string_val.all = "defmacro!" then
259 EVAL(ast.seq_val(2), env, val, sub_err);
260 if sub_err /= null then
264 val.func_val.f_is_macro := true;
265 env_set(env, ast.seq_val(1), val);
269 elsif a0.string_val.all = "macroexpand" then
270 macroexpand(ast.seq_val(1), env, result, err);
273 elsif a0.string_val.all = "do" then
274 for i in 1 to ast.seq_val'high - 1 loop
275 EVAL(ast.seq_val(i), env, result, sub_err);
276 if sub_err /= null then
281 ast := ast.seq_val(ast.seq_val'high);
284 elsif a0.string_val.all = "if" then
285 EVAL(ast.seq_val(1), env, val, sub_err);
286 if sub_err /= null then
290 if val.val_type = mal_nil or val.val_type = mal_false then
291 if ast.seq_val'length > 3 then
292 ast := ast.seq_val(3);
298 ast := ast.seq_val(2);
302 elsif a0.string_val.all = "fn*" then
303 new_fn(ast.seq_val(2), ast.seq_val(1), env, result);
309 eval_ast(ast, env, evaled_ast, sub_err);
310 if sub_err /= null then
314 seq_drop_prefix(evaled_ast, 1, call_args);
315 fn := evaled_ast.seq_val(0);
318 apply_native_func(fn, call_args, result, err);
321 new_env(fn_env, fn.func_val.f_env, fn.func_val.f_args, call_args);
323 ast := fn.func_val.f_body;
326 new_string("not a function", err);
332 procedure mal_PRINT(exp: inout mal_val_ptr; result: out line) is
334 pr_str(exp, true, result);
335 end procedure mal_PRINT;
337 procedure RE(str: in string; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
338 variable ast, read_err: mal_val_ptr;
340 mal_READ(str, ast, read_err);
341 if read_err /= null then
350 EVAL(ast, env, result, err);
353 procedure REP(str: in string; env: inout env_ptr; result: out line; err: out mal_val_ptr) is
354 variable eval_res, eval_err: mal_val_ptr;
356 RE(str, env, eval_res, eval_err);
357 if eval_err /= null then
362 mal_PRINT(eval_res, result);
365 procedure set_argv(e: inout env_ptr; program_file: inout line) is
366 variable argv_var_name: string(1 to 6) := "*ARGV*";
367 variable argv_sym, argv_list: mal_val_ptr;
369 variable status: file_open_status;
370 variable one_line: line;
371 variable seq: mal_seq_ptr;
372 variable element: mal_val_ptr;
374 program_file := null;
375 seq := new mal_seq(0 to -1);
376 file_open(status, f, external_name => "vhdl_argv.tmp", open_kind => read_mode);
377 if status = open_ok then
378 if not endfile(f) then
379 readline(f, program_file);
380 while not endfile(f) loop
381 readline(f, one_line);
382 new_string(one_line.all, element);
383 seq := new mal_seq'(seq.all & element);
388 new_seq_obj(mal_list, seq, argv_list);
389 new_symbol(argv_var_name, argv_sym);
390 env_set(e, argv_sym, argv_list);
391 end procedure set_argv;
394 variable is_eof: boolean;
395 variable program_file, input_line, result: line;
396 variable eval_sym, eval_fn, dummy_val, err: mal_val_ptr;
397 variable outer: env_ptr;
398 variable eval_func_name: string(1 to 4) := "eval";
401 new_env(repl_env, outer);
403 -- core.EXT: defined using VHDL (see core.vhdl)
404 define_core_functions(repl_env);
405 new_symbol(eval_func_name, eval_sym);
406 new_nativefn(eval_func_name, eval_fn);
407 env_set(repl_env, eval_sym, eval_fn);
408 set_argv(repl_env, program_file);
410 -- core.mal: defined using the language itself
411 RE("(def! not (fn* (a) (if a false true)))", repl_env, dummy_val, err);
412 RE("(def! load-file (fn* (f) (eval (read-string (str " & '"' & "(do " & '"' & " (slurp f) " & '"' & "\nnil)" & '"' & ")))))", repl_env, dummy_val, err);
413 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, dummy_val, err);
415 if program_file /= null then
416 REP("(load-file " & '"' & program_file.all & '"' & ")", repl_env, result, err);
421 mal_readline("user> ", is_eof, input_line);
423 next when input_line'length = 0;
424 REP(input_line.all, repl_env, result, err);
426 pr_str(err, false, result);
427 result := new string'("Error: " & result.all);
429 if result /= null then
430 mal_printline(result.all);
440 end architecture test;