8 CREATE OR REPLACE PACKAGE mal
IS
10 FUNCTION MAIN(args
varchar DEFAULT '()') RETURN integer;
15 CREATE OR REPLACE PACKAGE BODY mal
IS
17 FUNCTION MAIN(args
varchar DEFAULT '()') RETURN integer IS
18 M types.mal_table
; -- general mal value memory pool
19 H types.map_entry_table
; -- hashmap memory pool
20 E env_pkg.env_entry_table
; -- mal env memory pool
29 FUNCTION READ(line
varchar) RETURN integer IS
31 RETURN reader.
read_str(M
, H
, line
);
36 -- forward declarations
37 FUNCTION EVAL(orig_ast
integer, orig_env
integer) RETURN integer;
38 FUNCTION do_builtin(fn
integer, args mal_vals
) RETURN integer;
40 FUNCTION eval_ast(ast
integer, env
integer) RETURN integer IS
49 IF M(ast
).type_id
= 7 THEN
50 RETURN env_pkg.
env_get(M
, E
, env
, ast
);
51 ELSIF
M(ast
).type_id
IN (8,9) THEN
52 old_seq
:= TREAT(M(ast
) AS mal_seq_T
).val_seq
;
53 new_seq
:= mal_vals();
54 new_seq.
EXTEND(old_seq.
COUNT);
55 FOR i
IN 1..old_seq.
COUNT LOOP
56 new_seq(i
) := EVAL(old_seq(i
), env
);
58 RETURN types.
seq(M
, M(ast
).type_id
, new_seq
);
59 ELSIF
M(ast
).type_id
IN (10) THEN
60 new_hm
:= types.
hash_map(M
, H
, mal_vals());
61 old_midx
:= TREAT(M(ast
) AS mal_map_T
).map_idx
;
62 new_midx
:= TREAT(M(new_hm
) AS mal_map_T
).map_idx
;
64 k
:= H(old_midx
).
FIRST();
65 WHILE k
IS NOT NULL LOOP
66 H(new_midx
)(k
) := EVAL(H(old_midx
)(k
), env
);
67 k
:= H(old_midx
).
NEXT(k
);
75 FUNCTION EVAL(orig_ast
integer, orig_env
integer) RETURN integer IS
76 ast
integer := orig_ast
;
77 env
integer := orig_env
;
90 -- io.writeline('EVAL: ' || printer.pr_str(M, ast));
91 IF M(ast
).type_id
<> 8 THEN
92 RETURN eval_ast(ast
, env
);
94 IF types.
count(M
, ast
) = 0 THEN
95 RETURN ast
; -- empty list just returned
99 a0
:= types.
first(M
, ast
);
100 if M(a0
).type_id
= 7 THEN -- symbol
101 a0sym
:= TREAT(M(a0
) AS mal_str_T
).val_str
;
103 a0sym
:= '__<*fn*>__';
107 WHEN a0sym
= 'def!' THEN
108 RETURN env_pkg.
env_set(M
, E
, env
,
109 types.
nth(M
, ast
, 1), EVAL(types.
nth(M
, ast
, 2), env
));
110 WHEN a0sym
= 'let*' THEN
111 let_env
:= env_pkg.
env_new(M
, E
, env
);
112 seq
:= TREAT(M(types.
nth(M
, ast
, 1)) AS mal_seq_T
).val_seq
;
114 WHILE i
<= seq.
COUNT LOOP
115 x
:= env_pkg.
env_set(M
, E
, let_env
,
116 seq(i
), EVAL(seq(i
+1), let_env
));
120 ast
:= types.
nth(M
, ast
, 2); -- TCO
121 WHEN a0sym
= 'do' THEN
122 x
:= types.
slice(M
, ast
, 1, types.
count(M
, ast
)-2);
123 x
:= eval_ast(x
, env
);
124 ast
:= types.
nth(M
, ast
, types.
count(M
, ast
)-1); -- TCO
125 WHEN a0sym
= 'if' THEN
126 cond
:= EVAL(types.
nth(M
, ast
, 1), env
);
127 IF cond
= 1 OR cond
= 2 THEN -- nil or false
128 IF types.
count(M
, ast
) > 3 THEN
129 ast
:= types.
nth(M
, ast
, 3); -- TCO
134 ast
:= types.
nth(M
, ast
, 2); -- TCO
136 WHEN a0sym
= 'fn*' THEN
137 RETURN types.
malfunc(M
, types.
nth(M
, ast
, 2),
138 types.
nth(M
, ast
, 1),
141 el
:= eval_ast(ast
, env
);
142 f
:= types.
first(M
, el
);
143 args
:= TREAT(M(types.
slice(M
, el
, 1)) AS mal_seq_T
).val_seq
;
144 IF M(f
).type_id
= 12 THEN
145 malfn
:= TREAT(M(f
) AS mal_func_T
);
146 env
:= env_pkg.
env_new(M
, E
, malfn.env
,
148 ast
:= malfn.ast
; -- TCO
150 RETURN do_builtin(f
, args
);
158 -- hack to get around lack of function references
159 -- functions that require special access to repl_env or EVAL
160 -- are implemented directly here, otherwise, core.do_core_fn
162 FUNCTION do_builtin(fn
integer, args mal_vals
) RETURN integer IS
170 fname
:= TREAT(M(fn
) AS mal_str_T
).val_str
;
172 WHEN fname
= 'do_eval' THEN
173 RETURN EVAL(args(1), repl_env
);
174 WHEN fname
= 'swap!' THEN
175 val
:= TREAT(M(args(1)) AS mal_atom_T
).val
;
177 -- slice one extra at the beginning that will be changed
178 -- to the value of the atom
179 fargs
:= TREAT(M(types.
slice(M
, args
, 1)) AS mal_seq_T
).val_seq
;
181 IF M(f
).type_id
= 12 THEN
182 malfn
:= TREAT(M(f
) AS mal_func_T
);
183 fn_env
:= env_pkg.
env_new(M
, E
, malfn.env
,
184 malfn.params
, fargs
);
185 val
:= EVAL(malfn.ast
, fn_env
);
187 val
:= do_builtin(f
, fargs
);
189 RETURN types.
atom_reset(M
, args(1), val
);
191 RETURN core.
do_core_func(M
, H
, fn
, args
);
197 FUNCTION PRINT(exp integer) RETURN varchar IS
199 RETURN printer.
pr_str(M
, H
, exp);
203 FUNCTION REP(line
varchar) RETURN varchar IS
205 RETURN PRINT(EVAL(READ(line
), repl_env
));
209 -- initialize memory pools
210 M
:= types.
mem_new();
211 H
:= types.
map_entry_table();
212 E
:= env_pkg.
env_entry_table();
214 repl_env
:= env_pkg.
env_new(M
, E
, NULL);
216 argv
:= TREAT(M(reader.
read_str(M
, H
, args
)) AS mal_seq_T
).val_seq
;
218 -- core.EXT: defined using PL/SQL
219 core_ns
:= core.
get_core_ns();
220 FOR cidx
IN 1..core_ns.
COUNT LOOP
221 x
:= env_pkg.
env_set(M
, E
, repl_env
,
222 types.
symbol(M
, core_ns(cidx
)),
223 types.
func(M
, core_ns(cidx
)));
225 x
:= env_pkg.
env_set(M
, E
, repl_env
,
226 types.
symbol(M
, 'eval'),
227 types.
func(M
, 'do_eval'));
228 x
:= env_pkg.
env_set(M
, E
, repl_env
,
229 types.
symbol(M
, '*ARGV*'),
230 types.
slice(M
, argv
, 1));
232 -- core.mal: defined using the language itself
233 line
:= REP('(def! not (fn* (a) (if a false true)))');
234 line
:= REP('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))');
236 IF argv.
COUNT() > 0 THEN
238 line
:= REP('(load-file "' ||
239 TREAT(M(argv(1)) AS mal_str_T
).val_str ||
241 io.
close(1); -- close output stream
243 EXCEPTION WHEN OTHERS THEN
244 io.
writeline('Error: ' || SQLERRM
);
245 io.
writeline(dbms_utility.format_error_backtrace
);
246 io.
close(1); -- close output stream
253 line
:= io.
readline('user> ', 0);
254 IF line
= EMPTY_CLOB() THEN CONTINUE; END IF;
255 IF line
IS NOT NULL THEN
256 io.
writeline(REP(line
));
259 EXCEPTION WHEN OTHERS THEN
260 IF SQLCODE = -20001 THEN -- io read stream closed
261 io.
close(1); -- close output stream
264 io.
writeline('Error: ' || SQLERRM
);
265 io.
writeline(dbms_utility.format_error_backtrace
);