/
spirit.go
224 lines (182 loc) · 4.45 KB
/
spirit.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
package internal
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
)
const (
nsSeparator = '/'
defaultNS = "user"
)
var _ Scope = (*Spirit)(nil)
// returns new Spirit instance
func NewSpirit() *Spirit {
sl := &Spirit{ mu: &sync.RWMutex{},
Bindings: map[nsSymbol]Value{},
}
if err := bindAll(sl); err != nil {
panic(err)
}
sl.checkNS = true
_ = sl.SwitchNS(Symbol{Value: defaultNS})
_ = sl.BindGo("ns", sl.SwitchNS)
return sl
}
// Spirit instance
type Spirit struct {
Stack
mu *sync.RWMutex
currentNS string
checkNS bool
Bindings map[nsSymbol]Value
Files []string
}
// Eval evaluates the given value in spirit context.
func (spirit *Spirit) Eval(v Value) (Value, error) {
return Eval(spirit, v)
}
// ReadEval reads from the given reader and evaluates all the forms
// obtained in spirit context.
func (spirit *Spirit) ReadEval(r io.Reader) (Value, error) {
return ReadEval(spirit, r)
}
// ReadFile reads the content of the filename given. Use this to
// prevent recursive source
func (spirit *Spirit) ReadFile(filePath string) (Value, error) {
if spirit.FileImported(filePath) {
return nil, nil
}
spirit.AddFile(filePath)
f, err := os.Open(filePath)
defer f.Close()
if err != nil {
return nil, ImportError{err}
}
cwd, err := os.Getwd()
if err != nil {
return nil, OSError{err}
}
dir := filepath.Dir(filePath)
spirit.BindGo("*cwd*", dir)
os.Chdir(dir)
value, err := spirit.ReadEval(f)
if err != nil {
return nil, err
}
os.Chdir(cwd)
return value, nil
}
// AddFile adds file to slice of imported files to prevent circular dependency.
func (s *Spirit) AddFile(file string) {
s.mu.Lock()
s.Files = append(s.Files, file)
s.mu.Unlock()
}
func (s *Spirit) FileImported(file string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
for _, v := range s.Files {
if v == file {
return true
}
}
return false
}
// ReadEvalStr reads the source and evaluates it in spirit context.
func (spirit *Spirit) ReadEvalStr(src string) (Value, error) {
return spirit.ReadEval(strings.NewReader(src))
}
// Bind binds the given name to the given Value into the spirit interpreter
// context.
func (spirit *Spirit) Bind(symbol string, v Value) error {
spirit.mu.Lock()
defer spirit.mu.Unlock()
nsSym, err := spirit.splitSymbol(symbol)
if err != nil {
return err
}
if spirit.checkNS && nsSym.NS != spirit.currentNS {
return fmt.Errorf("cannot bind outside current namespace")
}
spirit.Bindings[*nsSym] = v
return nil
}
// Resolve finds the value bound to the given symbol and returns it if
// found in the spirit context and returns it.
func (spirit *Spirit) Resolve(symbol string) (Value, error) {
spirit.mu.RLock()
defer spirit.mu.RUnlock()
if symbol == "ns" {
symbol = "user/ns"
}
nsSym, err := spirit.splitSymbol(symbol)
if err != nil {
return nil, err
}
return spirit.resolveAny(symbol, *nsSym, nsSym.WithNS("core"))
}
// BindGo is similar to Bind but handles conversion of Go value 'v' to
// internal Value type.
func (spirit *Spirit) BindGo(symbol string, v interface{}) error {
return spirit.Bind(symbol, ValueOf(v))
}
// SwitchNS changes the current namespace to the string value of given symbol.
func (spirit *Spirit) SwitchNS(sym Symbol) error {
spirit.mu.Lock()
spirit.currentNS = sym.String()
spirit.mu.Unlock()
return spirit.Bind("*ns*", sym)
}
// CurrentNS returns the current active namespace.
func (spirit *Spirit) CurrentNS() string {
spirit.mu.RLock()
defer spirit.mu.RUnlock()
return spirit.currentNS
}
// Parent always returns nil to represent this is the root scope.
func (spirit *Spirit) Parent() Scope {
return nil
}
func (spirit *Spirit) resolveAny(symbol string, syms ...nsSymbol) (Value, error) {
for _, s := range syms {
v, found := spirit.Bindings[s]
if found {
return v, nil
}
}
return nil, ResolveError{Sym: Symbol{Value:symbol}}
}
func (spirit *Spirit) splitSymbol(symbol string) (*nsSymbol, error) {
sep := string(nsSeparator)
if symbol == sep {
return &nsSymbol{
NS: spirit.currentNS,
Name: symbol,
}, nil
}
parts := strings.SplitN(symbol, sep, 2)
if len(parts) < 2 {
return &nsSymbol{
NS: spirit.currentNS,
Name: symbol,
}, nil
}
if strings.Contains(parts[1], sep) && parts[1] != sep {
return nil, fmt.Errorf("invalid qualified symbol: '%s'", symbol)
}
return &nsSymbol{
NS: parts[0],
Name: parts[1],
}, nil
}
type nsSymbol struct {
NS string
Name string
}
func (s nsSymbol) WithNS(ns string) nsSymbol {
s.NS = ns
return s
}