1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ;;; Scheme eval, written in Scheme.
26 ;;; Expressions are first expanded, by the syntax expander (i.e.
27 ;;; psyntax), then memoized into internal forms. The evaluator itself
28 ;;; only operates on the internal forms ("memoized expressions").
30 ;;; Environments are represented as linked lists of the form (VAL ... .
31 ;;; MOD). If MOD is #f, it means the environment was captured before
32 ;;; modules were booted. If MOD is the literal value '(), we are
33 ;;; evaluating at the top level, and so should track changes to the
36 ;;; Evaluate this in Emacs to make code indentation work right:
38 ;;; (put 'memoized-expression-case 'scheme-indent-function 1)
46 (define-syntax capture-env
49 (let ((env (exp ...)))
55 ;; the and current-module checks that modules are booted,
56 ;; and thus the-root-module is defined
57 (and (current-module) the-root-module)
60 ;; Fast case for procedures with fixed arities.
61 (define-syntax make-fixed-closure
63 (define *max-static-argument-count* 8)
64 (define (make-formals n)
69 (string (integer->char (+ (char->integer #\a) i))))))
72 ((_ eval nreq body env) (not (identifier? #'env))
74 (make-fixed-closure eval nreq body e)))
75 ((_ eval nreq body env)
77 #,@(map (lambda (nreq)
78 (let ((formals (make-formals nreq)))
82 (cons* #,@(reverse formals) env))))))
83 (iota *max-static-argument-count*))
85 #,(let ((formals (make-formals *max-static-argument-count*)))
86 #`(lambda (#,@formals . more)
87 (let lp ((new-env (cons* #,@(reverse formals) env))
88 (nreq (- nreq #,*max-static-argument-count*))
94 (scm-error 'wrong-number-of-args
95 "eval" "Wrong number of arguments"
98 (scm-error 'wrong-number-of-args
99 "eval" "Wrong number of arguments"
101 (lp (cons (car args) new-env)
103 (cdr args)))))))))))))
107 (define *max-static-call-count* 4)
109 ((_ eval proc nargs args env) (identifier? #'env)
111 #,@(map (lambda (nargs)
116 (let lp ((n n) (args #'args))
118 #`(eval (car #,args) env)
119 (lp (1- n) #`(cdr #,args)))))
121 (iota *max-static-call-count*))
126 (let lp ((n n) (args #'args))
128 #`(eval (car #,args) env)
129 (lp (1- n) #`(cdr #,args)))))
130 (iota *max-static-call-count*))
131 (let lp ((exps #,(let lp ((n *max-static-call-count*)
135 (lp (1- n) #`(cdr #,args)))))
140 (cons (eval (car exps) env) args)))))))))))
142 ;; This macro could be more straightforward if the compiler had better
143 ;; copy propagation. As it is we do some copy propagation by hand.
144 (define-syntax mx-bind
149 ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
150 #'(let ((a (car data))
153 ((_ data (a . b) body) (identifier? #'a)
154 #'(let ((a (car data))
156 (mx-bind xb b body)))
157 ((_ data (a . b) body)
158 #'(let ((xa (car data))
160 (mx-bind xa a (mx-bind xb b body))))
161 ((_ data v body) (identifier? #'v)
165 ;; The resulting nested if statements will be an O(n) dispatch. Once
166 ;; we compile `case' effectively, this situation will improve.
167 (define-syntax mx-match
169 (syntax-case x (quote)
172 ((_ mx data tag (('type pat) body) c* ...)
173 #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
174 (error "not a typecode" #'type)))
175 (mx-bind data pat body)
176 (mx-match mx data tag c* ...))))))
178 (define-syntax memoized-expression-case
182 #'(let ((tag (memoized-expression-typecode mx))
183 (data (memoized-expression-data mx)))
184 (mx-match mx data tag c ...)))))))
188 ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
189 ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
192 ;;; lexical-ref: 32933054
194 ;;; toplevel-ref: 13228724
200 ;;; lexical-set: 509862
201 ;;; call-with-values: 139668
203 ;;; module-ref: 14468
205 ;;; toplevel-set: 328
211 ;;; So until we compile `case' into a computed goto, we'll order the clauses in
212 ;;; `eval' in this order, to put the most frequent cases first.
215 (define primitive-eval
217 ;; We pre-generate procedures with fixed arities, up to some number of
218 ;; arguments; see make-fixed-closure above.
220 ;; A unique marker for unbound keywords.
221 (define unbound-arg (list 'unbound-arg))
223 ;; Procedures with rest, optional, or keyword arguments, potentially with
224 ;; multiple arities, as with case-lambda.
225 (define (make-general-closure env body nreq rest? nopt kw inits alt)
227 (and alt ; (body docstring nreq ...)
228 (let* ((body (car alt))
231 (rest (if (null? (cdr spec)) #f (cadr spec)))
232 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
233 (nopt (if tail (car tail) 0))
234 (kw (and tail (cadr tail)))
235 (inits (if tail (caddr tail) '()))
236 (alt (and tail (cadddr tail))))
237 (make-general-closure env body nreq rest nopt kw inits alt))))
238 (define (set-procedure-arity! proc)
239 (let lp ((alt alt) (nreq nreq) (nopt nopt) (rest? rest?))
242 (set-procedure-property! proc 'arglist
248 (set-procedure-minimum-arity! proc nreq nopt rest?))
249 (let* ((spec (cddr alt))
251 (rest?* (if (null? (cdr spec)) #f (cadr spec)))
252 (tail (and (pair? (cdr spec)) (pair? (cddr spec)) (cddr spec)))
253 (nopt* (if tail (car tail) 0))
254 (alt* (and tail (cadddr tail))))
255 (if (or (< nreq* nreq)
258 (and rest?* (> nopt* nopt))
259 (or rest?* (> nopt* nopt)))))
260 (lp alt* nreq* nopt* rest?*)
261 (lp alt* nreq nopt rest?)))))
263 (set-procedure-arity!
269 ;; First, bind required arguments.
272 (apply alt-proc %args)
273 (scm-error 'wrong-number-of-args
274 "eval" "Wrong number of arguments"
276 (lp (cons (car args) env)
279 ;; Move on to optional arguments.
281 ;; Without keywords, bind optionals from arguments.
288 (eval body (cons args env))
292 (apply alt-proc %args)
293 (scm-error 'wrong-number-of-args
294 "eval" "Wrong number of arguments"
297 (lp (cons (eval (car inits) env) env)
298 (1- nopt) args (cdr inits))
299 (lp (cons (car args) env)
300 (1- nopt) (cdr args) (cdr inits)))))
306 ;; With keywords, we stop binding optionals at the
309 (if (or (null? args) (keyword? (car args)))
310 (lp (cons (eval (car inits) env) env)
311 (1- nopt*) args (cdr inits))
312 (lp (cons (car args) env)
313 (1- nopt*) (cdr args) (cdr inits))))
314 ;; Finished with optionals.
315 ((and alt (pair? args) (not (keyword? (car args)))
317 ;; Too many positional args, no #:rest arg,
318 ;; and we have an alternate.
319 (apply alt-proc %args))
321 (let* ((aok (car kw))
323 (kw-base (+ nopt nreq (if rest? 1 0)))
324 (imax (let lp ((imax (1- kw-base)) (kw kw))
327 (lp (max (cdar kw) imax)
329 ;; Fill in kwargs with "undefined" vals.
330 (env (let lp ((i kw-base)
331 ;; Also, here we bind the rest
337 (lp (1+ i) (cons unbound-arg env))
339 ;; Now scan args for keywords.
340 (let lp ((args args))
341 (if (and (pair? args) (pair? (cdr args))
342 (keyword? (car args)))
343 (let ((kw-pair (assq (car args) kw))
346 ;; Found a known keyword; set its value.
348 (- imax (cdr kw-pair)) v)
352 'keyword-argument-error
353 "eval" "Unrecognized keyword"
358 ;; Be lenient parsing rest args.
360 (scm-error 'keyword-argument-error
361 "eval" "Invalid keyword"
363 ;; Finished parsing keywords. Fill in
364 ;; uninitialized kwargs by evalling init
365 ;; expressions in their appropriate
367 (let lp ((i (- imax kw-base))
370 (let ((tail (list-tail env i)))
371 (if (eq? (car tail) unbound-arg)
375 (lp (1- i) (cdr inits)))
376 ;; Finally, eval the body.
377 (eval body env))))))))))))))))
379 ;; The "engine". EXP is a memoized expression.
380 (define (eval exp env)
381 (memoized-expression-case exp
385 (('call (f nargs . args))
386 (let ((proc (eval f env)))
387 (call eval proc nargs args env)))
389 (('toplevel-ref var-or-sym)
391 (if (variable? var-or-sym)
393 (memoize-variable-access! exp
394 (capture-env (if (pair? env)
395 (cdr (last-pair env))
398 (('if (test consequent . alternate))
400 (eval consequent env)
401 (eval alternate env)))
406 (('let (inits . body))
407 (let lp ((inits inits) (new-env (capture-env env)))
411 (cons (eval (car inits) env) new-env)))))
413 (('lambda (body docstring nreq . tail))
416 (make-fixed-closure eval nreq body (capture-env env))
417 (if (null? (cdr tail))
418 (make-general-closure (capture-env env) body
421 (apply make-general-closure (capture-env env)
424 (set-procedure-property! proc 'documentation docstring))
427 (('seq (head . tail))
432 (('lexical-set! (n . x))
433 (let ((val (eval x env)))
434 (list-set! env n val)))
436 (('call-with-values (producer . consumer))
437 (call-with-values (eval producer env)
438 (eval consumer env)))
441 (apply (eval f env) (eval args env)))
443 (('module-ref var-or-spec)
445 (if (variable? var-or-spec)
447 (memoize-variable-access! exp #f))))
449 (('define (name . x))
450 (let ((x (eval x env)))
451 (if (and (procedure? x) (not (procedure-property x 'name)))
452 (set-procedure-property! x 'name name))
456 (('toplevel-set! (var-or-sym . x))
458 (if (variable? var-or-sym)
460 (memoize-variable-access! exp
461 (capture-env (if (pair? env)
462 (cdr (last-pair env))
466 (('dynwind (in exp . out))
467 (dynamic-wind (eval in env)
468 (lambda () (eval exp env))
471 (('with-fluids (fluids vals . exp))
472 (let* ((fluids (map (lambda (x) (eval x env)) fluids))
473 (vals (map (lambda (x) (eval x env)) vals)))
474 (let lp ((fluids fluids) (vals vals))
477 (with-fluids (((car fluids) (car vals)))
478 (lp (cdr fluids) (cdr vals)))))))
480 (('prompt (tag exp . handler))
481 (@prompt (eval tag env)
486 (call/cc (eval proc env)))
488 (('module-set! (x . var-or-spec))
490 (if (variable? var-or-spec)
492 (memoize-variable-access! exp #f))
497 "Evaluate @var{exp} in the current module."
500 (if (macroexpanded? exp)
502 ((module-transformer (current-module)) exp)))