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
)
37 %fprintf('EVAL: %s\n', printer.pr_str(ast, true));
38 if ~type_utils.list_Q
(ast
)
39 ret
= eval_ast
(ast
, env
);
48 if isa(ast.
get(1),'types.Symbol')
49 a1sym
= ast.
get(1).name
;
55 ret
= env.
set(ast.
get(2), EVAL(ast.
get(3), env
));
59 for i
=1:2:length(ast.
get(2))
60 let_env.
set(ast.
get(2).
get(i
), EVAL(ast.
get(2).
get(i
+1), let_env
));
63 ast
= ast.
get(3); % TCO
65 el
= eval_ast
(ast.
slice(2,length(ast
)-1), env
);
66 ast
= ast.
get(length(ast
)); % TCO
68 cond = EVAL(ast.
get(2), env
);
69 if strcmp(class(cond), 'types.Nil') || ...
70 (islogical
(cond) && cond == false
)
72 ast
= ast.
get(4); % TCO
78 ast
= ast.
get(3); % TCO
81 fn
= @
(varargin) EVAL(ast.
get(3), Env
({env
}, ast.
get(2), ...
82 types.List
(varargin{:})));
83 ret
= types.
Function(fn
, ast.
get(3), env
, ast.
get(2));
86 el
= eval_ast
(ast
, env
);
89 if isa(f
, 'types.Function')
90 env
= Env
({f.env
}, f.params
, args
);
93 ret
= f
(args.data
{:});
101 function ret
= PRINT(ast
)
102 ret
= printer.pr_str
(ast
, true
);
106 function ret
= rep
(str
, env
)
107 ret
= PRINT(EVAL(READ
(str
), env
));
113 % core.m: defined using matlab
114 ns
= core.ns
(); ks
= ns.keys
();
117 repl_env.
set(types.Symbol
(k
), ns
(k
));
119 repl_env.
set(types.Symbol
('eval'), @
(a
) EVAL(a
, repl_env
));
120 rest_args
= args
(2:end);
121 repl_env.
set(types.Symbol
('*ARGV*'), types.List
(rest_args
{:}));
123 % core.mal: defined using the langauge itself
124 rep
('(def! not (fn* (a) (if a false true)))', repl_env
);
125 rep
('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env
);
128 rep
(sprintf('(load-file "%s")', args
{1}), repl_env
);
132 %cleanObj = onCleanup(@() disp('*** here1 ***'));
135 line = input('user> ', 's');
139 if strcmp(strtrim
(line),''), continue
, end
141 fprintf('%s\n', rep
(line, repl_env
));
143 fprintf('Error: %s\n', err.message
);
144 type_utils.print_stack
(err
);