1 function step6_file
(varargin), main
(varargin), end
4 function ret
= READ
(str
)
5 ret
= reader.read_str
(str
);
9 function ret
= eval_ast
(ast
, env
)
16 ret.append
(EVAL(ast.
get(i
), env
));
21 ret.append
(EVAL(ast.
get(i
), env
));
24 ret
= types.HashMap
();
28 ret.
set(EVAL(k
, env
), EVAL(ast.
get(k
), env
));
35 function ret
= EVAL(ast
, env
)
38 ret
= eval_ast
(ast
, env
);
43 if isa(ast.
get(1),'types.Symbol')
44 a1sym
= ast.
get(1).name
;
50 ret
= env.
set(ast.
get(2), EVAL(ast.
get(3), env
));
54 for i
=1:2:length(ast.
get(2))
55 let_env.
set(ast.
get(2).
get(i
), EVAL(ast.
get(2).
get(i
+1), let_env
));
58 ast
= ast.
get(3); % TCO
60 el
= eval_ast
(ast.
slice(2,length(ast
)-1), env
);
61 ast
= ast.
get(length(ast
)); % TCO
63 cond = EVAL(ast.
get(2), env
);
64 if strcmp(class(cond), 'types.Nil') || ...
65 (islogical
(cond) && cond == false
)
67 ast
= ast.
get(4); % TCO
73 ast
= ast.
get(3); % TCO
76 fn
= @
(varargin) EVAL(ast.
get(3), Env
(env
, ast.
get(2), ...
77 types.List
(varargin{:})));
78 ret
= types.
Function(fn
, ast.
get(3), env
, ast.
get(2));
81 el
= eval_ast
(ast
, env
);
84 if isa(f
, 'types.Function')
85 env
= Env
(f.env
, f.params
, args
);
88 ret
= f
(args.data
{:});
96 function ret
= PRINT(ast
)
97 ret
= printer.pr_str
(ast
, true
);
101 function ret
= rep
(str
, env
)
102 ret
= PRINT(EVAL(READ
(str
), env
));
106 repl_env
= Env
(false
);
108 % core.m: defined using matlab
109 ns
= core.ns
(); ks
= ns.keys
();
112 repl_env.
set(types.Symbol
(k
), ns
(k
));
114 repl_env.
set(types.Symbol
('eval'), @
(a
) EVAL(a
, repl_env
));
115 rest_args
= args
(2:end);
116 repl_env.
set(types.Symbol
('*ARGV*'), types.List
(rest_args
{:}));
118 % core.mal: defined using the langauge itself
119 rep
('(def! not (fn* (a) (if a false true)))', repl_env
);
120 rep
('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env
);
123 rep
(strcat
('(load-file "', args
{1}, '")'), repl_env
);
127 %cleanObj = onCleanup(@() disp('*** here1 ***'));
129 line = input('user> ', 's');
130 if strcmp(strtrim
(line),''), continue
, end
132 fprintf('%s\n', rep
(line, repl_env
));
134 fprintf('Error: %s\n', err.message
);
135 fprintf('%s\n', getReport
(err
, 'extended'));