-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.go
346 lines (311 loc) · 8.01 KB
/
gen.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"errors"
"fmt"
)
func (p *Procedure) Gen(v Value) error {
switch v.(type) {
case Vector:
panic("Vector macros not yet implemented")
case Boolean, String, Char, Integer, Rational:
p.Ins = append(p.Ins, Ins{Imm, v, 0})
case Symbol, Scoped:
p.Ins = append(p.Ins, Ins{GetVar, v, 0})
case *Pair:
args, err := list2vec(v.(*Pair))
if err != nil {
return err
}
if len(args) == 0 {
return errors.New("Empty expression")
}
car := args[0]
if scoped, ok := args[0].(Scoped); ok {
car = scoped.Symbol
}
if sym, ok := car.(Symbol); ok {
if syntaxrules, ok := p.Macros[sym]; ok {
var pattern *Pair
form := Unscope(*v.(*Pair).Cdr)
i, found := 0, false
for i, pattern = range syntaxrules.Patterns {
if IsMatch(Unscope(*pattern.Cdr),
form,
syntaxrules.Literals) {
found = true
break
}
}
if !found {
PrintValue(v)
fmt.Println()
return fmt.Errorf(
"No match found for macro %s", SymbolNames[sym],
)
}
m := MacroMap{}
m.parse(*pattern.Cdr, form, syntaxrules.Literals, true)
trans, err := m.transcribe(
syntaxrules.Templates[i],
false,
sym,
)
if err != nil {
return err
}
if err := p.Gen(trans); err != nil {
return err
}
return nil
}
switch sym {
case SymSet:
if len(args) != 3 {
return errors.New("set! takes 2 args")
}
if _, ok := args[1].(Symbol); !ok {
return errors.New("First arg to set! must be a symbol")
}
if err := p.Gen(args[2]); err != nil {
return err
}
p.Ins = append(p.Ins, Ins{Set, args[1], 1})
return nil
case SymDefine:
switch args[1].(type) {
case *Pair:
if len(args) < 2 {
return errors.New(
"Function definition requires at least one " +
"statement",
)
}
dest, defargs := *args[1].(*Pair).Car, *args[1].(*Pair).Cdr
lambda := Procedure{
Args: Unscope(defargs),
Ins: []Ins{},
Macros: map[Symbol]SyntaxRules{},
}
for k, v := range p.Macros {
lambda.Macros[k] = v
}
for _, expr := range args[2:] {
if err := lambda.Gen(Unscope(expr)); err != nil {
return err
}
}
p.Ins = append(p.Ins, Ins{Lambda, lambda, 0})
p.Ins = append(p.Ins, Ins{Define, dest, 1})
case Symbol:
if len(args) != 3 {
return errors.New("define takes 2 args")
}
if err := p.Gen(args[2]); err != nil {
return err
}
p.Ins = append(p.Ins, Ins{Define, args[1], 1})
default:
return fmt.Errorf(
"First arg to define must be a symbol: %T", args[1],
)
}
return nil
case SymLambda:
if len(args) < 3 {
return errors.New("lambda requires at least one statement")
}
lambda := Procedure{
Args: Unscope(args[1]),
Ins: []Ins{},
Macros: map[Symbol]SyntaxRules{},
}
for k, v := range p.Macros {
lambda.Macros[k] = v
}
for _, expr := range args[2:] {
if err := lambda.Gen(Unscope(expr)); err != nil {
return err
}
}
p.Ins = append(p.Ins, Ins{Lambda, lambda, 0})
return nil
case SymIf:
lt := Procedure{
Args: p.Args,
Ins: []Ins{},
Macros: map[Symbol]SyntaxRules{},
}
for k, v := range p.Macros {
lt.Macros[k] = v
}
lf := lt
lf.Macros = map[Symbol]SyntaxRules{}
for k, v := range p.Macros {
lf.Macros[k] = v
}
if err := lt.Gen(args[2]); err != nil {
return err
}
if len(args) > 4 {
return errors.New("Too many args to if")
} else if len(args) == 4 {
if err := lf.Gen(args[3]); err != nil {
return err
}
p.Ins = append(p.Ins, Ins{Imm, lf, 0})
} else if len(args) < 3 {
return errors.New("Too few args to if")
}
p.Ins = append(p.Ins, Ins{Imm, lt, 0})
if err := p.Gen(args[1]); err != nil {
return err
}
p.Ins = append(p.Ins, Ins{If, nil, len(args) - 1})
return nil
case Quote:
if len(args) != 2 {
return errors.New("Wrong number of args to quote")
}
p.Ins = append(p.Ins, Ins{Imm, Unscope(args[1]), 0})
return nil
// These are for the implementation of (hygenic) macros
case SymSaveScope:
if len(args) != 1 {
return errors.New("Wrong number of args to save-scope")
}
p.Ins = append(p.Ins, Ins{SaveScope, nil, 0})
return nil
case SymDefineSyntax:
if len(args) != 3 {
return errors.New("Wrong number of args to define-syntax")
}
var name Symbol
if _, ok = args[1].(Scoped); ok {
name = args[1].(Scoped).Symbol
} else {
name, ok = args[1].(Symbol)
if !ok {
return fmt.Errorf(
"Expected macro name, got %t", args[1],
)
}
}
_, ok := args[2].(*Pair)
if !ok {
return fmt.Errorf("Expected syntax-rules, got %T", args[2])
}
syntaxrules, err := ParseSyntaxRules(args[2].(*Pair))
if err != nil {
return err
}
if _, ok := p.Macros[name]; ok {
fmt.Printf("WARNING: Redefining macro %s",
SymbolNames[name])
}
p.Macros[name] = *syntaxrules
p.Ins = append(p.Ins, Ins{SaveScope, nil, 0})
p.Ins = append(p.Ins, Ins{Define, name, 1})
return nil
case SymLetrecSyntax, SymLetSyntax:
if len(args) != 3 {
return errors.New("Wrong number of args to letrec-syntax")
}
for i := range args {
args[i] = Unscope(args[i])
}
if _, ok := args[1].(*Pair); !ok {
return errors.New(
"First argument to let-syntax must be a list of " +
"bindings",
)
}
bindings, err := list2vec(args[1].(*Pair))
if err != nil {
return err
}
names := []Symbol{}
rules := []SyntaxRules{}
for _, binding := range bindings {
p, ok := binding.(*Pair)
if !ok {
return errors.New("let-syntax bindings must be lists")
}
if _, ok := (*p.Car).(Symbol); !ok {
return errors.New("Binding names must be symbols")
}
names = append(names, (*p.Car).(Symbol))
if _, ok := (*p.Cdr).(*Pair); !ok {
return errors.New("syntax-rules must be pairs")
}
syntaxrules, err := ParseSyntaxRules(*(*p.Cdr).(*Pair).Car)
if err != nil {
return err
}
rules = append(rules, *syntaxrules)
}
lambda := Procedure{
Ins: []Ins{},
Macros: map[Symbol]SyntaxRules{},
}
for k, v := range p.Macros {
lambda.Macros[k] = v
}
if sym == SymLetrecSyntax {
// letrec-syntax is easy, we just need to put everything in
// one new lambda
for i := range names {
lambda.Macros[names[i]] = rules[i]
lambda.Ins = append(lambda.Ins, Ins{SaveScope, nil, 0})
lambda.Ins = append(lambda.Ins, Ins{
Define,
names[i],
1})
}
} else {
// let-syntax is a bit more difficult. We need to isolate
// the macros from recursion. To do this, we create a
// lambda for each macro that returns its scope, and assign
// that scope to the lambda. Once the lambda is called,
// that scope is used, which excludes the other scopes
// since they are encapsulated in other lambdas
for i := range names {
mlambda := Procedure{
Ins: []Ins{},
Macros: map[Symbol]SyntaxRules{},
}
for k, v := range p.Macros {
mlambda.Macros[k] = v
}
mlambda.Macros[names[i]] = rules[i]
mlambda.Ins = append(mlambda.Ins, Ins{
SaveScope,
nil,
0,
})
lambda.Ins = append(lambda.Ins, Ins{Lambda, lambda, 0})
lambda.Ins = append(lambda.Ins, Ins{Call, nil, 0})
lambda.Ins = append(lambda.Ins, Ins{
Define,
names[i],
1,
})
}
}
if err := lambda.Gen(args[2]); err != nil {
return nil
}
p.Ins = append(p.Ins, Ins{Lambda, lambda, 0})
p.Ins = append(p.Ins, Ins{Call, nil, 0})
return nil
}
}
// first arg is the callee
for i := len(args) - 1; i >= 0; i-- {
if err := p.Gen(args[i]); err != nil {
return err
}
}
p.Ins = append(p.Ins, Ins{Call, nil, len(args) - 1})
}
return nil
}