-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp-Cfun.rkt
91 lines (82 loc) · 3.19 KB
/
interp-Cfun.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#lang racket
(require "utilities.rkt")
(require "interp-Lfun-prime.rkt")
(require "interp-Cvar.rkt")
(require "interp-Cif.rkt")
(require "interp-Cwhile.rkt")
(require "interp-Cvec.rkt")
(require "interp-Cvecof.rkt")
(require (prefix-in runtime-config: "runtime-config.rkt"))
(provide interp-Cfun interp-Cfun-mixin)
(define (interp-Cfun-mixin super-class)
(class super-class
(super-new)
(inherit initialize!)
(define/override (interp-stmt env)
(lambda (s)
(match s
[(Call e es)
(define f-val ((interp-exp env) e))
(define arg-vals (map (interp-exp env) es))
(call-function f-val arg-vals s)
env]
#;[(Assign (Var x) e)
(dict-set env x (box ((interp-exp env) e)))]
[else ((super interp-stmt env) s)]
)))
(define/public (call-function fun arg-vals ast)
(match fun
[(CFunction xs info blocks def-env)
(define f (dict-ref info 'name))
(define f-start (symbol-append f '_start))
(define params-args (for/list ([x xs] [arg arg-vals])
(cons x (box arg))))
(define new-env (append params-args def-env))
((interp-tail new-env blocks) (dict-ref blocks f-start))]
[else (error 'interp-exp "expected C function, not ~a\nin ~v" fun ast)]))
(define/override ((interp-exp env) ast)
(define result
(match ast
[(Call f args)
(define f-val ((interp-exp env) f))
(define arg-vals (map (interp-exp env) args))
(call-function f-val arg-vals ast)]
[else ((super interp-exp env) ast)]))
(verbose 'interp-exp ast result)
result)
(define/override ((interp-tail env blocks) ast)
(match ast
[(TailCall f args)
(define arg-vals (map (interp-exp env) args))
(define f-val ((interp-exp env) f))
(call-function f-val arg-vals ast)]
[else ((super interp-tail env blocks) ast)]))
(define/override (interp-def ast)
(match ast
[(Def f `([,xs : ,ps] ...) rt info blocks)
(cons f (box (CFunction xs `((name . ,f)) blocks '())))]
[else (error 'interp-def "unhandled" ast)]
))
(define/override (interp-program ast)
(match ast
[(ProgramDefs info ds)
((initialize!) runtime-config:rootstack-size
runtime-config:heap-size)
(define top-level (for/list ([d ds]) (interp-def d)))
(for/list ([f (in-dict-values top-level)])
(set-box! f (match (unbox f)
[(CFunction xs info blocks '())
(CFunction xs info blocks top-level)])))
((interp-tail top-level '()) (TailCall (Var 'main) '()))]
[else (error 'interp-program "unhandled ~a" ast)]
))
))
(define (interp-Cfun p)
(define Cfun-class (interp-Cfun-mixin
(interp-Cvecof-mixin
(interp-Cvec-mixin
(interp-Cwhile-mixin
(interp-Cif-mixin
(interp-Cvar-mixin
interp-Lfun-prime-class)))))))
(send (new Cfun-class) interp-program p))