/
Module.go
345 lines (275 loc) · 6.15 KB
/
Module.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
// a module must load all files at once, can't be done lazily
type Module struct {
dir string
p *Package
files []*File
all []*ScopedFunc
exported []*ScopedFunc
}
func LoadModule(p *Package, consumers []*Module, dir *String, ew ErrorWriter) *Module {
if !isDir(dir.Value()) {
ew.Add(dir.Context().Error("\"" + dir.Value() + "\" not found"))
return nil
}
if DEBUG_PKG_LOADING {
fmt.Println("loading module \"" + dir.Value() + "\"")
}
for _, consumer := range consumers {
if consumer.Dir() == dir.Value() {
ew.Add(errors.New("circular import detected"))
return nil
}
}
// parse all files ending with .cas in the given directory
infos, err := ioutil.ReadDir(dir.Value())
if err != nil {
ew.Add(err)
return nil
}
files := []*File{}
for _, info := range infos {
if filepath.Ext(info.Name()) == ".cas" {
path := filepath.Join(dir.Value(), info.Name())
f := ParseFile(path, ew)
if !ew.Empty() {
return nil
}
files = append(files, f)
}
}
if len(files) == 0 {
ew.Add(errors.New("module \"" + dir.Value() + "\" doesn't contain any source files"))
return nil
}
m := &Module{dir.Value(), p, files, nil, nil}
consumers = append(consumers, m)
for _, f := range m.files {
f.GetModules(p, consumers, ew)
}
return m
}
func LoadReplModule(p *Package, ew ErrorWriter) *Module {
files := []*File{NewReplFile()}
return &Module{"", p, files, nil, nil}
}
func (m *Module) Dir() string {
return m.dir
}
func isExportedName(name string) bool {
return !strings.HasPrefix(name, "_")
}
func (m *Module) MergeFuncs() {
allFns := []*ScopedFunc{}
for _, f := range m.files {
Outer:
for _, fn := range f.fns {
for _, check := range allFns {
if check == fn {
continue Outer
}
}
allFns = append(allFns, fn)
}
}
exported := []*ScopedFunc{}
for _, fn := range allFns {
if isExportedName(fn.Name()) {
exported = append(exported, fn)
}
}
for _, f := range m.files {
// make a copy of the list, so it can be mutated by files without affecting other files
cpy := make([]*ScopedFunc, len(allFns))
for i, fn := range allFns {
cpy[i] = fn
}
f.fns = cpy
}
m.all = allFns
m.exported = exported
}
func (m *Module) GetExportedFuncs() []*ScopedFunc {
return m.exported
}
func (m *Module) ImportFuncs(ew ErrorWriter) {
for _, f := range m.files {
f.ImportFuncs(ew)
}
}
func (m *Module) GetDependencies() []*Module {
deps := []*Module{}
for _, f := range m.files {
for _, imodule := range f.imodules {
firstTime := true
for _, check := range deps {
if check == imodule {
firstTime = false
}
}
if firstTime {
deps = append(deps, imodule)
}
}
}
return deps
}
func (m *Module) listFuncs(name string) []Func {
fns := []Func{}
for _, fn := range m.all {
if fn.Name() == name {
fns = append(fns, fn.fn)
}
}
return fns
}
func multipleConstructorsError(name string, fns []Func) string {
var b strings.Builder
b.WriteString("multiple definitions of \"")
b.WriteString(name)
b.WriteString("\"")
for _, fn := range fns {
b.WriteString("\n ")
b.WriteString(fn.Context().WriteLocation())
}
return b.String()
}
// also detect non-unique constructors
func (m *Module) ListLocalTypes(ew ErrorWriter) []string {
lst := []string{}
Outer:
for _, fn := range m.all {
if isConstructorName(fn.Name()) {
for _, check := range lst {
if check == fn.Name() {
ew.Add(errors.New(multipleConstructorsError(fn.Name(), m.listFuncs(fn.Name()))))
continue Outer
}
}
lst = append(lst, fn.Name())
}
}
return lst
}
// assume a and b are sorted
func haveCommonEntries(a []string, b []string) bool {
i := 0
j := 0
for {
if i >= len(a) || j >= len(b) {
return false
}
if a[i] == b[j] {
return true
} else if a[i] > b[j] {
j++
} else {
i++
}
}
}
func (m *Module) SyncMethods(ew ErrorWriter) {
local := sortUniqStrings(m.ListLocalTypes(ew))
// only the exported methods attached to any Constructors can be pushed upwards
pushed := []*ScopedFunc{}
for _, fn := range m.exported {
fnTypes := fn.ListHeaderTypes()
if haveCommonEntries(local, fnTypes) {
pushed = append(pushed, fn)
}
}
deps := m.GetDependencies()
for _, dep := range deps {
dep.PushMethods(pushed)
}
}
func (m *Module) PushMethods(fns []*ScopedFunc) {
for _, f := range m.files {
f.PushMethods(fns)
}
deps := m.GetDependencies()
for _, dep := range deps {
dep.PushMethods(fns)
}
}
func (m *Module) BuildDBs(gScope *GlobalScope) {
for _, f := range m.files {
f.BuildDB(gScope)
}
}
func (m *Module) SetLinker(linker *Linker) {
for _, f := range m.files {
f.SetLinker(linker)
}
}
func (m *Module) RunEntryPoint(path string, ew ErrorWriter) {
// pick any file
f := m.files[0]
if DEBUG_PKG_LOADING {
fmt.Println("fns available in \"" + path + "\":")
fmt.Println(f.DumpFuncs())
}
name := "main"
fns := f.ListDispatchable(name, 0, ew)
if !ew.Empty() { // linking happens at the same time dispatching
return
}
// now filter by path
var fn Func
for _, fn_ := range fns {
if fn_.Context().Path() == path {
if fn != nil {
ew.Add(errors.New("multiple definitions of entry-point \"" + name + "\" in \"" + path + "\""))
return
} else {
fn = fn_
}
}
}
if fn == nil {
ew.Add(errors.New("\"" + name + "\" not found in \"" + path + "\""))
return
}
f.linker.entry = fn
if DEBUG_PKG_LOADING {
fmt.Println("running entry point \"" + name + "\" in \"" + path + "\"")
}
d := fn.Dispatch([]Value{}, ew)
if d == nil || d.Failed() {
ew.Add(fn.Context().Error("failed to run func"))
return
} else if !ew.Empty() {
return
}
{
v := d.Eval()
v = EvalEager(v, ew)
if v != nil && ew.Empty() {
if !IsIO(v) {
ew.Add(v.Context().Error("expected IO, got " + v.Dump()))
} else {
io := AssertIO(v)
v = io.Run(NewDefaultIOContext())
if v != nil && ew.Empty() {
ew.Add(v.Context().Error("unused IO result"))
}
}
}
}
return
}
func (m *Module) DumpFuncs() string {
var b strings.Builder
for _, fn := range m.all {
b.WriteString(fn.Dump())
b.WriteString("\n")
}
return b.String()
}