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