forked from elves/elvish
/
compiler.go
152 lines (136 loc) · 3.42 KB
/
compiler.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
package eval
//go:generate ./boilerplate.py
import (
"fmt"
"strings"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
)
// compiler maintains the set of states needed when compiling a single source
// file.
type compiler struct {
// Builtin namespace.
builtin staticNs
// Lexical namespaces.
scopes []staticNs
// Variables captured from outer scopes.
capture staticNs
// Position of what is being compiled.
begin, end int
// Information about the source.
srcMeta *Source
}
func compile(b, g staticNs, n *parse.Chunk, src *Source) (op Op, err error) {
cp := &compiler{b, []staticNs{g}, make(staticNs), 0, 0, src}
defer util.Catch(&err)
return cp.chunkOp(n), nil
}
func (cp *compiler) compiling(n parse.Node) {
cp.begin, cp.end = n.Begin(), n.End()
}
func (cp *compiler) errorpf(begin, end int, format string, args ...interface{}) {
throw(&CompilationError{fmt.Sprintf(format, args...),
*util.NewSourceRange(cp.srcMeta.describePath(), cp.srcMeta.code, begin, end)})
}
func (cp *compiler) errorf(format string, args ...interface{}) {
cp.errorpf(cp.begin, cp.end, format, args...)
}
func (cp *compiler) thisScope() staticNs {
return cp.scopes[len(cp.scopes)-1]
}
func (cp *compiler) pushScope() staticNs {
sc := make(staticNs)
cp.scopes = append(cp.scopes, sc)
return sc
}
func (cp *compiler) popScope() {
cp.scopes[len(cp.scopes)-1] = make(staticNs)
cp.scopes = cp.scopes[:len(cp.scopes)-1]
}
func (cp *compiler) registerVariableGetQname(qname string) bool {
_, ns, name := ParseVariableRef(qname)
return cp.registerVariableGet(ns, name)
}
func (cp *compiler) registerVariableGet(ns, name string) bool {
switch ns {
case "", "local", "up":
// Handled below
case "e", "E":
return true
default:
return cp.registerModAccess(ns)
}
// Find in local scope
if ns == "" || ns == "local" {
if cp.thisScope().has(name) {
return true
}
}
// Find in upper scopes
if ns == "" || ns == "up" {
for i := len(cp.scopes) - 2; i >= 0; i-- {
if cp.scopes[i].has(name) {
// Existing name: record capture and return.
cp.capture.set(name)
return true
}
}
}
// Find in builtin scope
if ns == "" || ns == "builtin" {
if cp.builtin.has(name) {
return true
}
}
return false
}
func (cp *compiler) registerVariableSetQname(qname string) bool {
_, ns, name := ParseVariableRef(qname)
return cp.registerVariableSet(ns, name)
}
func (cp *compiler) registerVariableSet(ns, name string) bool {
switch ns {
case "local":
cp.thisScope().set(name)
return true
case "up":
for i := len(cp.scopes) - 2; i >= 0; i-- {
if cp.scopes[i].has(name) {
// Existing name: record capture and return.
cp.capture.set(name)
return true
}
}
return false
case "builtin":
cp.errorf("cannot set builtin variable")
return false
case "":
if cp.thisScope().has(name) {
// A name on current scope. Do nothing.
return true
}
// Walk up the upper scopes
for i := len(cp.scopes) - 2; i >= 0; i-- {
if cp.scopes[i].has(name) {
// Existing name. Do nothing
cp.capture.set(name)
return true
}
}
// New name. Register on this scope!
cp.thisScope().set(name)
return true
case "e", "E":
// Special namespaces, do nothing
return true
default:
return cp.registerModAccess(ns)
}
}
func (cp *compiler) registerModAccess(name string) bool {
if strings.ContainsRune(name, ':') {
name = name[:strings.IndexByte(name, ':')]
}
return cp.registerVariableGet("", name+NsSuffix)
}