-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp-Cif.rkt
49 lines (42 loc) · 1.43 KB
/
interp-Cif.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
#lang racket
(require "utilities.rkt")
(require "interp-Lif.rkt")
(require "interp-Cvar.rkt")
(provide interp-Cif interp-Cif-mixin)
(define (interp-Cif-mixin super-class)
(class super-class
(super-new)
(inherit interp-exp)
(define/override (interp-stmt env)
(lambda (s)
(match s
[(Assign (Var x) e)
(cons (cons x ((interp-exp env) e)) env)]
[else ((super interp-stmt env) s)]
)))
(define/override (interp-tail env blocks)
(lambda (t)
(match t
;; Cvar cases, repeated logic but with blocks added
[(Return e)
((interp-exp env) e)]
[(Seq s t2)
(define new-env ((interp-stmt env) s))
((interp-tail new-env blocks) t2)]
;; Cif cases
[(Goto l)
((interp-tail env blocks) (dict-ref blocks l))]
[(IfStmt (Prim op arg*) (Goto thn-label) (Goto els-label))
(if ((interp-exp env) (Prim op arg*))
((interp-tail env blocks) (dict-ref blocks thn-label))
((interp-tail env blocks) (dict-ref blocks els-label)))]
)))
(define/override (interp-program p)
(match p
[(CProgram info blocks)
((interp-tail '() blocks) (dict-ref blocks 'start))]
))
))
(define (interp-Cif p)
(define Cif-class (interp-Cif-mixin (interp-Cvar-mixin interp-Lif-class)))
(send (new Cif-class) interp-program p))