eval.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Copyright (C) 2009, 2010
  3. ;;;; Free Software Foundation, Inc.
  4. ;;;;
  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.
  9. ;;;;
  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.
  14. ;;;;
  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
  18. ;;;;
  19. ;;; Commentary:
  20. ;;; Scheme eval, written in Scheme.
  21. ;;;
  22. ;;; Expressions are first expanded, by the syntax expander (i.e.
  23. ;;; psyntax), then memoized into internal forms. The evaluator itself
  24. ;;; only operates on the internal forms ("memoized expressions").
  25. ;;;
  26. ;;; Environments are represented as linked lists of the form (VAL ... .
  27. ;;; MOD). If MOD is #f, it means the environment was captured before
  28. ;;; modules were booted. If MOD is the literal value '(), we are
  29. ;;; evaluating at the top level, and so should track changes to the
  30. ;;; current module.
  31. ;;;
  32. ;;; Evaluate this in Emacs to make code indentation work right:
  33. ;;;
  34. ;;; (put 'memoized-expression-case 'scheme-indent-function 1)
  35. ;;;
  36. ;;; Code:
  37. (eval-when (compile)
  38. (define-syntax capture-env
  39. (syntax-rules ()
  40. ((_ (exp ...))
  41. (let ((env (exp ...)))
  42. (capture-env env)))
  43. ((_ env)
  44. (if (null? env)
  45. (current-module)
  46. (if (not env)
  47. ;; the and current-module checks that modules are booted,
  48. ;; and thus the-root-module is defined
  49. (and (current-module) the-root-module)
  50. env)))))
  51. ;; Fast case for procedures with fixed arities.
  52. (define-syntax make-fixed-closure
  53. (lambda (x)
  54. (define *max-static-argument-count* 8)
  55. (define (make-formals n)
  56. (map (lambda (i)
  57. (datum->syntax
  58. x
  59. (string->symbol
  60. (string (integer->char (+ (char->integer #\a) i))))))
  61. (iota n)))
  62. (syntax-case x ()
  63. ((_ eval nreq body env) (not (identifier? #'env))
  64. #'(let ((e env))
  65. (make-fixed-closure eval nreq body e)))
  66. ((_ eval nreq body env)
  67. #`(case nreq
  68. #,@(map (lambda (nreq)
  69. (let ((formals (make-formals nreq)))
  70. #`((#,nreq)
  71. (lambda (#,@formals)
  72. (eval body
  73. (cons* #,@(reverse formals) env))))))
  74. (iota *max-static-argument-count*))
  75. (else
  76. #,(let ((formals (make-formals *max-static-argument-count*)))
  77. #`(lambda (#,@formals . more)
  78. (let lp ((new-env (cons* #,@(reverse formals) env))
  79. (nreq (- nreq #,*max-static-argument-count*))
  80. (args more))
  81. (if (zero? nreq)
  82. (eval body
  83. (if (null? args)
  84. new-env
  85. (scm-error 'wrong-number-of-args
  86. "eval" "Wrong number of arguments"
  87. '() #f)))
  88. (if (null? args)
  89. (scm-error 'wrong-number-of-args
  90. "eval" "Wrong number of arguments"
  91. '() #f)
  92. (lp (cons (car args) new-env)
  93. (1- nreq)
  94. (cdr args)))))))))))))
  95. (define-syntax call
  96. (lambda (x)
  97. (define *max-static-call-count* 4)
  98. (syntax-case x ()
  99. ((_ eval proc nargs args env) (identifier? #'env)
  100. #`(case nargs
  101. #,@(map (lambda (nargs)
  102. #`((#,nargs)
  103. (proc
  104. #,@(map
  105. (lambda (n)
  106. (let lp ((n n) (args #'args))
  107. (if (zero? n)
  108. #`(eval (car #,args) env)
  109. (lp (1- n) #`(cdr #,args)))))
  110. (iota nargs)))))
  111. (iota *max-static-call-count*))
  112. (else
  113. (apply proc
  114. #,@(map
  115. (lambda (n)
  116. (let lp ((n n) (args #'args))
  117. (if (zero? n)
  118. #`(eval (car #,args) env)
  119. (lp (1- n) #`(cdr #,args)))))
  120. (iota *max-static-call-count*))
  121. (let lp ((exps #,(let lp ((n *max-static-call-count*)
  122. (args #'args))
  123. (if (zero? n)
  124. args
  125. (lp (1- n) #`(cdr #,args)))))
  126. (args '()))
  127. (if (null? exps)
  128. (reverse args)
  129. (lp (cdr exps)
  130. (cons (eval (car exps) env) args)))))))))))
  131. ;; This macro could be more straightforward if the compiler had better
  132. ;; copy propagation. As it is we do some copy propagation by hand.
  133. (define-syntax mx-bind
  134. (lambda (x)
  135. (syntax-case x ()
  136. ((_ data () body)
  137. #'body)
  138. ((_ data (a . b) body) (and (identifier? #'a) (identifier? #'b))
  139. #'(let ((a (car data))
  140. (b (cdr data)))
  141. body))
  142. ((_ data (a . b) body) (identifier? #'a)
  143. #'(let ((a (car data))
  144. (xb (cdr data)))
  145. (mx-bind xb b body)))
  146. ((_ data (a . b) body)
  147. #'(let ((xa (car data))
  148. (xb (cdr data)))
  149. (mx-bind xa a (mx-bind xb b body))))
  150. ((_ data v body) (identifier? #'v)
  151. #'(let ((v data))
  152. body)))))
  153. ;; The resulting nested if statements will be an O(n) dispatch. Once
  154. ;; we compile `case' effectively, this situation will improve.
  155. (define-syntax mx-match
  156. (lambda (x)
  157. (syntax-case x (quote)
  158. ((_ mx data tag)
  159. #'(error "what" mx))
  160. ((_ mx data tag (('type pat) body) c* ...)
  161. #`(if (eqv? tag #,(or (memoized-typecode (syntax->datum #'type))
  162. (error "not a typecode" #'type)))
  163. (mx-bind data pat body)
  164. (mx-match mx data tag c* ...))))))
  165. (define-syntax memoized-expression-case
  166. (lambda (x)
  167. (syntax-case x ()
  168. ((_ mx c ...)
  169. #'(let ((tag (memoized-expression-typecode mx))
  170. (data (memoized-expression-data mx)))
  171. (mx-match mx data tag c ...)))))))
  172. ;;;
  173. ;;; On 18 Feb 2010, I did a profile of how often the various memoized expression
  174. ;;; types occur when getting to a prompt on a fresh build. Here are the numbers
  175. ;;; I got:
  176. ;;;
  177. ;;; lexical-ref: 32933054
  178. ;;; call: 20281547
  179. ;;; toplevel-ref: 13228724
  180. ;;; if: 9156156
  181. ;;; quote: 6610137
  182. ;;; let: 2619707
  183. ;;; lambda: 1010921
  184. ;;; begin: 948945
  185. ;;; lexical-set: 509862
  186. ;;; call-with-values: 139668
  187. ;;; apply: 49402
  188. ;;; module-ref: 14468
  189. ;;; define: 1259
  190. ;;; toplevel-set: 328
  191. ;;; dynwind: 162
  192. ;;; with-fluids: 0
  193. ;;; call/cc: 0
  194. ;;; module-set: 0
  195. ;;;
  196. ;;; So until we compile `case' into a computed goto, we'll order the clauses in
  197. ;;; `eval' in this order, to put the most frequent cases first.
  198. ;;;
  199. (define primitive-eval
  200. (let ()
  201. ;; We pre-generate procedures with fixed arities, up to some number of
  202. ;; arguments; see make-fixed-closure above.
  203. ;; A unique marker for unbound keywords.
  204. (define unbound-arg (list 'unbound-arg))
  205. ;; Procedures with rest, optional, or keyword arguments, potentially with
  206. ;; multiple arities, as with case-lambda.
  207. (define (make-general-closure env body nreq rest? nopt kw inits alt)
  208. (define alt-proc
  209. (and alt
  210. (let* ((body (car alt))
  211. (nreq (cadr alt))
  212. (rest (if (null? (cddr alt)) #f (caddr alt)))
  213. (tail (and (pair? (cddr alt)) (pair? (cdddr alt)) (cdddr alt)))
  214. (nopt (if tail (car tail) 0))
  215. (kw (and tail (cadr tail)))
  216. (inits (if tail (caddr tail) '()))
  217. (alt (and tail (cadddr tail))))
  218. (make-general-closure env body nreq rest nopt kw inits alt))))
  219. (lambda %args
  220. (let lp ((env env)
  221. (nreq* nreq)
  222. (args %args))
  223. (if (> nreq* 0)
  224. ;; First, bind required arguments.
  225. (if (null? args)
  226. (if alt
  227. (apply alt-proc %args)
  228. (scm-error 'wrong-number-of-args
  229. "eval" "Wrong number of arguments"
  230. '() #f))
  231. (lp (cons (car args) env)
  232. (1- nreq*)
  233. (cdr args)))
  234. ;; Move on to optional arguments.
  235. (if (not kw)
  236. ;; Without keywords, bind optionals from arguments.
  237. (let lp ((env env)
  238. (nopt nopt)
  239. (args args)
  240. (inits inits))
  241. (if (zero? nopt)
  242. (if rest?
  243. (eval body (cons args env))
  244. (if (null? args)
  245. (eval body env)
  246. (if alt
  247. (apply alt-proc %args)
  248. (scm-error 'wrong-number-of-args
  249. "eval" "Wrong number of arguments"
  250. '() #f))))
  251. (if (null? args)
  252. (lp (cons (eval (car inits) env) env)
  253. (1- nopt) args (cdr inits))
  254. (lp (cons (car args) env)
  255. (1- nopt) (cdr args) (cdr inits)))))
  256. ;; With keywords, we stop binding optionals at the first
  257. ;; keyword.
  258. (let lp ((env env)
  259. (nopt* nopt)
  260. (args args)
  261. (inits inits))
  262. (if (> nopt* 0)
  263. (if (or (null? args) (keyword? (car args)))
  264. (lp (cons (eval (car inits) env) env)
  265. (1- nopt*) args (cdr inits))
  266. (lp (cons (car args) env)
  267. (1- nopt*) (cdr args) (cdr inits)))
  268. ;; Finished with optionals.
  269. (let* ((aok (car kw))
  270. (kw (cdr kw))
  271. (kw-base (+ nopt nreq (if rest? 1 0)))
  272. (imax (let lp ((imax (1- kw-base)) (kw kw))
  273. (if (null? kw)
  274. imax
  275. (lp (max (cdar kw) imax)
  276. (cdr kw)))))
  277. ;; Fill in kwargs with "undefined" vals.
  278. (env (let lp ((i kw-base)
  279. ;; Also, here we bind the rest
  280. ;; arg, if any.
  281. (env (if rest? (cons args env) env)))
  282. (if (<= i imax)
  283. (lp (1+ i) (cons unbound-arg env))
  284. env))))
  285. ;; Now scan args for keywords.
  286. (let lp ((args args))
  287. (if (and (pair? args) (pair? (cdr args))
  288. (keyword? (car args)))
  289. (let ((kw-pair (assq (car args) kw))
  290. (v (cadr args)))
  291. (if kw-pair
  292. ;; Found a known keyword; set its value.
  293. (list-set! env (- imax (cdr kw-pair)) v)
  294. ;; Unknown keyword.
  295. (if (not aok)
  296. (scm-error 'keyword-argument-error
  297. "eval" "Unrecognized keyword"
  298. '() #f)))
  299. (lp (cddr args)))
  300. (if (pair? args)
  301. (if rest?
  302. ;; Be lenient parsing rest args.
  303. (lp (cdr args))
  304. (scm-error 'keyword-argument-error
  305. "eval" "Invalid keyword"
  306. '() #f))
  307. ;; Finished parsing keywords. Fill in
  308. ;; uninitialized kwargs by evalling init
  309. ;; expressions in their appropriate
  310. ;; environment.
  311. (let lp ((i (- imax kw-base))
  312. (inits inits))
  313. (if (pair? inits)
  314. (let ((tail (list-tail env i)))
  315. (if (eq? (car tail) unbound-arg)
  316. (set-car! tail
  317. (eval (car inits)
  318. (cdr tail))))
  319. (lp (1- i) (cdr inits)))
  320. ;; Finally, eval the body.
  321. (eval body env))))))))))))))
  322. ;; The "engine". EXP is a memoized expression.
  323. (define (eval exp env)
  324. (memoized-expression-case exp
  325. (('lexical-ref n)
  326. (list-ref env n))
  327. (('call (f nargs . args))
  328. (let ((proc (eval f env)))
  329. (call eval proc nargs args env)))
  330. (('toplevel-ref var-or-sym)
  331. (variable-ref
  332. (if (variable? var-or-sym)
  333. var-or-sym
  334. (memoize-variable-access! exp
  335. (capture-env (if (pair? env)
  336. (cdr (last-pair env))
  337. env))))))
  338. (('if (test consequent . alternate))
  339. (if (eval test env)
  340. (eval consequent env)
  341. (eval alternate env)))
  342. (('quote x)
  343. x)
  344. (('let (inits . body))
  345. (let lp ((inits inits) (new-env (capture-env env)))
  346. (if (null? inits)
  347. (eval body new-env)
  348. (lp (cdr inits)
  349. (cons (eval (car inits) env) new-env)))))
  350. (('lambda (body nreq . tail))
  351. (if (null? tail)
  352. (make-fixed-closure eval nreq body (capture-env env))
  353. (if (null? (cdr tail))
  354. (make-general-closure (capture-env env) body nreq (car tail)
  355. 0 #f '() #f)
  356. (apply make-general-closure (capture-env env) body nreq tail))))
  357. (('begin (first . rest))
  358. (let lp ((first first) (rest rest))
  359. (if (null? rest)
  360. (eval first env)
  361. (begin
  362. (eval first env)
  363. (lp (car rest) (cdr rest))))))
  364. (('lexical-set! (n . x))
  365. (let ((val (eval x env)))
  366. (list-set! env n val)))
  367. (('call-with-values (producer . consumer))
  368. (call-with-values (eval producer env)
  369. (eval consumer env)))
  370. (('apply (f args))
  371. (apply (eval f env) (eval args env)))
  372. (('module-ref var-or-spec)
  373. (variable-ref
  374. (if (variable? var-or-spec)
  375. var-or-spec
  376. (memoize-variable-access! exp #f))))
  377. (('define (name . x))
  378. (define! name (eval x env)))
  379. (('toplevel-set! (var-or-sym . x))
  380. (variable-set!
  381. (if (variable? var-or-sym)
  382. var-or-sym
  383. (memoize-variable-access! exp
  384. (capture-env (if (pair? env)
  385. (cdr (last-pair env))
  386. env))))
  387. (eval x env)))
  388. (('dynwind (in exp . out))
  389. (dynamic-wind (eval in env)
  390. (lambda () (eval exp env))
  391. (eval out env)))
  392. (('with-fluids (fluids vals . exp))
  393. (let* ((fluids (map (lambda (x) (eval x env)) fluids))
  394. (vals (map (lambda (x) (eval x env)) vals)))
  395. (let lp ((fluids fluids) (vals vals))
  396. (if (null? fluids)
  397. (eval exp env)
  398. (with-fluids (((car fluids) (car vals)))
  399. (lp (cdr fluids) (cdr vals)))))))
  400. (('prompt (tag exp . handler))
  401. (@prompt (eval tag env)
  402. (eval exp env)
  403. (eval handler env)))
  404. (('call/cc proc)
  405. (call/cc (eval proc env)))
  406. (('module-set! (x . var-or-spec))
  407. (variable-set!
  408. (if (variable? var-or-spec)
  409. var-or-spec
  410. (memoize-variable-access! exp #f))
  411. (eval x env)))))
  412. ;; primitive-eval
  413. (lambda (exp)
  414. "Evaluate @var{exp} in the current module."
  415. (eval
  416. (memoize-expression
  417. (if (macroexpanded? exp)
  418. exp
  419. ((module-transformer (current-module)) exp)))
  420. '()))))