-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp-Lfun.rkt
65 lines (57 loc) · 2.17 KB
/
interp-Lfun.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#lang racket
(require racket/fixnum)
(require "utilities.rkt")
(require "interp-Lvecof.rkt")
(provide interp-Lfun interp-Lfun-class)
;; Note to maintainers of this code:
;; A copy of this interpreter is in the book and should be
;; kept in sync with this code.
(define interp-Lfun-class
(class interp-Lvecof-class
(super-new)
(define/public (apply-fun fun-val arg-vals e)
(match fun-val
[(Function xs body fun-env)
(define params-args (for/list ([x xs] [arg arg-vals])
(cons x (box arg))))
(define new-env (append params-args fun-env))
((interp-exp new-env) body)]
[else (error 'interp-exp "expected function, not ~a\nin ~v"
fun-val e)]))
(define/override ((interp-exp env) e)
(define recur (interp-exp env))
(verbose "Lfun/interp-exp" e)
(match e
[(Apply fun args)
(define fun-val (recur fun))
(define arg-vals (for/list ([e args]) (recur e)))
(apply-fun fun-val arg-vals e)]
[else ((super interp-exp env) e)]))
(define/public (interp-def d)
(match d
[(Def f (list `[,xs : ,ps] ...) rt _ body)
(cons f (box (Function xs body '())))]
))
(define/override (interp-program p)
(verbose "interp-Lfun" p)
(match p
[(ProgramDefsExp info ds body)
(let ([top-level (for/list ([d ds]) (interp-def d))])
(for/list ([f (in-dict-values top-level)])
(set-box! f (match (unbox f)
[(Function xs body '())
(Function xs body top-level)])))
((interp-exp top-level) body))]
;; For after the shrink pass.
[(ProgramDefs info ds)
(define top-level (for/list ([d ds]) (interp-def d)))
(for ([f (in-dict-values top-level)])
(set-box! f (match (unbox f)
[(Function xs body '())
(Function xs body top-level)])))
;; call the main function
((interp-exp top-level) (Apply (Var 'main) '()))]
))
))
(define (interp-Lfun p)
(send (new interp-Lfun-class) interp-program p))