-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsymbol_table_frame.go
318 lines (280 loc) · 8.03 KB
/
symbol_table_frame.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
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements the symbol table.
package golisp
import (
"container/list"
"errors"
"fmt"
"sync"
"sync/atomic"
)
const (
READ_LOCK = iota
WRITE_LOCK
)
type SymbolTableFrame struct {
Name string
Parent *SymbolTableFrame
Previous *SymbolTableFrame
Frame *FrameMap
Bindings map[string]*Binding
Mutex sync.RWMutex
CurrentCode *list.List
IsRestricted bool
}
type symbolsTable struct {
Symbols map[string]*Data
Mutex sync.RWMutex
}
type environmentsTable struct {
Environments map[string]*SymbolTableFrame
Mutex sync.RWMutex
}
var Global *SymbolTableFrame
var TopLevelEnvironments environmentsTable = environmentsTable{make(map[string]*SymbolTableFrame, 5), sync.RWMutex{}}
var internedSymbols symbolsTable = symbolsTable{make(map[string]*Data, 256), sync.RWMutex{}}
func Intern(name string) (sym *Data) {
internedSymbols.Mutex.RLock()
sym = internedSymbols.Symbols[name]
internedSymbols.Mutex.RUnlock()
if sym == nil {
internedSymbols.Mutex.Lock()
sym = SymbolWithName(name)
internedSymbols.Symbols[name] = sym
internedSymbols.Mutex.Unlock()
}
return
}
func (self *SymbolTableFrame) Depth() int {
if self.Previous == nil {
return 1
} else {
return 1 + self.Previous.Depth()
}
}
func (self *SymbolTableFrame) CurrentCodeString() string {
if self.CurrentCode.Len() > 0 {
return self.CurrentCode.Front().Value.(string)
} else {
return "Unknown code"
}
}
func (self *SymbolTableFrame) InternalDump(frameNumber int) {
fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCodeString())
self.Mutex.RLock()
defer self.Mutex.RUnlock()
for _, b := range self.Bindings {
if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
b.Dump()
}
}
fmt.Printf("\n")
if self.Previous != nil {
self.Previous.InternalDump(frameNumber + 1)
}
}
func (self *SymbolTableFrame) Dump() {
println()
self.InternalDump(0)
}
func (self *SymbolTableFrame) DumpSingleFrame(frameNumber int) {
if frameNumber == 0 {
fmt.Printf("%s\n", self.CurrentCodeString())
self.Mutex.RLock()
defer self.Mutex.RUnlock()
for _, b := range self.Bindings {
if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
b.Dump()
}
}
fmt.Printf("\n")
} else if self.Previous != nil {
self.Previous.DumpSingleFrame(frameNumber - 1)
} else {
fmt.Printf("Invalid frame selected.\n")
}
}
func (self *SymbolTableFrame) InternalDumpHeaders(frameNumber int) {
fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCodeString())
if self.Previous != nil {
self.Previous.InternalDumpHeaders(frameNumber + 1)
}
}
func (self *SymbolTableFrame) DumpHeaders() {
println()
self.InternalDumpHeaders(0)
}
func (self *SymbolTableFrame) DumpHeader() {
fmt.Printf("%s\n", self.CurrentCodeString())
}
func NewSymbolTableFrameBelow(p *SymbolTableFrame, name string) *SymbolTableFrame {
var f *FrameMap = nil
if p != nil {
f = p.Frame
}
restricted := p != nil && p.IsRestricted
env := &SymbolTableFrame{Name: name, Parent: p, Bindings: make(map[string]*Binding), Frame: f, CurrentCode: list.New(), IsRestricted: restricted}
if p == nil || p == Global {
TopLevelEnvironments.Mutex.Lock()
TopLevelEnvironments.Environments[name] = env
TopLevelEnvironments.Mutex.Unlock()
}
return env
}
func NewSymbolTableFrameBelowWithFrame(p *SymbolTableFrame, f *FrameMap, name string) *SymbolTableFrame {
if f == nil {
f = p.Frame
}
restricted := p != nil && p.IsRestricted
env := &SymbolTableFrame{Name: name, Parent: p, Bindings: make(map[string]*Binding, 10), Frame: f, CurrentCode: list.New(), IsRestricted: restricted}
if p == nil || p == Global {
TopLevelEnvironments.Mutex.Lock()
TopLevelEnvironments.Environments[name] = env
TopLevelEnvironments.Mutex.Unlock()
}
return env
}
func (self *SymbolTableFrame) HasFrame() bool {
return self.Frame != nil
}
func (self *SymbolTableFrame) BindingNamed(name string) (b *Binding, present bool) {
self.Mutex.RLock()
b, present = self.Bindings[name]
self.Mutex.RUnlock()
return
}
func (self *SymbolTableFrame) SetBindingAt(name string, b *Binding) {
self.Mutex.Lock()
self.Bindings[name] = b
self.Mutex.Unlock()
}
func (self *SymbolTableFrame) DeleteBinding(name string) {
self.Mutex.Lock()
delete(self.Bindings, name)
self.Mutex.Unlock()
}
func (self *SymbolTableFrame) findSymbol(name string) (symbol *Data, found bool) {
binding, found := self.BindingNamed(name)
if found {
return binding.Sym, true
} else if self.Parent != nil {
return self.Parent.findSymbol(name)
} else {
return nil, false
}
}
func (self *SymbolTableFrame) FindBindingFor(symbol *Data) (binding *Binding, found bool) {
name := StringValue(symbol)
binding, found = self.BindingNamed(name)
if found {
return
} else if self.Parent != nil {
return self.Parent.FindBindingFor(symbol)
} else {
return nil, false
}
}
func (self *SymbolTableFrame) BindTo(symbol *Data, value *Data) (*Data, error) {
binding, found := self.FindBindingFor(symbol)
if found {
if binding.Protected {
return nil, fmt.Errorf("%s is a protected binding", StringValue(symbol))
}
binding.Val = value
} else {
binding = BindingWithSymbolAndValue(symbol, value)
self.SetBindingAt(StringValue(symbol), binding)
}
return binding.Val, nil
}
func (self *SymbolTableFrame) BindToProtected(symbol *Data, value *Data) *Data {
binding, found := self.FindBindingFor(symbol)
if found {
binding.Val = value
binding.Protected = true
} else {
binding = ProtectedBindingWithSymbolAndValue(symbol, value)
self.SetBindingAt(StringValue(symbol), binding)
}
return binding.Val
}
func (self *SymbolTableFrame) SetTo(symbol *Data, value *Data) (result *Data, err error) {
localBinding, found := self.findBindingInLocalFrameFor(symbol)
if found {
if localBinding.Protected {
return nil, fmt.Errorf("%s is a protected binding", StringValue(symbol))
} else {
localBinding.Val = value
return value, nil
}
}
naked := StringValue(NakedSymbolFrom(symbol))
if self.HasFrame() && self.Frame.HasSlot(naked) {
self.Frame.Set(naked, value)
return value, nil
}
binding, found := self.FindBindingFor(symbol)
if found {
if binding.Protected {
return nil, fmt.Errorf("%s is a protected binding", StringValue(symbol))
} else {
binding.Val = value
return value, nil
}
}
return nil, errors.New(fmt.Sprintf("%s is undefined", StringValue(symbol)))
}
func (self *SymbolTableFrame) findBindingInLocalFrameFor(symbol *Data) (b *Binding, found bool) {
return self.BindingNamed(StringValue(symbol))
}
func (self *SymbolTableFrame) BindLocallyTo(symbol *Data, value *Data) (*Data, error) {
binding, found := self.findBindingInLocalFrameFor(symbol)
if found {
if binding.Protected {
return nil, fmt.Errorf("%s is a protected binding", StringValue(symbol))
}
binding.Val = value
} else {
binding = BindingWithSymbolAndValue(symbol, value)
self.SetBindingAt(StringValue(symbol), binding)
}
return binding.Val, nil
}
func (self *SymbolTableFrame) ValueOfWithFunctionSlotCheck(symbol *Data, needFunction bool) *Data {
localBinding, found := self.findBindingInLocalFrameFor(symbol)
if found {
if FunctionP(localBinding.Val) {
atomic.StoreInt32(&FunctionValue(localBinding.Val).SlotFunction, 1)
}
return localBinding.Val
}
if self.HasFrame() {
f := self.Frame
naked := StringValue(NakedSymbolFrom(symbol))
if f.HasSlot(naked) {
slotValue := f.Get(naked)
if FunctionP(slotValue) {
atomic.StoreInt32(&FunctionValue(slotValue).SlotFunction, 1)
return slotValue
}
if !needFunction {
return slotValue
}
}
}
binding, found := self.FindBindingFor(symbol)
if found {
if FunctionP(binding.Val) {
atomic.StoreInt32(&FunctionValue(binding.Val).SlotFunction, 0)
}
return binding.Val
} else {
return EmptyCons()
}
}
func (self *SymbolTableFrame) ValueOf(symbol *Data) *Data {
return self.ValueOfWithFunctionSlotCheck(symbol, false)
}