-
Notifications
You must be signed in to change notification settings - Fork 1
/
initial-state.rkt
95 lines (88 loc) · 3.67 KB
/
initial-state.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
92
93
94
95
#lang typed/racket/base
(require
racket/match
"binding.rkt"
"core-lang.rkt"
"scanner.rkt"
"env.rkt"
)
(provide
;; -> (values initial-eval-env initial-expand-env initial-state)
make-default-initial-state
)
(: extend-envs
(-> (Listof (List Symbol Any)) AstEnv Env (Values AstEnv Env)))
(define (extend-envs src-bindings eval-env expand-env)
(for/fold ((#{eval-env : AstEnv} eval-env)
(#{expand-env : Env} expand-env))
((#{src-binding : (List Symbol Any)} src-bindings))
(match src-binding
((list name val)
(values
(cons (list (Var name) (scan val)) eval-env)
(Env-set expand-env name (VarBinding (Stx (Sym name) (EmptyCtx)))))))))
(: make-initial-state
(-> #:src-bindings (Listof (List Symbol Any))
#:t-src-bindings (Listof (List Symbol Any))
#:expand Transform
#:quote Transform
#:syntax Transform
#:lambda Transform
#:let-syntax Transform
(Values AstEnv Env CompState)))
(define (make-initial-state
;; Initial bindings available during evaluation:
#:src-bindings src-bindings
;; More bindings only available during the application of a
;; macro transformer:
#:t-src-bindings t-src-bindings
#:expand expand-transform
#:quote quote-transform
#:syntax syntax-transform
#:lambda lambda-transform
#:let-syntax let-syntax-transform)
(define core-expand-env : Env
(for/fold ((env (empty-Env)))
((entry : (List Symbol CompileTimeBinding)
(list (list 'lambda (TransformBinding lambda-transform))
(list 'quote (TransformBinding quote-transform))
(list 'syntax (TransformBinding syntax-transform))
(list 'let-syntax (TransformBinding let-syntax-transform)))))
(match entry ((list name binding)
(Env-set env name binding)))))
(define-values (eval-env expand-env)
(extend-envs src-bindings '() core-expand-env))
(define-values (t-eval-env t-expand-env)
(extend-envs t-src-bindings eval-env expand-env))
(values eval-env
expand-env
(CompState 0 (empty-BindingTable) t-eval-env t-expand-env expand-transform)))
;; NOTE: expand and the transformers are passed in to break a cycle
;; with the eval.rkt tests (which don't use an expander anyway):
(: make-default-initial-state
(-> #:expand Transform
#:quote Transform
#:syntax Transform
#:lambda Transform
#:let-syntax Transform
(Values AstEnv Env CompState)))
(define (make-default-initial-state #:expand expand-transform
#:quote quote-transform
#:syntax syntax-transform
#:lambda lambda-transform
#:let-syntax let-syntax-transform)
(make-initial-state #:src-bindings '((cons #%cons)
(car #%car)
(cdr #%cdr)
(list-ref #%list-ref)
(list #%list)
(stx-e #%stx-e)
(mk-stx #%mk-stx)
(+ #%+))
#:t-src-bindings '((lvalue #%lvalue)
(lexpand #%lexpand))
#:expand expand-transform
#:quote quote-transform
#:syntax syntax-transform
#:lambda lambda-transform
#:let-syntax let-syntax-transform))