-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.go
348 lines (302 loc) · 6.83 KB
/
library.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
347
348
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"io"
"os"
"github.com/markkurossi/scheme/types"
)
// Library implements a Scheme compilation unit.
type Library struct {
scm *Scheme
Source string
Name Value
Body *ASTSequence
Exports Value
ExportAll bool
Imports Value
Init Code
PCMap PCMap
lambdas []*lambdaCompilation
nextLabel int
exported map[string]*export
recheck bool
current *lambdaCompilation
}
// Scheme implements Value.Scheme.
func (lib *Library) Scheme() string {
return fmt.Sprintf("library %s", lib.Name.Scheme())
}
// Eq implements Value.Eq.
func (lib *Library) Eq(o Value) bool {
olib, ok := o.(*Library)
return ok && lib == olib
}
// Equal implements Value.Equal.
func (lib *Library) Equal(o Value) bool {
return lib.Eq(o)
}
// Type implements Value.Type.
func (lib *Library) Type() *types.Type {
return types.Any
}
func (lib *Library) parseLibraryHeader(list []Pair) error {
if len(list) < 4 {
return list[0].Errorf("truncated library header")
}
// Name.
pair, ok := list[1].Car().(Pair)
if !ok {
return list[1].Errorf("invalid library name: %v", list[1].Car())
}
l, ok := ListPairs(pair)
if !ok || len(l) == 0 {
return list[1].Errorf("invalid library name: %v", pair)
}
lib.Name = pair
// Export.
l, ok = ListPairs(list[2].Car())
if !ok || len(l) == 0 || !isNamedIdentifier(l[0].Car(), "export") {
return list[2].Errorf("expected (export ...)")
}
for i := 1; i < len(l); i++ {
_, ok := isIdentifier(l[i].Car())
if !ok {
return l[i].Errorf("invalid export name: %v", l[i])
}
}
lib.Exports = l[0].Cdr()
// Import.
pair, ok = list[3].Car().(Pair)
if !ok {
return list[3].Errorf("invalid library import: %v", list[3].Car())
}
_, ok = ListPairs(list[3].Car())
if !ok {
return list[3].Errorf("expected (import ...)")
}
lib.Imports, _ = Cdr(pair, true)
return nil
}
// MapPC maps the program counter value to the source location.
func (lib *Library) MapPC(pc int) (source string, line int) {
source = lib.Source
if false {
fmt.Printf("Library.MapPC: %v:%v\n", source, pc)
for idx, pm := range lib.PCMap {
fmt.Printf(" - %v\tPC=%v, Line=%v\n", idx, pm.PC, pm.Line)
}
lib.Init.Print(os.Stdout)
}
line = lib.PCMap.MapPC(pc)
return
}
// PCMap implements mapping from program counter values to source line
// numbers.
type PCMap []PCLine
// MapPC maps the program counter value to the source line number.
func (pcmap PCMap) MapPC(pc int) (line int) {
for _, pm := range pcmap {
if pc > pm.PC {
line = pm.Line
}
if pc <= pm.PC {
break
}
}
return
}
// PCLine maps program counter values to line numbers.
type PCLine struct {
PC int
Line int
}
// Code implements scheme bytecode.
type Code []*Instr
// Print prints the code to standard output.
func (code Code) Print(w io.Writer) {
for idx, c := range code {
fmt.Fprintf(w, "%v\t%s\n", idx, c)
}
}
// Compile compiles the library into bytecode.
func (lib *Library) Compile() (Value, error) {
lib.recheck = true
for round := 0; lib.recheck; round++ {
lib.recheck = false
err := lib.Body.Typecheck(lib, round)
if err != nil {
return nil, err
}
}
err := lib.Body.Bytecode(lib)
if err != nil {
return nil, err
}
lib.addInstr(nil, OpReturn, nil, 0)
// Compile lambdas.
var pcmaps []PCMap
for i := 0; i < len(lib.lambdas); i++ {
lambda := lib.lambdas[i]
pcmapStart := len(lib.PCMap)
lib.current = lambda
// Lambda body starts after the label.
ofs := len(lib.Init)
lambda.Start = ofs + 1
lambda.Label = lib.addInstr(nil, OpLabel, nil, lambda.Start)
for _, ast := range lambda.Body {
err := ast.Bytecode(lib)
if err != nil {
return nil, err
}
}
lib.addInstr(nil, OpReturn, nil, 0)
lambda.End = len(lib.Init)
pcmap := lib.PCMap[pcmapStart:len(lib.PCMap)]
for i := 0; i < len(pcmap); i++ {
pcmap[i].PC -= lambda.Start
}
pcmaps = append(pcmaps, pcmap)
lib.current = nil
}
// Collect label offsets
labels := make(map[int]int)
for idx, c := range lib.Init {
if c.Op == OpLabel {
labels[c.I] = idx
}
}
// Patch code offsets.
for i := 0; i < len(lib.Init); i++ {
instr := lib.Init[i]
switch instr.Op {
case OpLambda:
pcmap := pcmaps[instr.I]
def := lib.lambdas[instr.I]
var name string
if def.Name != nil {
name = def.Name.Name
}
ctx := make(types.Ctx)
instr.I = def.Start
instr.J = def.End
instr.V = &LambdaImpl{
Name: name,
Args: def.Args,
Return: def.Body[len(def.Body)-1].Type(ctx),
Captures: def.Captures,
Source: lib.Source,
Code: lib.Init[def.Start:def.End],
MaxStack: def.Env.Stats.MaxStack,
PCMap: pcmap,
Body: def.Body,
}
case OpIf, OpIfNot, OpJmp:
ofs, ok := labels[instr.J]
if !ok {
return nil, fmt.Errorf("Label l%v not defined", instr.J)
}
instr.I = ofs - i
}
}
// Check that all exported names were defined.
for k, v := range lib.exported {
if v.id == nil {
return nil, v.from.Errorf("exported symbol '%s' not defined", k)
}
}
return &Lambda{
Impl: &LambdaImpl{
Return: types.Any,
Source: lib.Source,
Code: lib.Init,
PCMap: lib.PCMap,
Captures: true,
},
}, nil
}
func (lib *Library) addCall(from Locator, numArgs int, tail bool) {
if numArgs >= 0 {
lib.addInstr(from, OpConst, Int(numArgs), 0)
}
var i int
if tail {
i = 1
}
lib.addInstr(from, OpCall, nil, i)
}
func (lib *Library) addPushS(from Locator, size int, capture bool) {
var op Operand
if capture {
op = OpPushE
} else {
op = OpPushS
}
lib.addInstr(from, op, nil, size)
}
func (lib *Library) addPopS(from Locator, size int, capture bool) {
var op Operand
if capture {
op = OpPopE
} else {
op = OpPopS
}
instr := lib.addInstr(from, op, nil, size)
if !capture {
instr.J = 1
}
}
func (lib *Library) addInstr(from Locator, op Operand, v Value, i int) *Instr {
instr := &Instr{
Op: op,
V: v,
I: i,
}
if from != nil {
p := from.From()
if len(lib.PCMap) == 0 ||
lib.PCMap[len(lib.PCMap)-1].Line != p.Line {
lib.PCMap = append(lib.PCMap, PCLine{
PC: len(lib.Init),
Line: p.Line,
})
}
}
lib.Init = append(lib.Init, instr)
return instr
}
func (lib *Library) addLabel(l *Instr) {
lib.Init = append(lib.Init, l)
}
func (lib *Library) newLabel() *Instr {
lib.nextLabel++
return &Instr{
Op: OpLabel,
I: lib.nextLabel - 1,
}
}
func (lib *Library) setBinding(from Locator, b *EnvBinding) {
if b.Frame.Type == TypeStack {
lib.addInstr(from, OpLocalSet, nil, b.Frame.Index+b.Index)
} else {
instr := lib.addInstr(from, OpEnvSet, nil, b.Frame.Index)
instr.J = b.Index
}
}
type lambdaCompilation struct {
Start int
Label *Instr
End int
Self AST
Name *Identifier
Args Args
ArgBindings []*EnvBinding
Body []AST
Env *Env
MaxStack int
Captures bool
}