1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
function step6_file(varargin), main(varargin), end
% read
function ret = READ(str)
ret = reader.read_str(str);
end
% eval
function ret = eval_ast(ast, env)
switch class(ast)
case 'types.Symbol'
ret = env.get(ast);
case 'types.List'
ret = types.List();
for i=1:length(ast)
ret.append(EVAL(ast.get(i), env));
end
case 'types.Vector'
ret = types.Vector();
for i=1:length(ast)
ret.append(EVAL(ast.get(i), env));
end
case 'types.HashMap'
ret = types.HashMap();
ks = ast.keys();
for i=1:length(ks)
k = ks{i};
ret.set(EVAL(k, env), EVAL(ast.get(k), env));
end
otherwise
ret = ast;
end
end
function ret = EVAL(ast, env)
while true
%fprintf('EVAL: %s\n', printer.pr_str(ast, true));
if ~types.list_Q(ast)
ret = eval_ast(ast, env);
return;
end
% apply
if isa(ast.get(1),'types.Symbol')
a1sym = ast.get(1).name;
else
a1sym = '_@$fn$@_';
end
switch (a1sym)
case 'def!'
ret = env.set(ast.get(2), EVAL(ast.get(3), env));
return;
case 'let*'
let_env = Env(env);
for i=1:2:length(ast.get(2))
let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env));
end
env = let_env;
ast = ast.get(3); % TCO
case 'do'
el = eval_ast(ast.slice(2,length(ast)-1), env);
ast = ast.get(length(ast)); % TCO
case 'if'
cond = EVAL(ast.get(2), env);
if strcmp(class(cond), 'types.Nil') || ...
(islogical(cond) && cond == false)
if length(ast) > 3
ast = ast.get(4); % TCO
else
ret = types.nil;
return;
end
else
ast = ast.get(3); % TCO
end
case 'fn*'
fn = @(varargin) EVAL(ast.get(3), Env(env, ast.get(2), ...
types.List(varargin{:})));
ret = types.Function(fn, ast.get(3), env, ast.get(2));
return;
otherwise
el = eval_ast(ast, env);
f = el.get(1);
args = el.slice(2);
if isa(f, 'types.Function')
env = Env(f.env, f.params, args);
ast = f.ast; % TCO
else
ret = f(args.data{:});
return
end
end
end
end
% print
function ret = PRINT(ast)
ret = printer.pr_str(ast, true);
end
% REPL
function ret = rep(str, env)
ret = PRINT(EVAL(READ(str), env));
end
function main(args)
repl_env = Env(false);
% core.m: defined using matlab
ns = core.ns(); ks = ns.keys();
for i=1:length(ks)
k = ks{i};
repl_env.set(types.Symbol(k), ns(k));
end
repl_env.set(types.Symbol('eval'), @(a) EVAL(a, repl_env));
rest_args = args(2:end);
repl_env.set(types.Symbol('*ARGV*'), types.List(rest_args{:}));
% core.mal: defined using the langauge itself
rep('(def! not (fn* (a) (if a false true)))', repl_env);
rep('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env);
if ~isempty(args)
rep(sprintf('(load-file "%s")', args{1}), repl_env);
quit;
end
%cleanObj = onCleanup(@() disp('*** here1 ***'));
while (true)
line = input('user> ', 's');
if strcmp(strtrim(line),''), continue, end
try
fprintf('%s\n', rep(line, repl_env));
catch err
fprintf('Error: %s\n', err.message);
fprintf('%s\n', getReport(err, 'extended'));
end
end
end
|