-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp-Rvar.rkt
43 lines (37 loc) · 1.12 KB
/
interp-Rvar.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
#lang racket
(require racket/fixnum)
(require racket/dict)
(require "utilities.rkt")
(provide interp-Rvar interp-Rvar-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-Rvar-class
(class object%
(super-new)
(define/public ((interp-exp env) e)
(match e
[(Int n) n]
[(Prim 'read '())
(define r (read))
(cond [(fixnum? r) r]
[else (error 'interp-exp "expected an integer" r)])]
[(Prim '- (list e))
(define v ((interp-exp env) e))
(fx- 0 v)]
[(Prim '+ (list e1 e2))
(define v1 ((interp-exp env) e1))
(define v2 ((interp-exp env) e2))
(fx+ v1 v2)]
[(Var x) (dict-ref env x)]
[(Let x e body)
(define new-env (dict-set env x ((interp-exp env) e)))
((interp-exp new-env) body)]
))
(define/public (interp-program p)
(match p
[(Program '() e) ((interp-exp '()) e)]
))
))
(define (interp-Rvar p)
(send (new interp-Rvar-class) interp-program p))