-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzlang.s0
158 lines (147 loc) · 3.38 KB
/
zlang.s0
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(define z-int-code (desugar '(
(define (z-eval term inames ivals)
(if
(call is-z-term? term)
(call z-interp (call z-subst term inames ivals))
(op error 'z-eval '"Not a valid Z term ~s" term)))
(define (is-z-term? term)
(if
(op atom? term)
#t
(slet
(toplevel (tag term))
(if
(op equal? toplevel '++)
(call is-++? term)
(if
(op equal? toplevel 'i-t-e)
(call is-ite? term)
#f)))))
(define (is-++? t)
(if
(op null? (tl t))
#f
(slet
(t_1 (e1 t))
(if
(op null? (tl (tl t)))
#f
(slet
(t_2 (e2 t))
(if
(op null? (tl (tl (tl t))))
(if
(call is-z-term? t_1)
(call is-z-term? t_2)
#f)
#f))))))
(define (is-ite? t)
(if
(op null? (tl t))
#f
(slet
(t_1 (e1 t))
(if
(op null? (tl (tl t)))
#f
(slet
(t_2 (e2 t))
(if
(op null? (tl (tl (tl t))))
#f
(slet
(t_3 (e3 t))
(if
(op null? (tl (tl (tl (tl t)))))
(if
(call is-z-term? t_1)
(if
(call is-z-term? t_2)
(call is-z-term? t_3)
#f)
#f)
#f))))))))
(define (z-subst t inames ivals)
(if
(op atom? t)
(if
(op number? t)
t
(call z-lookup t inames ivals))
(slet
(toplevel (tag t))
(if
(op equal? toplevel '++)
(list
'++
(call z-subst (e1 t) inames ivals)
(call z-subst (e2 t) inames ivals))
(if
(op equal? toplevel 'i-t-e)
(list
'i-t-e
(call z-subst (e1 t) inames ivals)
(call z-subst (e2 t) inames ivals)
(call z-subst (e3 t) inames ivals))
(op error 'z-step '"Should not be reachable ~s" toplevel))))))
(define (z-lookup x inames ivals)
(if
(op null? inames)
(op error 'z-lookup '"No value found for" x)
(if
(op equal? x (hd inames))
(if
(op number? (hd ivals))
(hd ivals)
(op error 'z-lookup '"Not a valid value for" (list x (hd ivals))))
(call z-lookup x (tl inames) (tl ivals)))))
(define (z-interp t)
(if
(op number? t)
t
(call z-interp (call z-step t))))
(define (z-step t)
(if
(op atom? t)
(if
(op number? t)
t
(op error 'z-step '"Should not happen" t))
(slet
(toplevel (tag t))
(if
(op equal? toplevel '++)
(call handle++ t)
(if
(op equal? toplevel 'i-t-e)
(call handle-ite t)
(op error 'z-step '"Should not happen" toplevel))))))
(define (handle++ t)
(slet
(t_1 (e1 t))
(slet
(t_2 (e2 t))
(if
(op number? t_1)
(if
(op number? t_2)
(op + t_1 t_2)
(list '++ t_1 (call z-step t_2)))
(list '++ (call z-step t_1) t_2))
(op error 'handle++ '"Should not happen" t))))
(define (handle-ite t)
(slet
(t_1 (e1 t))
(slet
(t_2 (e2 t))
(slet
(t_3 (e3 t))
(if
(op null? (tl (tl (tl (tl t)))))
(if
(op number? t_1)
(if (op equal? t_1 '0) t_3 t_2)
(list 'i-t-e (call z-step t_1) t_2 t_3))
(op error '"Should not happen" t))))))
)))
(make 'z-int z-int-code)