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 starts_with(lst : inout mal_val_ptr;
27 res := lst.seq_val.all'length = 2
28 and lst.seq_val.all (lst.seq_val.all'low).val_type = mal_symbol
29 and lst.seq_val.all (lst.seq_val.all'low).string_val.all = sym;
32 -- Forward declaration
33 procedure quasiquote(ast: inout mal_val_ptr;
34 result: out mal_val_ptr);
36 procedure qq_loop(elt : inout mal_val_ptr;
37 acc : inout mal_val_ptr) is
38 variable sw : boolean := elt.val_type = mal_list;
39 variable seq : mal_seq_ptr := new mal_seq(0 to 2);
42 starts_with(elt, "splice-unquote", sw);
45 new_symbol("concat", seq(0));
46 seq(1) := elt.seq_val(1);
48 new_symbol("cons", seq(0));
49 quasiquote(elt, seq(1));
52 new_seq_obj(mal_list, seq, acc);
55 procedure qq_foldr (xs : inout mal_seq_ptr;
56 res : out mal_val_ptr) is
57 variable seq : mal_seq_ptr := new mal_seq(0 to -1);
58 variable acc : mal_val_ptr;
60 new_seq_obj(mal_list, seq, acc);
61 for i in xs'reverse_range loop
65 end procedure qq_foldr;
67 procedure quasiquote(ast: inout mal_val_ptr;
68 result: out mal_val_ptr) is
69 variable sw : boolean;
70 variable seq : mal_seq_ptr;
74 starts_with(ast, "unquote", sw);
76 result := ast.seq_val(1);
78 qq_foldr(ast.seq_val, result);
81 seq := new mal_seq(0 to 1);
82 new_symbol("vec", seq(0));
83 qq_foldr(ast.seq_val, seq(1));
84 new_seq_obj(mal_list, seq, result);
85 when mal_symbol | mal_hashmap =>
86 seq := new mal_seq(0 to 1);
87 new_symbol("quote", seq(0));
89 new_seq_obj(mal_list, seq, result);
93 end procedure quasiquote;
95 -- Forward declaration
96 procedure EVAL(in_ast: inout mal_val_ptr; in_env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr);
98 procedure apply_func(fn: inout mal_val_ptr; args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr);
100 procedure is_macro_call(ast: inout mal_val_ptr; env: inout env_ptr; is_macro: out boolean) is
101 variable f, env_err: mal_val_ptr;
104 if ast.val_type = mal_list and
105 ast.seq_val'length > 0 and
106 ast.seq_val(0).val_type = mal_symbol then
107 env_get(env, ast.seq_val(0), f, env_err);
108 if env_err = null and f /= null and
109 f.val_type = mal_fn and f.func_val.f_is_macro then
113 end procedure is_macro_call;
115 procedure macroexpand(in_ast: inout mal_val_ptr; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
116 variable ast, macro_fn, call_args, macro_err: mal_val_ptr;
117 variable is_macro: boolean;
120 is_macro_call(ast, env, is_macro);
122 env_get(env, ast.seq_val(0), macro_fn, macro_err);
123 seq_drop_prefix(ast, 1, call_args);
124 apply_func(macro_fn, call_args, ast, macro_err);
125 if macro_err /= null then
129 is_macro_call(ast, env, is_macro);
132 end procedure macroexpand;
134 procedure fn_eval(args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
136 EVAL(args.seq_val(0), repl_env, result, err);
137 end procedure fn_eval;
139 procedure fn_swap(args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
140 variable atom: mal_val_ptr := args.seq_val(0);
141 variable fn: mal_val_ptr := args.seq_val(1);
142 variable call_args_seq: mal_seq_ptr;
143 variable call_args, eval_res, sub_err: mal_val_ptr;
145 call_args_seq := new mal_seq(0 to args.seq_val'length - 2);
146 call_args_seq(0) := atom.seq_val(0);
147 call_args_seq(1 to call_args_seq'length - 1) := args.seq_val(2 to args.seq_val'length - 1);
148 new_seq_obj(mal_list, call_args_seq, call_args);
149 apply_func(fn, call_args, eval_res, sub_err);
150 if sub_err /= null then
154 atom.seq_val(0) := eval_res;
156 end procedure fn_swap;
158 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
160 if func_sym.string_val.all = "eval" then
161 fn_eval(args, result, err);
162 elsif func_sym.string_val.all = "swap!" then
163 fn_swap(args, result, err);
165 eval_native_func(func_sym, args, result, err);
167 end procedure apply_native_func;
169 procedure apply_func(fn: inout mal_val_ptr; args: inout mal_val_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
170 variable fn_env: env_ptr;
174 apply_native_func(fn, args, result, err);
176 new_env(fn_env, fn.func_val.f_env, fn.func_val.f_args, args);
177 EVAL(fn.func_val.f_body, fn_env, result, err);
179 new_string("not a function", err);
182 end procedure apply_func;
184 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
185 variable eval_err: mal_val_ptr;
187 result := new mal_seq(0 to ast_seq'length - 1);
188 for i in result'range loop
189 EVAL(ast_seq(i), env, result(i), eval_err);
190 if eval_err /= null then
195 end procedure eval_ast_seq;
197 procedure eval_ast(ast: inout mal_val_ptr; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
198 variable key, val, eval_err, env_err: mal_val_ptr;
199 variable new_seq: mal_seq_ptr;
204 env_get(env, ast, val, env_err);
205 if env_err /= null then
211 when mal_list | mal_vector | mal_hashmap =>
212 eval_ast_seq(ast.seq_val, env, new_seq, eval_err);
213 if eval_err /= null then
217 new_seq_obj(ast.val_type, new_seq, result);
223 end procedure eval_ast;
225 procedure EVAL(in_ast: inout mal_val_ptr; in_env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
227 variable ast, evaled_ast, a0, call_args, val, vars, sub_err, fn: mal_val_ptr;
228 variable env, let_env, fn_env: env_ptr;
233 if ast.val_type /= mal_list then
234 eval_ast(ast, env, result, err);
238 macroexpand(ast, env, ast, sub_err);
239 if sub_err /= null then
243 if ast.val_type /= mal_list then
244 eval_ast(ast, env, result, err);
247 if ast.seq_val'length = 0 then
252 a0 := ast.seq_val(0);
253 if a0.val_type = mal_symbol then
254 if a0.string_val.all = "def!" then
255 EVAL(ast.seq_val(2), env, val, sub_err);
256 if sub_err /= null then
260 env_set(env, ast.seq_val(1), val);
264 elsif a0.string_val.all = "let*" then
265 vars := ast.seq_val(1);
266 new_env(let_env, env);
268 while i < vars.seq_val'length loop
269 EVAL(vars.seq_val(i + 1), let_env, val, sub_err);
270 if sub_err /= null then
274 env_set(let_env, vars.seq_val(i), val);
278 ast := ast.seq_val(2);
281 elsif a0.string_val.all = "quote" then
282 result := ast.seq_val(1);
285 elsif a0.string_val.all = "quasiquoteexpand" then
286 quasiquote(ast.seq_val(1), result);
289 elsif a0.string_val.all = "quasiquote" then
290 quasiquote(ast.seq_val(1), ast);
293 elsif a0.string_val.all = "defmacro!" then
294 EVAL(ast.seq_val(2), env, val, sub_err);
295 if sub_err /= null then
299 val.func_val.f_is_macro := true;
300 env_set(env, ast.seq_val(1), val);
304 elsif a0.string_val.all = "macroexpand" then
305 macroexpand(ast.seq_val(1), env, result, err);
308 elsif a0.string_val.all = "do" then
309 for i in 1 to ast.seq_val'high - 1 loop
310 EVAL(ast.seq_val(i), env, result, sub_err);
311 if sub_err /= null then
316 ast := ast.seq_val(ast.seq_val'high);
319 elsif a0.string_val.all = "if" then
320 EVAL(ast.seq_val(1), env, val, sub_err);
321 if sub_err /= null then
325 if val.val_type = mal_nil or val.val_type = mal_false then
326 if ast.seq_val'length > 3 then
327 ast := ast.seq_val(3);
333 ast := ast.seq_val(2);
337 elsif a0.string_val.all = "fn*" then
338 new_fn(ast.seq_val(2), ast.seq_val(1), env, result);
344 eval_ast(ast, env, evaled_ast, sub_err);
345 if sub_err /= null then
349 seq_drop_prefix(evaled_ast, 1, call_args);
350 fn := evaled_ast.seq_val(0);
353 apply_native_func(fn, call_args, result, err);
356 new_env(fn_env, fn.func_val.f_env, fn.func_val.f_args, call_args);
358 ast := fn.func_val.f_body;
361 new_string("not a function", err);
367 procedure mal_PRINT(exp: inout mal_val_ptr; result: out line) is
369 pr_str(exp, true, result);
370 end procedure mal_PRINT;
372 procedure RE(str: in string; env: inout env_ptr; result: out mal_val_ptr; err: out mal_val_ptr) is
373 variable ast, read_err: mal_val_ptr;
375 mal_READ(str, ast, read_err);
376 if read_err /= null then
385 EVAL(ast, env, result, err);
388 procedure REP(str: in string; env: inout env_ptr; result: out line; err: out mal_val_ptr) is
389 variable eval_res, eval_err: mal_val_ptr;
391 RE(str, env, eval_res, eval_err);
392 if eval_err /= null then
397 mal_PRINT(eval_res, result);
400 procedure set_argv(e: inout env_ptr; program_file: inout line) is
401 variable argv_var_name: string(1 to 6) := "*ARGV*";
402 variable argv_sym, argv_list: mal_val_ptr;
404 variable status: file_open_status;
405 variable one_line: line;
406 variable seq: mal_seq_ptr;
407 variable element: mal_val_ptr;
409 program_file := null;
410 seq := new mal_seq(0 to -1);
411 file_open(status, f, external_name => "vhdl_argv.tmp", open_kind => read_mode);
412 if status = open_ok then
413 if not endfile(f) then
414 readline(f, program_file);
415 while not endfile(f) loop
416 readline(f, one_line);
417 new_string(one_line.all, element);
418 seq := new mal_seq'(seq.all & element);
423 new_seq_obj(mal_list, seq, argv_list);
424 new_symbol(argv_var_name, argv_sym);
425 env_set(e, argv_sym, argv_list);
426 end procedure set_argv;
429 variable is_eof: boolean;
430 variable program_file, input_line, result: line;
431 variable eval_sym, eval_fn, dummy_val, err: mal_val_ptr;
432 variable outer: env_ptr;
433 variable eval_func_name: string(1 to 4) := "eval";
436 new_env(repl_env, outer);
438 -- core.EXT: defined using VHDL (see core.vhdl)
439 define_core_functions(repl_env);
440 new_symbol(eval_func_name, eval_sym);
441 new_nativefn(eval_func_name, eval_fn);
442 env_set(repl_env, eval_sym, eval_fn);
443 set_argv(repl_env, program_file);
445 -- core.mal: defined using the language itself
446 RE("(def! not (fn* (a) (if a false true)))", repl_env, dummy_val, err);
447 RE("(def! load-file (fn* (f) (eval (read-string (str " & '"' & "(do " & '"' & " (slurp f) " & '"' & "\nnil)" & '"' & ")))))", repl_env, dummy_val, err);
448 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);
450 if program_file /= null then
451 REP("(load-file " & '"' & program_file.all & '"' & ")", repl_env, result, err);
456 mal_readline("user> ", is_eof, input_line);
458 next when input_line'length = 0;
459 REP(input_line.all, repl_env, result, err);
461 pr_str(err, false, result);
462 result := new string'("Error: " & result.all);
464 if result /= null then
465 mal_printline(result.all);
475 end architecture test;