Commit | Line | Data |
---|---|---|
bab98046 | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 |
b7742c6b AW |
2 | * Free Software Foundation, Inc. |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public License | |
6 | * as published by the Free Software Foundation; either version 3 of | |
7 | * the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | * 02110-1301 USA | |
18 | */ | |
19 | ||
20 | \f | |
21 | ||
22 | #ifdef HAVE_CONFIG_H | |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
b7742c6b | 26 | #include "libguile/__scm.h" |
b7742c6b AW |
27 | #include "libguile/_scm.h" |
28 | #include "libguile/continuations.h" | |
29 | #include "libguile/eq.h" | |
30 | #include "libguile/list.h" | |
31 | #include "libguile/macros.h" | |
32 | #include "libguile/memoize.h" | |
33 | #include "libguile/modules.h" | |
34 | #include "libguile/srcprop.h" | |
35 | #include "libguile/ports.h" | |
36 | #include "libguile/print.h" | |
37 | #include "libguile/strings.h" | |
38 | #include "libguile/throw.h" | |
39 | #include "libguile/validate.h" | |
40 | ||
41 | ||
42 | \f | |
43 | ||
44 | ||
45 | #if 0 | |
46 | #define CAR(x) SCM_CAR(x) | |
47 | #define CDR(x) SCM_CDR(x) | |
48 | #define CAAR(x) SCM_CAAR(x) | |
49 | #define CADR(x) SCM_CADR(x) | |
50 | #define CDAR(x) SCM_CDAR(x) | |
51 | #define CDDR(x) SCM_CDDR(x) | |
52 | #define CADDR(x) SCM_CADDR(x) | |
53 | #define CDDDR(x) SCM_CDDDR(x) | |
54 | #define CADDDR(x) SCM_CDDDR(x) | |
55 | #else | |
56 | #define CAR(x) scm_car(x) | |
57 | #define CDR(x) scm_cdr(x) | |
58 | #define CAAR(x) scm_caar(x) | |
59 | #define CADR(x) scm_cadr(x) | |
60 | #define CDAR(x) scm_cdar(x) | |
61 | #define CDDR(x) scm_cddr(x) | |
62 | #define CADDR(x) scm_caddr(x) | |
63 | #define CDDDR(x) scm_cdddr(x) | |
64 | #define CADDDR(x) scm_cadddr(x) | |
65 | #endif | |
66 | ||
67 | ||
68 | static const char s_bad_expression[] = "Bad expression"; | |
69 | static const char s_expression[] = "Missing or extra expression in"; | |
70 | static const char s_missing_expression[] = "Missing expression in"; | |
71 | static const char s_extra_expression[] = "Extra expression in"; | |
72 | static const char s_empty_combination[] = "Illegal empty combination"; | |
73 | static const char s_missing_body_expression[] = "Missing body expression in"; | |
74 | static const char s_mixed_body_forms[] = "Mixed definitions and expressions in"; | |
75 | static const char s_bad_define[] = "Bad define placement"; | |
76 | static const char s_missing_clauses[] = "Missing clauses"; | |
77 | static const char s_misplaced_else_clause[] = "Misplaced else clause"; | |
78 | static const char s_bad_case_clause[] = "Bad case clause"; | |
79 | static const char s_bad_case_labels[] = "Bad case labels"; | |
80 | static const char s_duplicate_case_label[] = "Duplicate case label"; | |
81 | static const char s_bad_cond_clause[] = "Bad cond clause"; | |
82 | static const char s_missing_recipient[] = "Missing recipient in"; | |
83 | static const char s_bad_variable[] = "Bad variable"; | |
84 | static const char s_bad_bindings[] = "Bad bindings"; | |
85 | static const char s_bad_binding[] = "Bad binding"; | |
86 | static const char s_duplicate_binding[] = "Duplicate binding"; | |
87 | static const char s_bad_exit_clause[] = "Bad exit clause"; | |
88 | static const char s_bad_formals[] = "Bad formals"; | |
89 | static const char s_bad_formal[] = "Bad formal"; | |
90 | static const char s_duplicate_formal[] = "Duplicate formal"; | |
91 | static const char s_splicing[] = "Non-list result for unquote-splicing"; | |
92 | static const char s_bad_slot_number[] = "Bad slot number"; | |
93 | ||
94 | ||
95 | /* Signal a syntax error. We distinguish between the form that caused the | |
96 | * error and the enclosing expression. The error message will print out as | |
97 | * shown in the following pattern. The file name and line number are only | |
98 | * given when they can be determined from the erroneous form or from the | |
99 | * enclosing expression. | |
100 | * | |
101 | * <filename>: In procedure memoization: | |
102 | * <filename>: In file <name>, line <nr>: <error-message> in <expression>. */ | |
103 | ||
104 | SCM_SYMBOL (syntax_error_key, "syntax-error"); | |
105 | ||
106 | /* The prototype is needed to indicate that the function does not return. */ | |
107 | static void | |
108 | syntax_error (const char* const, const SCM, const SCM) SCM_NORETURN; | |
109 | ||
110 | static void | |
111 | syntax_error (const char* const msg, const SCM form, const SCM expr) | |
112 | { | |
113 | SCM msg_string = scm_from_locale_string (msg); | |
114 | SCM filename = SCM_BOOL_F; | |
115 | SCM linenr = SCM_BOOL_F; | |
116 | const char *format; | |
117 | SCM args; | |
118 | ||
119 | if (scm_is_pair (form)) | |
120 | { | |
121 | filename = scm_source_property (form, scm_sym_filename); | |
122 | linenr = scm_source_property (form, scm_sym_line); | |
123 | } | |
124 | ||
125 | if (scm_is_false (filename) && scm_is_false (linenr) && scm_is_pair (expr)) | |
126 | { | |
127 | filename = scm_source_property (expr, scm_sym_filename); | |
128 | linenr = scm_source_property (expr, scm_sym_line); | |
129 | } | |
130 | ||
131 | if (!SCM_UNBNDP (expr)) | |
132 | { | |
133 | if (scm_is_true (filename)) | |
134 | { | |
135 | format = "In file ~S, line ~S: ~A ~S in expression ~S."; | |
136 | args = scm_list_5 (filename, linenr, msg_string, form, expr); | |
137 | } | |
138 | else if (scm_is_true (linenr)) | |
139 | { | |
140 | format = "In line ~S: ~A ~S in expression ~S."; | |
141 | args = scm_list_4 (linenr, msg_string, form, expr); | |
142 | } | |
143 | else | |
144 | { | |
145 | format = "~A ~S in expression ~S."; | |
146 | args = scm_list_3 (msg_string, form, expr); | |
147 | } | |
148 | } | |
149 | else | |
150 | { | |
151 | if (scm_is_true (filename)) | |
152 | { | |
153 | format = "In file ~S, line ~S: ~A ~S."; | |
154 | args = scm_list_4 (filename, linenr, msg_string, form); | |
155 | } | |
156 | else if (scm_is_true (linenr)) | |
157 | { | |
158 | format = "In line ~S: ~A ~S."; | |
159 | args = scm_list_3 (linenr, msg_string, form); | |
160 | } | |
161 | else | |
162 | { | |
163 | format = "~A ~S."; | |
164 | args = scm_list_2 (msg_string, form); | |
165 | } | |
166 | } | |
167 | ||
168 | scm_error (syntax_error_key, "memoization", format, args, SCM_BOOL_F); | |
169 | } | |
170 | ||
171 | ||
172 | /* Shortcut macros to simplify syntax error handling. */ | |
173 | #define ASSERT_SYNTAX(cond, message, form) \ | |
174 | { if (SCM_UNLIKELY (!(cond))) \ | |
175 | syntax_error (message, form, SCM_UNDEFINED); } | |
176 | #define ASSERT_SYNTAX_2(cond, message, form, expr) \ | |
177 | { if (SCM_UNLIKELY (!(cond))) \ | |
178 | syntax_error (message, form, expr); } | |
179 | ||
180 | \f | |
181 | ||
182 | ||
183 | /* {Evaluator memoized expressions} | |
184 | */ | |
185 | ||
186 | scm_t_bits scm_tc16_memoized; | |
187 | ||
188 | #define MAKMEMO(n, args) (scm_cell (scm_tc16_memoized | ((n) << 16), (scm_t_bits)(args))) | |
189 | ||
190 | #define MAKMEMO_BEGIN(exps) \ | |
191 | MAKMEMO (SCM_M_BEGIN, exps) | |
192 | #define MAKMEMO_IF(test, then, else_) \ | |
193 | MAKMEMO (SCM_M_IF, scm_cons (test, scm_cons (then, else_))) | |
194 | #define MAKMEMO_LAMBDA(nreq, rest, body) \ | |
195 | MAKMEMO (SCM_M_LAMBDA, scm_cons (SCM_I_MAKINUM (nreq), scm_cons (rest, body))) | |
196 | #define MAKMEMO_LET(inits, body) \ | |
197 | MAKMEMO (SCM_M_LET, scm_cons (inits, body)) | |
198 | #define MAKMEMO_QUOTE(exp) \ | |
199 | MAKMEMO (SCM_M_QUOTE, exp) | |
200 | #define MAKMEMO_DEFINE(var, val) \ | |
201 | MAKMEMO (SCM_M_DEFINE, scm_cons (var, val)) | |
d69531e2 AW |
202 | #define MAKMEMO_DYNWIND(in, expr, out) \ |
203 | MAKMEMO (SCM_M_DYNWIND, scm_cons (in, scm_cons (expr, out))) | |
b7742c6b AW |
204 | #define MAKMEMO_APPLY(exp) \ |
205 | MAKMEMO (SCM_M_APPLY, exp) | |
206 | #define MAKMEMO_CONT(proc) \ | |
207 | MAKMEMO (SCM_M_CONT, proc) | |
208 | #define MAKMEMO_CALL_WITH_VALUES(prod, cons) \ | |
209 | MAKMEMO (SCM_M_CALL_WITH_VALUES, scm_cons (prod, cons)) | |
9331f91c AW |
210 | #define MAKMEMO_CALL(proc, nargs, args) \ |
211 | MAKMEMO (SCM_M_CALL, scm_cons (proc, scm_cons (SCM_I_MAKINUM (nargs), args))) | |
b7742c6b AW |
212 | #define MAKMEMO_LEX_REF(n) \ |
213 | MAKMEMO (SCM_M_LEXICAL_REF, SCM_I_MAKINUM (n)) | |
214 | #define MAKMEMO_LEX_SET(n, val) \ | |
215 | MAKMEMO (SCM_M_LEXICAL_SET, scm_cons (SCM_I_MAKINUM (n), val)) | |
216 | #define MAKMEMO_TOP_REF(var) \ | |
217 | MAKMEMO (SCM_M_TOPLEVEL_REF, var) | |
218 | #define MAKMEMO_TOP_SET(var, val) \ | |
219 | MAKMEMO (SCM_M_TOPLEVEL_SET, scm_cons (var, val)) | |
220 | #define MAKMEMO_MOD_REF(mod, var, public) \ | |
221 | MAKMEMO (SCM_M_MODULE_REF, scm_cons (mod, scm_cons (var, public))) | |
222 | #define MAKMEMO_MOD_SET(val, mod, var, public) \ | |
223 | MAKMEMO (SCM_M_MODULE_SET, scm_cons (val, scm_cons (mod, scm_cons (var, public)))) | |
224 | ||
225 | \f | |
226 | ||
227 | /* This table must agree with the list of M_ constants in memoize.h */ | |
228 | static const char *const memoized_tags[] = | |
229 | { | |
230 | "begin", | |
231 | "if", | |
232 | "lambda", | |
233 | "let", | |
234 | "quote", | |
235 | "define", | |
d69531e2 | 236 | "dynwind", |
b7742c6b | 237 | "apply", |
3149a5b6 | 238 | "call/cc", |
b7742c6b AW |
239 | "call-with-values", |
240 | "call", | |
241 | "lexical-ref", | |
242 | "lexical-set!", | |
243 | "toplevel-ref", | |
244 | "toplevel-set!", | |
245 | "module-ref", | |
3149a5b6 | 246 | "module-set!", |
b7742c6b AW |
247 | }; |
248 | ||
249 | static int | |
250 | scm_print_memoized (SCM memoized, SCM port, scm_print_state *pstate) | |
251 | { | |
252 | scm_puts ("#<memoized ", port); | |
253 | scm_write (scm_unmemoize_expression (memoized), port); | |
254 | scm_puts (">", port); | |
255 | return 1; | |
256 | } | |
257 | ||
258 | static SCM scm_m_at (SCM xorig, SCM env); | |
259 | static SCM scm_m_atat (SCM xorig, SCM env); | |
260 | static SCM scm_m_and (SCM xorig, SCM env); | |
261 | static SCM scm_m_apply (SCM xorig, SCM env); | |
262 | static SCM scm_m_begin (SCM xorig, SCM env); | |
263 | static SCM scm_m_cont (SCM xorig, SCM env); | |
264 | static SCM scm_m_at_call_with_values (SCM xorig, SCM env); | |
265 | static SCM scm_m_cond (SCM xorig, SCM env); | |
266 | static SCM scm_m_define (SCM x, SCM env); | |
d69531e2 | 267 | static SCM scm_m_at_dynamic_wind (SCM xorig, SCM env); |
b7742c6b AW |
268 | static SCM scm_m_eval_when (SCM xorig, SCM env); |
269 | static SCM scm_m_if (SCM xorig, SCM env); | |
270 | static SCM scm_m_lambda (SCM xorig, SCM env); | |
271 | static SCM scm_m_let (SCM xorig, SCM env); | |
272 | static SCM scm_m_letrec (SCM xorig, SCM env); | |
273 | static SCM scm_m_letstar (SCM xorig, SCM env); | |
274 | static SCM scm_m_or (SCM xorig, SCM env); | |
275 | static SCM scm_m_quote (SCM xorig, SCM env); | |
276 | static SCM scm_m_set_x (SCM xorig, SCM env); | |
277 | ||
278 | ||
279 | \f | |
280 | ||
281 | ||
e809758a | 282 | static scm_t_macro_primitive |
b7742c6b AW |
283 | memoize_env_ref_transformer (SCM env, SCM x) |
284 | { | |
285 | SCM var; | |
286 | for (; scm_is_pair (env); env = CDR (env)) | |
287 | if (scm_is_eq (x, CAR (env))) | |
288 | return NULL; /* lexical */ | |
289 | ||
290 | var = scm_module_variable (env, x); | |
291 | if (scm_is_true (var) && scm_is_true (scm_variable_bound_p (var)) | |
e809758a AW |
292 | && scm_is_true (scm_macro_p (scm_variable_ref (var)))) |
293 | return scm_i_macro_primitive (scm_variable_ref (var)); | |
b7742c6b AW |
294 | else |
295 | return NULL; /* anything else */ | |
296 | } | |
297 | ||
298 | static int | |
299 | memoize_env_var_is_free (SCM env, SCM x) | |
300 | { | |
301 | for (; scm_is_pair (env); env = CDR (env)) | |
302 | if (scm_is_eq (x, CAR (env))) | |
303 | return 0; /* bound */ | |
304 | return 1; /* free */ | |
305 | } | |
306 | ||
307 | static int | |
308 | memoize_env_lexical_index (SCM env, SCM x) | |
309 | { | |
310 | int i = 0; | |
311 | for (; scm_is_pair (env); env = CDR (env), i++) | |
312 | if (scm_is_eq (x, CAR (env))) | |
313 | return i; /* bound */ | |
314 | return -1; /* free */ | |
315 | } | |
316 | ||
317 | static SCM | |
318 | memoize_env_extend (SCM env, SCM vars) | |
319 | { | |
320 | return scm_append (scm_list_2 (vars, env)); | |
321 | } | |
322 | ||
323 | static SCM | |
324 | memoize (SCM exp, SCM env) | |
325 | { | |
326 | if (scm_is_pair (exp)) | |
327 | { | |
328 | SCM CAR; | |
e809758a | 329 | scm_t_macro_primitive trans; |
b7742c6b AW |
330 | |
331 | CAR = CAR (exp); | |
332 | if (scm_is_symbol (CAR)) | |
333 | trans = memoize_env_ref_transformer (env, CAR); | |
334 | else | |
335 | trans = NULL; | |
336 | ||
337 | if (trans) | |
338 | return trans (exp, env); | |
339 | else | |
340 | { | |
9331f91c | 341 | SCM proc; |
b7742c6b | 342 | SCM args = SCM_EOL; |
9331f91c AW |
343 | int nargs = 0; |
344 | proc = memoize (CAR (exp), env); | |
345 | for (exp = CDR (exp); scm_is_pair (exp); exp = CDR (exp), nargs++) | |
b7742c6b AW |
346 | args = scm_cons (memoize (CAR (exp), env), args); |
347 | if (scm_is_null (exp)) | |
9331f91c AW |
348 | return MAKMEMO_CALL (proc, nargs, |
349 | scm_reverse_x (args, SCM_UNDEFINED)); | |
b7742c6b AW |
350 | else |
351 | syntax_error ("expected a proper list", exp, SCM_UNDEFINED); | |
352 | } | |
353 | } | |
354 | else if (scm_is_symbol (exp)) | |
355 | { | |
356 | int i = memoize_env_lexical_index (env, exp); | |
357 | if (i < 0) | |
358 | return MAKMEMO_TOP_REF (exp); | |
359 | else | |
360 | return MAKMEMO_LEX_REF (i); | |
361 | } | |
362 | else | |
363 | return MAKMEMO_QUOTE (exp); | |
364 | } | |
365 | ||
366 | static SCM | |
367 | memoize_exprs (SCM forms, const SCM env) | |
368 | { | |
369 | SCM ret = SCM_EOL; | |
370 | ||
371 | for (; !scm_is_null (forms); forms = CDR (forms)) | |
372 | ret = scm_cons (memoize (CAR (forms), env), ret); | |
373 | return scm_reverse_x (ret, SCM_UNDEFINED); | |
374 | } | |
375 | ||
376 | static SCM | |
377 | memoize_sequence (const SCM forms, const SCM env) | |
378 | { | |
379 | ASSERT_SYNTAX (scm_ilength (forms) >= 1, s_bad_expression, | |
380 | scm_cons (scm_sym_begin, forms)); | |
b5e9f3f9 AW |
381 | if (scm_is_null (CDR (forms))) |
382 | return memoize (CAR (forms), env); | |
383 | else | |
384 | return MAKMEMO_BEGIN (memoize_exprs (forms, env)); | |
b7742c6b AW |
385 | } |
386 | ||
387 | ||
388 | \f | |
389 | /* Memoization. */ | |
390 | ||
bab98046 AW |
391 | #define SCM_SYNTAX(RANAME, STR, CFN) \ |
392 | SCM_SNARF_HERE(static const char RANAME[]=STR)\ | |
e809758a | 393 | SCM_SNARF_INIT(scm_c_define (RANAME, scm_i_make_primitive_macro (RANAME, CFN))) |
bab98046 | 394 | |
bab98046 AW |
395 | SCM_SYNTAX (s_at, "@", scm_m_at); |
396 | SCM_SYNTAX (s_atat, "@@", scm_m_atat); | |
397 | SCM_SYNTAX (s_and, "and", scm_m_and); | |
398 | SCM_SYNTAX (s_begin, "begin", scm_m_begin); | |
399 | SCM_SYNTAX (s_atcall_cc, "@call-with-current-continuation", scm_m_cont); | |
400 | SCM_SYNTAX (s_at_call_with_values, "@call-with-values", scm_m_at_call_with_values); | |
401 | SCM_SYNTAX (s_cond, "cond", scm_m_cond); | |
402 | SCM_SYNTAX (s_define, "define", scm_m_define); | |
d69531e2 | 403 | SCM_SYNTAX (s_at_dynamic_wind, "@dynamic-wind", scm_m_at_dynamic_wind); |
bab98046 AW |
404 | SCM_SYNTAX (s_eval_when, "eval-when", scm_m_eval_when); |
405 | SCM_SYNTAX (s_if, "if", scm_m_if); | |
406 | SCM_SYNTAX (s_lambda, "lambda", scm_m_lambda); | |
407 | SCM_SYNTAX (s_let, "let", scm_m_let); | |
408 | SCM_SYNTAX (s_letrec, "letrec", scm_m_letrec); | |
409 | SCM_SYNTAX (s_letstar, "let*", scm_m_letstar); | |
410 | SCM_SYNTAX (s_or, "or", scm_m_or); | |
411 | SCM_SYNTAX (s_quote, "quote", scm_m_quote); | |
412 | SCM_SYNTAX (s_set_x, "set!", scm_m_set_x); | |
413 | SCM_SYNTAX (s_atapply, "@apply", scm_m_apply); | |
b7742c6b AW |
414 | |
415 | ||
416 | SCM_GLOBAL_SYMBOL (scm_sym_apply, "apply"); | |
417 | SCM_GLOBAL_SYMBOL (scm_sym_arrow, "=>"); | |
418 | SCM_GLOBAL_SYMBOL (scm_sym_at, "@"); | |
419 | SCM_GLOBAL_SYMBOL (scm_sym_atat, "@@"); | |
420 | SCM_GLOBAL_SYMBOL (scm_sym_at_call_with_values, "@call-with-values"); | |
421 | SCM_GLOBAL_SYMBOL (scm_sym_atapply, "@apply"); | |
422 | SCM_GLOBAL_SYMBOL (scm_sym_atcall_cc, "@call-with-current-continuation"); | |
423 | SCM_GLOBAL_SYMBOL (scm_sym_begin, "begin"); | |
424 | SCM_GLOBAL_SYMBOL (scm_sym_case, "case"); | |
425 | SCM_GLOBAL_SYMBOL (scm_sym_cond, "cond"); | |
426 | SCM_GLOBAL_SYMBOL (scm_sym_define, "define"); | |
d69531e2 | 427 | SCM_GLOBAL_SYMBOL (scm_sym_at_dynamic_wind, "@dynamic-wind"); |
b7742c6b AW |
428 | SCM_GLOBAL_SYMBOL (scm_sym_else, "else"); |
429 | SCM_GLOBAL_SYMBOL (scm_sym_eval_when, "eval-when"); | |
430 | SCM_GLOBAL_SYMBOL (scm_sym_if, "if"); | |
431 | SCM_GLOBAL_SYMBOL (scm_sym_lambda, "lambda"); | |
432 | SCM_GLOBAL_SYMBOL (scm_sym_let, "let"); | |
433 | SCM_GLOBAL_SYMBOL (scm_sym_letrec, "letrec"); | |
434 | SCM_GLOBAL_SYMBOL (scm_sym_letstar, "let*"); | |
435 | SCM_GLOBAL_SYMBOL (scm_sym_or, "or"); | |
436 | SCM_GLOBAL_SYMBOL (scm_sym_quote, "quote"); | |
437 | SCM_GLOBAL_SYMBOL (scm_sym_set_x, "set!"); | |
438 | SCM_SYMBOL (sym_eval, "eval"); | |
439 | SCM_SYMBOL (sym_load, "load"); | |
440 | ||
441 | SCM_GLOBAL_SYMBOL (scm_sym_unquote, "unquote"); | |
442 | SCM_GLOBAL_SYMBOL (scm_sym_quasiquote, "quasiquote"); | |
443 | SCM_GLOBAL_SYMBOL (scm_sym_uq_splicing, "unquote-splicing"); | |
444 | ||
445 | ||
446 | static SCM | |
447 | scm_m_at (SCM expr, SCM env SCM_UNUSED) | |
448 | { | |
449 | ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr); | |
450 | ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr); | |
451 | ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr); | |
452 | ||
453 | return MAKMEMO_MOD_REF (CADR (expr), CADDR (expr), SCM_BOOL_T); | |
454 | } | |
455 | ||
456 | static SCM | |
457 | scm_m_atat (SCM expr, SCM env SCM_UNUSED) | |
458 | { | |
459 | ASSERT_SYNTAX (scm_ilength (expr) == 3, s_bad_expression, expr); | |
460 | ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr); | |
461 | ASSERT_SYNTAX (scm_is_symbol (CADDR (expr)), s_bad_expression, expr); | |
462 | ||
463 | return MAKMEMO_MOD_REF (CADR (expr), CADDR (expr), SCM_BOOL_F); | |
464 | } | |
465 | ||
466 | static SCM | |
467 | scm_m_and (SCM expr, SCM env) | |
468 | { | |
469 | const SCM cdr_expr = CDR (expr); | |
470 | ||
471 | if (scm_is_null (cdr_expr)) | |
472 | return MAKMEMO_QUOTE (SCM_BOOL_T); | |
473 | ASSERT_SYNTAX (scm_is_pair (cdr_expr), s_bad_expression, expr); | |
474 | ||
475 | if (scm_is_null (CDR (cdr_expr))) | |
476 | return memoize (CAR (cdr_expr), env); | |
477 | else | |
478 | return MAKMEMO_IF (memoize (CAR (cdr_expr), env), | |
479 | scm_m_and (cdr_expr, env), | |
480 | MAKMEMO_QUOTE (SCM_BOOL_F)); | |
481 | } | |
482 | ||
483 | static SCM | |
484 | scm_m_apply (SCM expr, SCM env) | |
485 | { | |
486 | const SCM cdr_expr = CDR (expr); | |
487 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
488 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_missing_expression, expr); | |
489 | ||
490 | return MAKMEMO_APPLY (memoize_exprs (cdr_expr, env)); | |
491 | } | |
492 | ||
493 | static SCM | |
494 | scm_m_begin (SCM expr, SCM env) | |
495 | { | |
496 | const SCM cdr_expr = CDR (expr); | |
497 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 1, s_bad_expression, expr); | |
498 | return MAKMEMO_BEGIN (memoize_exprs (cdr_expr, env)); | |
499 | } | |
500 | ||
501 | static SCM | |
502 | scm_m_cont (SCM expr, SCM env) | |
503 | { | |
504 | const SCM cdr_expr = CDR (expr); | |
505 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
506 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr); | |
507 | ||
508 | return MAKMEMO_CONT (memoize (CADR (expr), env)); | |
509 | } | |
510 | ||
511 | static SCM | |
512 | scm_m_at_call_with_values (SCM expr, SCM env) | |
513 | { | |
514 | const SCM cdr_expr = CDR (expr); | |
515 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
516 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr); | |
517 | ||
518 | return MAKMEMO_CALL_WITH_VALUES (memoize (CADR (expr), env), | |
519 | memoize (CADDR (expr), env)); | |
520 | } | |
521 | ||
522 | static SCM | |
523 | scm_m_cond (SCM expr, SCM env) | |
524 | { | |
525 | /* Check, whether 'else or '=> is a literal, i. e. not bound to a value. */ | |
526 | const int else_literal_p = memoize_env_var_is_free (env, scm_sym_else); | |
527 | const int arrow_literal_p = memoize_env_var_is_free (env, scm_sym_arrow); | |
528 | ||
529 | const SCM clauses = CDR (expr); | |
530 | SCM clause_idx; | |
531 | SCM ret, loc; | |
532 | ||
533 | ASSERT_SYNTAX (scm_ilength (clauses) >= 0, s_bad_expression, expr); | |
534 | ASSERT_SYNTAX (scm_ilength (clauses) >= 1, s_missing_clauses, expr); | |
535 | ||
536 | ret = scm_cons (SCM_UNDEFINED, MAKMEMO_QUOTE (SCM_UNSPECIFIED)); | |
537 | loc = ret; | |
538 | ||
539 | for (clause_idx = clauses; | |
540 | !scm_is_null (clause_idx); | |
541 | clause_idx = CDR (clause_idx)) | |
542 | { | |
543 | SCM test; | |
544 | ||
545 | const SCM clause = CAR (clause_idx); | |
546 | const long length = scm_ilength (clause); | |
547 | ASSERT_SYNTAX_2 (length >= 1, s_bad_cond_clause, clause, expr); | |
548 | ||
549 | test = CAR (clause); | |
550 | if (scm_is_eq (test, scm_sym_else) && else_literal_p) | |
551 | { | |
552 | const int last_clause_p = scm_is_null (CDR (clause_idx)); | |
553 | ASSERT_SYNTAX_2 (length >= 2, | |
554 | s_bad_cond_clause, clause, expr); | |
555 | ASSERT_SYNTAX_2 (last_clause_p, | |
556 | s_misplaced_else_clause, clause, expr); | |
557 | SCM_SETCDR (loc, | |
558 | memoize (scm_cons (scm_sym_begin, CDR (clause)), env)); | |
559 | } | |
560 | else if (length >= 2 | |
561 | && scm_is_eq (CADR (clause), scm_sym_arrow) | |
562 | && arrow_literal_p) | |
563 | { | |
564 | SCM tmp = scm_gensym (scm_from_locale_string ("cond ")); | |
565 | SCM i; | |
566 | SCM new_env = scm_cons (tmp, env); | |
567 | ASSERT_SYNTAX_2 (length > 2, s_missing_recipient, clause, expr); | |
568 | ASSERT_SYNTAX_2 (length == 3, s_extra_expression, clause, expr); | |
569 | i = MAKMEMO_IF (MAKMEMO_LEX_REF (0), | |
570 | MAKMEMO_CALL (memoize (CADDR (clause), | |
571 | scm_cons (tmp, new_env)), | |
9331f91c | 572 | 1, |
b7742c6b AW |
573 | scm_list_1 (MAKMEMO_LEX_REF (0))), |
574 | MAKMEMO_QUOTE (SCM_UNSPECIFIED)); | |
575 | SCM_SETCDR (loc, | |
576 | MAKMEMO_LET (scm_list_1 (memoize (CAR (clause), env)), | |
577 | i)); | |
578 | env = new_env; | |
579 | loc = scm_last_pair (SCM_MEMOIZED_ARGS (i)); | |
580 | } | |
581 | /* FIXME length == 1 case */ | |
582 | else | |
583 | { | |
584 | SCM i = MAKMEMO_IF (memoize (CAR (clause), env), | |
585 | memoize (scm_cons (scm_sym_begin, CDR (clause)), env), | |
586 | MAKMEMO_QUOTE (SCM_UNSPECIFIED)); | |
587 | SCM_SETCDR (loc, i); | |
588 | loc = scm_last_pair (SCM_MEMOIZED_ARGS (i)); | |
589 | } | |
590 | } | |
591 | ||
592 | return CDR (ret); | |
593 | } | |
594 | ||
595 | /* According to Section 5.2.1 of R5RS we first have to make sure that the | |
596 | variable is bound, and then perform the `(set! variable expression)' | |
597 | operation. However, EXPRESSION _can_ be evaluated before VARIABLE is | |
598 | bound. This means that EXPRESSION won't necessarily be able to assign | |
599 | values to VARIABLE as in `(define foo (begin (set! foo 1) (+ foo 1)))'. */ | |
600 | static SCM | |
601 | scm_m_define (SCM expr, SCM env) | |
602 | { | |
603 | const SCM cdr_expr = CDR (expr); | |
604 | SCM body; | |
605 | SCM variable; | |
606 | ||
607 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
608 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr); | |
609 | ASSERT_SYNTAX (!scm_is_pair (env), s_bad_define, expr); | |
610 | ||
611 | body = CDR (cdr_expr); | |
612 | variable = CAR (cdr_expr); | |
613 | ||
614 | if (scm_is_pair (variable)) | |
615 | { | |
616 | ASSERT_SYNTAX_2 (scm_is_symbol (CAR (variable)), s_bad_variable, variable, expr); | |
617 | return MAKMEMO_DEFINE (CAR (variable), | |
618 | memoize (scm_cons (scm_sym_lambda, | |
619 | scm_cons (CDR (variable), body)), | |
620 | env)); | |
621 | } | |
622 | ASSERT_SYNTAX_2 (scm_is_symbol (variable), s_bad_variable, variable, expr); | |
623 | ASSERT_SYNTAX (scm_ilength (body) == 1, s_expression, expr); | |
624 | return MAKMEMO_DEFINE (variable, memoize (CAR (body), env)); | |
625 | } | |
626 | ||
d69531e2 AW |
627 | static SCM |
628 | scm_m_at_dynamic_wind (SCM expr, SCM env) | |
629 | { | |
630 | const SCM cdr_expr = CDR (expr); | |
631 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 3, s_bad_expression, expr); | |
632 | ||
633 | return MAKMEMO_DYNWIND (memoize (CADR (expr), env), | |
634 | memoize (CADDR (expr), env), | |
635 | memoize (CADDDR (expr), env)); | |
636 | } | |
637 | ||
b7742c6b AW |
638 | static SCM |
639 | scm_m_eval_when (SCM expr, SCM env) | |
640 | { | |
641 | ASSERT_SYNTAX (scm_ilength (expr) >= 3, s_bad_expression, expr); | |
642 | ASSERT_SYNTAX (scm_ilength (CADR (expr)) > 0, s_bad_expression, expr); | |
643 | ||
644 | if (scm_is_true (scm_memq (sym_eval, CADR (expr))) | |
645 | || scm_is_true (scm_memq (sym_load, CADR (expr)))) | |
646 | return MAKMEMO_BEGIN (memoize_exprs (CDDR (expr), env)); | |
647 | else | |
648 | return MAKMEMO_QUOTE (SCM_UNSPECIFIED); | |
649 | } | |
650 | ||
651 | static SCM | |
652 | scm_m_if (SCM expr, SCM env SCM_UNUSED) | |
653 | { | |
654 | const SCM cdr_expr = CDR (expr); | |
655 | const long length = scm_ilength (cdr_expr); | |
656 | ASSERT_SYNTAX (length == 2 || length == 3, s_expression, expr); | |
657 | return MAKMEMO_IF (memoize (CADR (expr), env), | |
658 | memoize (CADDR (expr), env), | |
659 | ((length == 3) | |
660 | ? memoize (CADDDR (expr), env) | |
661 | : MAKMEMO_QUOTE (SCM_UNSPECIFIED))); | |
662 | } | |
663 | ||
664 | /* A helper function for memoize_lambda to support checking for duplicate | |
665 | * formal arguments: Return true if OBJ is `eq?' to one of the elements of | |
666 | * LIST or to the CDR of the last cons. Therefore, LIST may have any of the | |
667 | * forms that a formal argument can have: | |
668 | * <rest>, (<arg1> ...), (<arg1> ... . <rest>) */ | |
669 | static int | |
670 | c_improper_memq (SCM obj, SCM list) | |
671 | { | |
672 | for (; scm_is_pair (list); list = CDR (list)) | |
673 | { | |
674 | if (scm_is_eq (CAR (list), obj)) | |
675 | return 1; | |
676 | } | |
677 | return scm_is_eq (list, obj); | |
678 | } | |
679 | ||
680 | static SCM | |
681 | scm_m_lambda (SCM expr, SCM env SCM_UNUSED) | |
682 | { | |
683 | SCM formals; | |
684 | SCM formals_idx; | |
685 | SCM formal_vars = SCM_EOL; | |
686 | int nreq = 0; | |
687 | ||
688 | const SCM cdr_expr = CDR (expr); | |
689 | const long length = scm_ilength (cdr_expr); | |
690 | ASSERT_SYNTAX (length >= 0, s_bad_expression, expr); | |
691 | ASSERT_SYNTAX (length >= 2, s_missing_expression, expr); | |
692 | ||
693 | /* Before iterating the list of formal arguments, make sure the formals | |
694 | * actually are given as either a symbol or a non-cyclic list. */ | |
695 | formals = CAR (cdr_expr); | |
696 | if (scm_is_pair (formals)) | |
697 | { | |
698 | /* Dirk:FIXME:: We should check for a cyclic list of formals, and if | |
699 | * detected, report a 'Bad formals' error. */ | |
700 | } | |
701 | else | |
702 | { | |
703 | ASSERT_SYNTAX_2 (scm_is_symbol (formals) || scm_is_null (formals), | |
704 | s_bad_formals, formals, expr); | |
705 | } | |
706 | ||
707 | /* Now iterate the list of formal arguments to check if all formals are | |
708 | * symbols, and that there are no duplicates. */ | |
709 | formals_idx = formals; | |
710 | while (scm_is_pair (formals_idx)) | |
711 | { | |
712 | const SCM formal = CAR (formals_idx); | |
713 | const SCM next_idx = CDR (formals_idx); | |
714 | ASSERT_SYNTAX_2 (scm_is_symbol (formal), s_bad_formal, formal, expr); | |
715 | ASSERT_SYNTAX_2 (!c_improper_memq (formal, next_idx), | |
716 | s_duplicate_formal, formal, expr); | |
717 | nreq++; | |
718 | formal_vars = scm_cons (formal, formal_vars); | |
719 | formals_idx = next_idx; | |
720 | } | |
721 | ASSERT_SYNTAX_2 (scm_is_null (formals_idx) || scm_is_symbol (formals_idx), | |
722 | s_bad_formal, formals_idx, expr); | |
723 | if (scm_is_symbol (formals_idx)) | |
724 | formal_vars = scm_cons (formals_idx, formal_vars); | |
725 | return MAKMEMO_LAMBDA (nreq, scm_symbol_p (formals_idx), | |
726 | memoize_sequence (CDDR (expr), | |
727 | memoize_env_extend (env, formal_vars))); | |
728 | } | |
729 | ||
730 | /* Check if the format of the bindings is ((<symbol> <init-form>) ...). */ | |
731 | static void | |
732 | check_bindings (const SCM bindings, const SCM expr) | |
733 | { | |
734 | SCM binding_idx; | |
735 | ||
736 | ASSERT_SYNTAX_2 (scm_ilength (bindings) >= 0, | |
737 | s_bad_bindings, bindings, expr); | |
738 | ||
739 | binding_idx = bindings; | |
740 | for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx)) | |
741 | { | |
742 | SCM name; /* const */ | |
743 | ||
744 | const SCM binding = CAR (binding_idx); | |
745 | ASSERT_SYNTAX_2 (scm_ilength (binding) == 2, | |
746 | s_bad_binding, binding, expr); | |
747 | ||
748 | name = CAR (binding); | |
749 | ASSERT_SYNTAX_2 (scm_is_symbol (name), s_bad_variable, name, expr); | |
750 | } | |
751 | } | |
752 | ||
753 | /* The bindings, which must have the format ((v1 i1) (v2 i2) ... (vn in)), are | |
754 | * transformed to the lists (vn .. v2 v1) and (i1 i2 ... in). If a duplicate | |
755 | * variable name is detected, an error is signalled. */ | |
756 | static int | |
757 | transform_bindings (const SCM bindings, const SCM expr, | |
758 | SCM *const rvarptr, SCM *const initptr) | |
759 | { | |
760 | SCM rvariables = SCM_EOL; | |
761 | SCM rinits = SCM_EOL; | |
762 | SCM binding_idx = bindings; | |
763 | int n = 0; | |
764 | for (; !scm_is_null (binding_idx); binding_idx = CDR (binding_idx)) | |
765 | { | |
766 | const SCM binding = CAR (binding_idx); | |
767 | const SCM CDR_binding = CDR (binding); | |
768 | const SCM name = CAR (binding); | |
769 | ASSERT_SYNTAX_2 (scm_is_false (scm_c_memq (name, rvariables)), | |
770 | s_duplicate_binding, name, expr); | |
771 | rvariables = scm_cons (name, rvariables); | |
772 | rinits = scm_cons (CAR (CDR_binding), rinits); | |
773 | n++; | |
774 | } | |
775 | *rvarptr = rvariables; | |
776 | *initptr = scm_reverse_x (rinits, SCM_UNDEFINED); | |
777 | return n; | |
778 | } | |
779 | ||
780 | /* This function is a helper function for memoize_let. It transforms | |
781 | * (let name ((var init) ...) body ...) into | |
782 | * ((letrec ((name (lambda (var ...) body ...))) name) init ...) | |
783 | * and memoizes the expression. It is assumed that the caller has checked | |
784 | * that name is a symbol and that there are bindings and a body. */ | |
785 | static SCM | |
786 | memoize_named_let (const SCM expr, SCM env) | |
787 | { | |
788 | SCM rvariables; | |
789 | SCM inits; | |
790 | int nreq; | |
791 | ||
792 | const SCM cdr_expr = CDR (expr); | |
793 | const SCM name = CAR (cdr_expr); | |
794 | const SCM cddr_expr = CDR (cdr_expr); | |
795 | const SCM bindings = CAR (cddr_expr); | |
796 | check_bindings (bindings, expr); | |
797 | ||
798 | nreq = transform_bindings (bindings, expr, &rvariables, &inits); | |
799 | ||
800 | env = scm_cons (name, env); | |
801 | return MAKMEMO_LET | |
802 | (scm_list_1 (MAKMEMO_QUOTE (SCM_UNDEFINED)), | |
803 | MAKMEMO_BEGIN | |
804 | (scm_list_2 (MAKMEMO_LEX_SET | |
805 | (0, | |
806 | MAKMEMO_LAMBDA | |
807 | (nreq, SCM_BOOL_F, | |
808 | memoize_sequence (CDDDR (expr), | |
809 | memoize_env_extend (env, rvariables)))), | |
810 | MAKMEMO_CALL (MAKMEMO_LEX_REF (0), | |
9331f91c | 811 | nreq, |
b7742c6b AW |
812 | memoize_exprs (inits, env))))); |
813 | } | |
814 | ||
815 | /* (let ((v1 i1) (v2 i2) ...) body) with variables v1 .. vn and initializers | |
816 | * i1 .. in is transformed to (#@let (vn ... v2 v1) (i1 i2 ...) body). */ | |
817 | static SCM | |
818 | scm_m_let (SCM expr, SCM env) | |
819 | { | |
820 | SCM bindings; | |
821 | ||
822 | const SCM cdr_expr = CDR (expr); | |
823 | const long length = scm_ilength (cdr_expr); | |
824 | ASSERT_SYNTAX (length >= 0, s_bad_expression, expr); | |
825 | ASSERT_SYNTAX (length >= 2, s_missing_expression, expr); | |
826 | ||
827 | bindings = CAR (cdr_expr); | |
828 | if (scm_is_symbol (bindings)) | |
829 | { | |
830 | ASSERT_SYNTAX (length >= 3, s_missing_expression, expr); | |
831 | return memoize_named_let (expr, env); | |
832 | } | |
833 | ||
834 | check_bindings (bindings, expr); | |
835 | if (scm_is_null (bindings)) | |
836 | return memoize_sequence (CDDR (expr), env); | |
837 | else | |
838 | { | |
839 | SCM rvariables; | |
840 | SCM inits; | |
841 | transform_bindings (bindings, expr, &rvariables, &inits); | |
842 | return MAKMEMO_LET (memoize_exprs (inits, env), | |
843 | memoize_sequence (CDDR (expr), | |
844 | memoize_env_extend (env, rvariables))); | |
845 | } | |
846 | } | |
847 | ||
848 | static SCM | |
849 | scm_m_letrec (SCM expr, SCM env) | |
850 | { | |
851 | SCM bindings; | |
852 | ||
853 | const SCM cdr_expr = CDR (expr); | |
854 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
855 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr); | |
856 | ||
857 | bindings = CAR (cdr_expr); | |
858 | if (scm_is_null (bindings)) | |
859 | return memoize_sequence (CDDR (expr), env); | |
860 | else | |
861 | { | |
862 | SCM rvariables; | |
863 | SCM inits; | |
864 | SCM v, i; | |
865 | SCM undefs = SCM_EOL; | |
866 | SCM vals = SCM_EOL; | |
867 | SCM sets = SCM_EOL; | |
868 | SCM new_env; | |
869 | int offset; | |
870 | int n = transform_bindings (bindings, expr, &rvariables, &inits); | |
871 | offset = n; | |
872 | new_env = memoize_env_extend (env, rvariables); | |
873 | for (v = scm_reverse (rvariables), i = inits; scm_is_pair (v); | |
874 | v = CDR (v), i = CDR (i), n--) | |
875 | { | |
876 | undefs = scm_cons (MAKMEMO_QUOTE (SCM_UNDEFINED), undefs); | |
877 | vals = scm_cons (memoize (CAR (i), new_env), vals); | |
878 | sets = scm_cons (MAKMEMO_LEX_SET ((n-1) + offset, | |
879 | MAKMEMO_LEX_REF (n-1)), | |
880 | sets); | |
881 | } | |
882 | return MAKMEMO_LET | |
883 | (undefs, | |
884 | MAKMEMO_BEGIN (scm_list_2 (MAKMEMO_LET (scm_reverse (vals), | |
885 | MAKMEMO_BEGIN (sets)), | |
886 | memoize_sequence (CDDR (expr), | |
887 | new_env)))); | |
888 | } | |
889 | } | |
890 | ||
891 | static SCM | |
892 | scm_m_letstar (SCM expr, SCM env SCM_UNUSED) | |
893 | { | |
894 | SCM bindings; | |
895 | ||
896 | const SCM cdr_expr = CDR (expr); | |
897 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
898 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 2, s_missing_expression, expr); | |
899 | ||
900 | bindings = CAR (cdr_expr); | |
901 | if (scm_is_null (bindings)) | |
902 | return memoize_sequence (CDDR (expr), env); | |
903 | else | |
904 | { | |
905 | SCM rvariables; | |
906 | SCM variables; | |
907 | SCM inits; | |
908 | SCM ret, loc; | |
909 | transform_bindings (bindings, expr, &rvariables, &inits); | |
910 | variables = scm_reverse (rvariables); | |
911 | ret = scm_cons (SCM_UNDEFINED, SCM_UNSPECIFIED); | |
912 | loc = ret; | |
913 | for (; scm_is_pair (variables); | |
914 | variables = CDR (variables), inits = CDR (inits)) | |
915 | { SCM x = MAKMEMO_LET (scm_list_1 (memoize (CAR (inits), env)), | |
916 | MAKMEMO_QUOTE (SCM_UNSPECIFIED)); | |
917 | SCM_SETCDR (loc, x); | |
918 | loc = scm_last_pair (SCM_MEMOIZED_ARGS (x)); | |
919 | env = scm_cons (CAR (variables), env); | |
920 | } | |
921 | SCM_SETCDR (loc, memoize_sequence (CDDR (expr), env)); | |
922 | return CDR (ret); | |
923 | } | |
924 | } | |
925 | ||
926 | static SCM | |
927 | scm_m_or (SCM expr, SCM env SCM_UNUSED) | |
928 | { | |
929 | SCM tail = CDR (expr); | |
930 | SCM ret, loc; | |
931 | const long length = scm_ilength (tail); | |
932 | ||
933 | ASSERT_SYNTAX (length >= 0, s_bad_expression, expr); | |
934 | ||
935 | ret = scm_cons (SCM_UNDEFINED, SCM_UNSPECIFIED); | |
936 | loc = ret; | |
937 | for (; scm_is_pair (tail); tail = CDR (tail)) | |
938 | { | |
939 | SCM tmp = scm_gensym (scm_from_locale_string ("cond ")); | |
940 | SCM x = MAKMEMO_IF (MAKMEMO_LEX_REF (0), | |
941 | MAKMEMO_LEX_REF (0), | |
942 | MAKMEMO_QUOTE (SCM_UNSPECIFIED)); | |
943 | SCM new_env = scm_cons (tmp, env); | |
944 | SCM_SETCDR (loc, MAKMEMO_LET (scm_list_1 (memoize (CAR (tail), | |
945 | env)), | |
946 | x)); | |
947 | env = new_env; | |
948 | loc = scm_last_pair (SCM_MEMOIZED_ARGS (x)); | |
949 | } | |
950 | SCM_SETCDR (loc, MAKMEMO_QUOTE (SCM_BOOL_F)); | |
951 | return CDR (ret); | |
952 | } | |
953 | ||
954 | static SCM | |
955 | scm_m_quote (SCM expr, SCM env SCM_UNUSED) | |
956 | { | |
957 | SCM quotee; | |
958 | ||
959 | const SCM cdr_expr = CDR (expr); | |
960 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
961 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 1, s_expression, expr); | |
962 | quotee = CAR (cdr_expr); | |
963 | return MAKMEMO_QUOTE (quotee); | |
964 | } | |
965 | ||
966 | static SCM | |
967 | scm_m_set_x (SCM expr, SCM env) | |
968 | { | |
969 | SCM variable; | |
970 | SCM vmem; | |
971 | ||
972 | const SCM cdr_expr = CDR (expr); | |
973 | ASSERT_SYNTAX (scm_ilength (cdr_expr) >= 0, s_bad_expression, expr); | |
974 | ASSERT_SYNTAX (scm_ilength (cdr_expr) == 2, s_expression, expr); | |
975 | variable = CAR (cdr_expr); | |
976 | vmem = memoize (variable, env); | |
977 | ||
978 | switch (SCM_MEMOIZED_TAG (vmem)) | |
979 | { | |
980 | case SCM_M_LEXICAL_REF: | |
981 | return MAKMEMO_LEX_SET (SCM_I_INUM (SCM_MEMOIZED_ARGS (vmem)), | |
982 | memoize (CADDR (expr), env)); | |
983 | case SCM_M_TOPLEVEL_REF: | |
984 | return MAKMEMO_TOP_SET (variable, | |
985 | memoize (CADDR (expr), env)); | |
986 | case SCM_M_MODULE_REF: | |
987 | return MAKMEMO_MOD_SET (memoize (CADDR (expr), env), | |
988 | CAR (SCM_MEMOIZED_ARGS (vmem)), | |
989 | CADR (SCM_MEMOIZED_ARGS (vmem)), | |
990 | CDDR (SCM_MEMOIZED_ARGS (vmem))); | |
991 | default: | |
992 | syntax_error (s_bad_variable, variable, expr); | |
993 | } | |
994 | } | |
995 | ||
996 | ||
997 | \f | |
998 | ||
999 | SCM_DEFINE (scm_memoize_expression, "memoize-expression", 1, 0, 0, | |
1000 | (SCM exp), | |
1001 | "Memoize the expression @var{exp}.") | |
1002 | #define FUNC_NAME s_scm_memoize_expression | |
1003 | { | |
1004 | return memoize (exp, scm_current_module ()); | |
1005 | } | |
1006 | #undef FUNC_NAME | |
1007 | ||
1008 | \f | |
1009 | ||
1010 | ||
1011 | SCM_SYMBOL (sym_placeholder, "_"); | |
1012 | ||
1013 | static SCM unmemoize (SCM expr); | |
1014 | ||
1015 | static SCM | |
1016 | unmemoize_exprs (SCM exprs) | |
1017 | { | |
1018 | SCM ret, tail; | |
1019 | if (scm_is_null (exprs)) | |
1020 | return SCM_EOL; | |
1021 | ret = scm_list_1 (unmemoize (CAR (exprs))); | |
1022 | tail = ret; | |
1023 | for (exprs = CDR (exprs); !scm_is_null (exprs); exprs = CDR (exprs)) | |
1024 | { | |
1025 | SCM_SETCDR (tail, scm_list_1 (unmemoize (CAR (exprs)))); | |
1026 | tail = CDR (tail); | |
1027 | } | |
1028 | return ret; | |
1029 | } | |
1030 | ||
1031 | static SCM | |
1032 | unmemoize_bindings (SCM inits) | |
1033 | { | |
1034 | SCM ret, tail; | |
1035 | if (scm_is_null (inits)) | |
1036 | return SCM_EOL; | |
1037 | ret = scm_list_1 (scm_list_2 (sym_placeholder, unmemoize (CAR (inits)))); | |
1038 | tail = ret; | |
1039 | for (inits = CDR (inits); !scm_is_null (inits); inits = CDR (inits)) | |
1040 | { | |
1041 | SCM_SETCDR (tail, scm_list_1 (scm_list_2 (sym_placeholder, | |
1042 | unmemoize (CAR (inits))))); | |
1043 | tail = CDR (tail); | |
1044 | } | |
1045 | return ret; | |
1046 | } | |
1047 | ||
1048 | static SCM | |
1049 | unmemoize_lexical (SCM n) | |
1050 | { | |
1051 | char buf[16]; | |
1052 | buf[15] = 0; | |
1053 | snprintf (buf, 15, "<%u>", scm_to_uint32 (n)); | |
1054 | return scm_from_locale_symbol (buf); | |
1055 | } | |
1056 | ||
1057 | static SCM | |
1058 | unmemoize (const SCM expr) | |
1059 | { | |
1060 | SCM args; | |
1061 | ||
1062 | if (!SCM_MEMOIZED_P (expr)) | |
1063 | abort (); | |
1064 | ||
1065 | args = SCM_MEMOIZED_ARGS (expr); | |
1066 | switch (SCM_MEMOIZED_TAG (expr)) | |
1067 | { | |
1068 | case SCM_M_APPLY: | |
1069 | return scm_cons (scm_sym_atapply, unmemoize_exprs (args)); | |
1070 | case SCM_M_BEGIN: | |
1071 | return scm_cons (scm_sym_begin, unmemoize_exprs (args)); | |
1072 | case SCM_M_CALL: | |
9331f91c | 1073 | return scm_cons (unmemoize (CAR (args)), unmemoize_exprs (CDDR (args))); |
b7742c6b AW |
1074 | case SCM_M_CONT: |
1075 | return scm_list_2 (scm_sym_atcall_cc, unmemoize (args)); | |
1076 | case SCM_M_CALL_WITH_VALUES: | |
1077 | return scm_list_3 (scm_sym_at_call_with_values, | |
1078 | unmemoize (CAR (args)), unmemoize (CDR (args))); | |
1079 | case SCM_M_DEFINE: | |
1080 | return scm_list_3 (scm_sym_define, CAR (args), unmemoize (CDR (args))); | |
d69531e2 AW |
1081 | case SCM_M_DYNWIND: |
1082 | return scm_list_4 (scm_sym_at_dynamic_wind, | |
1083 | unmemoize (CAR (args)), | |
1084 | unmemoize (CADR (args)), | |
1085 | unmemoize (CDDR (args))); | |
b7742c6b AW |
1086 | case SCM_M_IF: |
1087 | return scm_list_4 (scm_sym_if, unmemoize (scm_car (args)), | |
1088 | unmemoize (scm_cadr (args)), unmemoize (scm_cddr (args))); | |
1089 | case SCM_M_LAMBDA: | |
1090 | return scm_list_3 (scm_sym_lambda, | |
1091 | scm_make_list (CAR (args), sym_placeholder), | |
1092 | unmemoize (CDDR (args))); | |
1093 | case SCM_M_LET: | |
1094 | return scm_list_3 (scm_sym_let, | |
1095 | unmemoize_bindings (CAR (args)), | |
1096 | unmemoize (CDR (args))); | |
1097 | case SCM_M_QUOTE: | |
1098 | return scm_list_2 (scm_sym_quote, args); | |
1099 | case SCM_M_LEXICAL_REF: | |
1100 | return unmemoize_lexical (args); | |
1101 | case SCM_M_LEXICAL_SET: | |
1102 | return scm_list_3 (scm_sym_set_x, unmemoize_lexical (CAR (args)), | |
1103 | unmemoize (CDR (args))); | |
1104 | case SCM_M_TOPLEVEL_REF: | |
1105 | return args; | |
1106 | case SCM_M_TOPLEVEL_SET: | |
1107 | return scm_list_3 (scm_sym_set_x, CAR (args), unmemoize (CDR (args))); | |
1108 | case SCM_M_MODULE_REF: | |
1109 | return scm_list_3 (scm_is_true (CDDR (args)) ? scm_sym_at : scm_sym_atat, | |
1110 | scm_i_finite_list_copy (CAR (args)), | |
1111 | CADR (args)); | |
1112 | case SCM_M_MODULE_SET: | |
1113 | return scm_list_3 (scm_sym_set_x, | |
1114 | scm_list_3 (scm_is_true (CDDDR (args)) | |
1115 | ? scm_sym_at : scm_sym_atat, | |
1116 | scm_i_finite_list_copy (CADR (args)), | |
1117 | CADDR (args)), | |
1118 | unmemoize (CAR (args))); | |
1119 | default: | |
1120 | abort (); | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | SCM_DEFINE (scm_memoized_p, "memoized?", 1, 0, 0, | |
1125 | (SCM obj), | |
1126 | "Return @code{#t} if @var{obj} is memoized.") | |
1127 | #define FUNC_NAME s_scm_memoized_p | |
1128 | { | |
1129 | return scm_from_bool (SCM_MEMOIZED_P (obj)); | |
1130 | } | |
1131 | #undef FUNC_NAME | |
1132 | ||
1133 | SCM_DEFINE (scm_unmemoize_expression, "unmemoize-expression", 1, 0, 0, | |
1134 | (SCM m), | |
1135 | "Unmemoize the memoized expression @var{m}.") | |
1136 | #define FUNC_NAME s_scm_unmemoize_expression | |
1137 | { | |
1138 | SCM_VALIDATE_MEMOIZED (1, m); | |
1139 | return unmemoize (m); | |
1140 | } | |
1141 | #undef FUNC_NAME | |
1142 | ||
3149a5b6 AW |
1143 | SCM_DEFINE (scm_memoized_expression_typecode, "memoized-expression-typecode", 1, 0, 0, |
1144 | (SCM m), | |
1145 | "Return the typecode from the memoized expression @var{m}.") | |
1146 | #define FUNC_NAME s_scm_memoized_expression_typecode | |
1147 | { | |
1148 | SCM_VALIDATE_MEMOIZED (1, m); | |
1149 | return scm_from_uint16 (SCM_MEMOIZED_TAG (m)); | |
1150 | } | |
1151 | #undef FUNC_NAME | |
1152 | ||
1153 | SCM_DEFINE (scm_memoized_expression_data, "memoized-expression-data", 1, 0, 0, | |
1154 | (SCM m), | |
1155 | "Return the data from the memoized expression @var{m}.") | |
1156 | #define FUNC_NAME s_scm_memoized_expression_data | |
1157 | { | |
1158 | SCM_VALIDATE_MEMOIZED (1, m); | |
1159 | return SCM_MEMOIZED_ARGS (m); | |
1160 | } | |
1161 | #undef FUNC_NAME | |
1162 | ||
1163 | SCM_DEFINE (scm_memoized_typecode, "memoized-typecode", 1, 0, 0, | |
1164 | (SCM sym), | |
1165 | "Return the memoized typecode corresponding to the symbol @var{sym}.") | |
1166 | #define FUNC_NAME s_scm_memoized_typecode | |
1167 | { | |
1168 | int i; | |
1169 | ||
1170 | SCM_VALIDATE_SYMBOL (1, sym); | |
1171 | ||
1172 | for (i = 0; i < sizeof(memoized_tags)/sizeof(const char*); i++) | |
1173 | if (strcmp (scm_i_symbol_chars (sym), memoized_tags[i]) == 0) | |
1174 | return scm_from_int32 (i); | |
1175 | ||
1176 | return SCM_BOOL_F; | |
1177 | } | |
1178 | #undef FUNC_NAME | |
1179 | ||
1180 | SCM_SYMBOL (scm_unbound_variable_key, "unbound-variable"); | |
1181 | static void error_unbound_variable (SCM symbol) SCM_NORETURN; | |
1182 | static void error_unbound_variable (SCM symbol) | |
1183 | { | |
1184 | scm_error (scm_unbound_variable_key, NULL, "Unbound variable: ~S", | |
1185 | scm_list_1 (symbol), SCM_BOOL_F); | |
1186 | } | |
1187 | ||
1188 | SCM_DEFINE (scm_memoize_variable_access_x, "memoize-variable-access!", 2, 0, 0, | |
1189 | (SCM m, SCM mod), | |
1190 | "Look up and cache the variable that @var{m} will access, returning the variable.") | |
7230aaf9 | 1191 | #define FUNC_NAME s_scm_memoize_variable_access_x |
3149a5b6 AW |
1192 | { |
1193 | SCM mx; | |
1194 | SCM_VALIDATE_MEMOIZED (1, m); | |
1195 | mx = SCM_MEMOIZED_ARGS (m); | |
1196 | switch (SCM_MEMOIZED_TAG (m)) | |
1197 | { | |
1198 | case SCM_M_TOPLEVEL_REF: | |
1199 | if (SCM_VARIABLEP (mx)) | |
1200 | return mx; | |
1201 | else | |
1202 | { | |
1203 | SCM var = scm_module_variable (mod, mx); | |
1204 | if (scm_is_false (var) || scm_is_false (scm_variable_bound_p (var))) | |
1205 | error_unbound_variable (mx); | |
1206 | SCM_SET_SMOB_OBJECT (m, var); | |
1207 | return var; | |
1208 | } | |
1209 | ||
1210 | case SCM_M_TOPLEVEL_SET: | |
1211 | { | |
1212 | SCM var = CAR (mx); | |
1213 | if (SCM_VARIABLEP (var)) | |
1214 | return var; | |
1215 | else | |
1216 | { | |
1217 | var = scm_module_variable (mod, var); | |
1218 | if (scm_is_false (var)) | |
1219 | error_unbound_variable (CAR (mx)); | |
1220 | SCM_SETCAR (mx, var); | |
1221 | return var; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | case SCM_M_MODULE_REF: | |
1226 | if (SCM_VARIABLEP (mx)) | |
1227 | return mx; | |
1228 | else | |
1229 | { | |
1230 | SCM var; | |
1231 | mod = scm_resolve_module (CAR (mx)); | |
1232 | if (scm_is_true (CDDR (mx))) | |
1233 | mod = scm_module_public_interface (mod); | |
1234 | var = scm_module_lookup (mod, CADR (mx)); | |
1235 | if (scm_is_false (scm_variable_bound_p (var))) | |
1236 | error_unbound_variable (CADR (mx)); | |
1237 | SCM_SET_SMOB_OBJECT (m, var); | |
1238 | return var; | |
1239 | } | |
1240 | ||
1241 | case SCM_M_MODULE_SET: | |
1242 | /* FIXME: not quite threadsafe */ | |
1243 | if (SCM_VARIABLEP (CDR (mx))) | |
1244 | return CDR (mx); | |
1245 | else | |
1246 | { | |
1247 | SCM var; | |
1248 | mod = scm_resolve_module (CADR (mx)); | |
1249 | if (scm_is_true (CDDDR (mx))) | |
1250 | mod = scm_module_public_interface (mod); | |
1251 | var = scm_module_lookup (mod, CADDR (mx)); | |
1252 | SCM_SETCDR (mx, var); | |
1253 | return var; | |
1254 | } | |
1255 | ||
1256 | default: | |
1257 | scm_wrong_type_arg (FUNC_NAME, 1, m); | |
1258 | return SCM_BOOL_F; | |
1259 | } | |
1260 | } | |
1261 | #undef FUNC_NAME | |
1262 | ||
b7742c6b AW |
1263 | |
1264 | \f | |
1265 | ||
1266 | void | |
1267 | scm_init_memoize () | |
1268 | { | |
1269 | scm_tc16_memoized = scm_make_smob_type ("%memoized", 0); | |
1270 | scm_set_smob_mark (scm_tc16_memoized, scm_markcdr); | |
1271 | scm_set_smob_print (scm_tc16_memoized, scm_print_memoized); | |
1272 | ||
1273 | #include "libguile/memoize.x" | |
1274 | } | |
1275 | ||
1276 | /* | |
1277 | Local Variables: | |
1278 | c-file-style: "gnu" | |
1279 | End: | |
1280 | */ |