-
Notifications
You must be signed in to change notification settings - Fork 546
/
module2.go
202 lines (164 loc) · 3.88 KB
/
module2.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
package qlang
import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"syscall"
"qiniupkg.com/text/tpl.v1/interpreter"
"qlang.io/exec"
)
// -----------------------------------------------------------------------------
// An FindEntryError represents a FindEntry error.
//
type FindEntryError struct {
Name string
Err error
}
func (e *FindEntryError) Error() string {
return strconv.Quote(e.Name) + ": " + e.Err.Error()
}
func findEntry(file string, libs []string) (string, error) {
if strings.HasPrefix(file, "/") {
_, err := os.Stat(file)
if err == nil {
return file, nil
}
return "", &FindEntryError{file, err}
}
for _, dir := range libs {
if dir == "" {
continue
}
path := dir + "/" + file
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return "", &FindEntryError{file, syscall.ENOENT}
}
func resolvePath(file string, base string) string {
if strings.HasPrefix(file, "/") {
return file
}
return path.Join(base, file)
}
var (
// FindEntry specifies the policy how qlang searches library file.
FindEntry = findEntry
// ReadFile specifies the policy how qlang reads source file.
ReadFile = ioutil.ReadFile
)
// -----------------------------------------------------------------------------
const (
indexFile = "/main.ql"
)
func qlangFile(file string) string {
if path.Ext(file) == ".ql" {
return file
}
return file + indexFile
}
func (p *Compiler) dir() string {
if v, ok := p.gvars["__dir__"]; ok {
if dir, ok := v.(string); ok {
return dir
}
}
panic("ident `__dir__` not found")
}
func (p *Compiler) compileModule(fname string, parent *funcCtx) (end int, symtbl map[string]int) {
ctx := newFuncCtx(parent, nil)
old := p.fnctx
p.fnctx = ctx
end = p.Compile(fname)
symtbl = ctx.symtbl
p.fnctx = old
return
}
// Compile compiles a qlang source file.
//
func (p *Compiler) Compile(fname string) int {
codeText, err := ReadFile(fname)
if err != nil {
panic(err)
}
return p.Cl(codeText, fname)
}
// Cl compiles a qlang source code.
//
func (p *Compiler) Cl(codeText []byte, fname string) int {
engine, err := interpreter.New(p, p.Opts)
if err != nil {
panic(err)
}
old := p.ipt
defer func() { // backup interpreter to restore
p.ipt = old
}()
p.ipt = engine
p.gvars["__dir__"] = path.Dir(fname)
p.gvars["__file__"] = fname
err = engine.MatchExactly(codeText, fname)
if err != nil {
panic(err)
}
return p.code.Len()
}
// -----------------------------------------------------------------------------
func (p *Compiler) include(lit string) {
file, err := strconv.Unquote(lit)
if err != nil {
panic("invalid string `" + lit + "`: " + err.Error())
}
fname := qlangFile(resolvePath(file, p.dir()))
p.Compile(fname)
}
func requireModuleSym(fnctx *funcCtx, name string) int {
if _, ok := fnctx.getSymbol(name); ok {
panic("import `" + name + "` error: ident exists")
}
return fnctx.newSymbol(name)
}
func (p *Compiler) fnImport(lit string) {
dir, err := strconv.Unquote(lit)
if err != nil {
panic("invalid string `" + lit + "`: " + err.Error())
}
file, err := FindEntry(dir+indexFile, p.libs)
if err != nil {
panic(err)
}
var name string
arity := p.popArity()
if arity > 0 {
name = p.popName()
} else {
name = path.Base(dir)
if pos := strings.Index(name, "."); pos > 0 {
name = name[:pos]
}
}
code := p.code
instr := code.Reserve()
id := requireModuleSym(p.fnctx, name)
code.Block(exec.As(id))
p.exits = append(p.exits, func() {
file = path.Clean(file)
mod, ok := p.mods[file]
if !ok {
start := code.Len()
end, symtbl := p.compileModule(file, nil)
mod = module{start, end, symtbl}
p.mods[file] = mod
}
instr.Set(exec.Module(file, mod.start, mod.end, mod.symtbl))
})
}
func (p *Compiler) export() {
arity := p.popArity()
names := p.gstk.PopFnArgs(arity)
p.code.Block(exec.Export(names...))
}
// -----------------------------------------------------------------------------