matlab: add metadata and atom support.
[jackhill/mal.git] / matlab / step8_macros.m
CommitLineData
c4033aab
JM
1function step8_macros(varargin), main(varargin), end
2
3% read
4function ret = READ(str)
5 ret = reader.read_str(str);
6end
7
8% eval
9function ret = is_pair(ast)
6a572dff 10 ret = types.sequential_Q(ast) && length(ast) > 0;
c4033aab
JM
11end
12
13function ret = quasiquote(ast)
14 if ~is_pair(ast)
6a572dff
JM
15 ret = types.List(types.Symbol('quote'), ast);
16 elseif isa(ast.get(1),'types.Symbol') && ...
17 strcmp(ast.get(1).name, 'unquote')
18 ret = ast.get(2);
19 elseif is_pair(ast.get(1)) && ...
20 isa(ast.get(1).get(1),'types.Symbol') && ...
21 strcmp(ast.get(1).get(1).name, 'splice-unquote')
22 ret = types.List(types.Symbol('concat'), ...
23 ast.get(1).get(2), ...
24 quasiquote(ast.slice(2)));
c4033aab 25 else
6a572dff
JM
26 ret = types.List(types.Symbol('cons'), ...
27 quasiquote(ast.get(1)), ...
28 quasiquote(ast.slice(2)));
c4033aab
JM
29 end
30end
31
32function ret = is_macro_call(ast, env)
6a572dff
JM
33 if types.list_Q(ast) && isa(ast.get(1), 'types.Symbol') && ...
34 ~islogical(env.find(ast.get(1)))
35 f = env.get(ast.get(1));
7f567f36 36 ret = isa(f,'types.Function') && f.is_macro;
c4033aab
JM
37 else
38 ret = false;
39 end
40end
41
42function ret = macroexpand(ast, env)
43 while is_macro_call(ast, env)
6a572dff
JM
44 mac = env.get(ast.get(1));
45 args = ast.slice(2);
46 ast = mac.fn(args.data{:});
c4033aab
JM
47 end
48 ret = ast;
49end
50
51function ret = eval_ast(ast, env)
52 switch class(ast)
53 case 'types.Symbol'
54 ret = env.get(ast);
6a572dff
JM
55 case 'types.List'
56 ret = types.List();
c4033aab 57 for i=1:length(ast)
6a572dff
JM
58 ret.append(EVAL(ast.get(i), env));
59 end
60 case 'types.Vector'
61 ret = types.Vector();
62 for i=1:length(ast)
63 ret.append(EVAL(ast.get(i), env));
64 end
65 case 'types.HashMap'
66 ret = types.HashMap();
67 ks = ast.keys();
68 for i=1:length(ks)
69 k = ks{i};
70 ret.set(EVAL(k, env), EVAL(ast.get(k), env));
c4033aab
JM
71 end
72 otherwise
73 ret = ast;
74 end
75end
76
77function ret = EVAL(ast, env)
78 while true
6a572dff 79 if ~types.list_Q(ast)
c4033aab
JM
80 ret = eval_ast(ast, env);
81 return;
82 end
83
84 % apply
85 ast = macroexpand(ast, env);
6a572dff 86 if ~types.list_Q(ast)
c4033aab
JM
87 ret = ast;
88 return;
89 end
90
6a572dff
JM
91 if isa(ast.get(1),'types.Symbol')
92 a1sym = ast.get(1).name;
c4033aab
JM
93 else
94 a1sym = '_@$fn$@_';
95 end
96 switch (a1sym)
97 case 'def!'
6a572dff 98 ret = env.set(ast.get(2), EVAL(ast.get(3), env));
c4033aab
JM
99 return;
100 case 'let*'
101 let_env = Env(env);
6a572dff
JM
102 for i=1:2:length(ast.get(2))
103 let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env));
c4033aab
JM
104 end
105 env = let_env;
6a572dff 106 ast = ast.get(3); % TCO
c4033aab 107 case 'quote'
6a572dff 108 ret = ast.get(2);
c4033aab
JM
109 return;
110 case 'quasiquote'
6a572dff 111 ast = quasiquote(ast.get(2)); % TCO
c4033aab 112 case 'defmacro!'
6a572dff 113 ret = env.set(ast.get(2), EVAL(ast.get(3), env));
c4033aab
JM
114 ret.is_macro = true;
115 return;
116 case 'macroexpand'
6a572dff 117 ret = macroexpand(ast.get(2), env);
c4033aab
JM
118 return;
119 case 'do'
6a572dff
JM
120 el = eval_ast(ast.slice(2,length(ast)-1), env);
121 ast = ast.get(length(ast)); % TCO
c4033aab 122 case 'if'
6a572dff 123 cond = EVAL(ast.get(2), env);
c4033aab
JM
124 if strcmp(class(cond), 'types.Nil') || ...
125 (islogical(cond) && cond == false)
126 if length(ast) > 3
6a572dff 127 ast = ast.get(4); % TCO
c4033aab
JM
128 else
129 ret = types.nil;
130 return;
131 end
132 else
6a572dff 133 ast = ast.get(3); % TCO
c4033aab
JM
134 end
135 case 'fn*'
6a572dff
JM
136 fn = @(varargin) EVAL(ast.get(3), Env(env, ast.get(2), ...
137 types.List(varargin{:})));
138 ret = types.Function(fn, ast.get(3), env, ast.get(2));
c4033aab
JM
139 return;
140 otherwise
141 el = eval_ast(ast, env);
6a572dff
JM
142 f = el.get(1);
143 args = el.slice(2);
b550d8b7 144 if isa(f, 'types.Function')
c4033aab
JM
145 env = Env(f.env, f.params, args);
146 ast = f.ast; % TCO
147 else
6a572dff 148 ret = f(args.data{:});
c4033aab
JM
149 return
150 end
151 end
152 end
153end
154
155% print
156function ret = PRINT(ast)
157 ret = printer.pr_str(ast, true);
158end
159
160% REPL
161function ret = rep(str, env)
162 ret = PRINT(EVAL(READ(str), env));
163end
164
165function main(args)
166 repl_env = Env(false);
167
168 % core.m: defined using matlab
169 ns = core.ns(); ks = ns.keys();
170 for i=1:length(ks)
171 k = ks{i};
172 repl_env.set(types.Symbol(k), ns(k));
173 end
174 repl_env.set(types.Symbol('eval'), @(a) EVAL(a, repl_env));
6a572dff
JM
175 rest_args = args(2:end);
176 repl_env.set(types.Symbol('*ARGV*'), types.List(rest_args{:}));
c4033aab
JM
177
178 % core.mal: defined using the langauge itself
179 rep('(def! not (fn* (a) (if a false true)))', repl_env);
180 rep('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env);
181 rep('(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);
182 rep('(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);
183
184 if ~isempty(args)
185 rep(strcat('(load-file "', args{1}, '")'), repl_env);
186 quit;
187 end
188
189 %cleanObj = onCleanup(@() disp('*** here1 ***'));
190 while (true)
191 line = input('user> ', 's');
192 if strcmp(strtrim(line),''), continue, end
193 try
194 fprintf('%s\n', rep(line, repl_env));
195 catch err
196 fprintf('Error: %s\n', err.message);
197 fprintf('%s\n', getReport(err, 'extended'));
198 end
199 end
200end