/
stack.go
278 lines (236 loc) · 6.19 KB
/
stack.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
package zygo
import (
"fmt"
"strings"
)
type StackElem interface {
IsStackElem()
}
type Stack struct {
tos int
elements []StackElem
env *Zlisp
Name string // type name
// package support:
PackageName string
IsPackage bool
}
func (s *Stack) SexpString(ps *PrintState) string {
if ps == nil {
ps = NewPrintState()
}
var label string
head := ""
if s.IsPackage {
head = "(package " + s.PackageName
} else {
label = "scope " + s.Name
}
str, err := s.Show(s.env, ps, s.Name)
if err != nil {
return "(" + label + ")"
}
return head + " " + str + " )"
}
// Type() satisfies the Sexp interface, returning the type of the value.
func (s *Stack) Type() *RegisteredType {
return GoStructRegistry.Lookup("packageScopeStack")
}
func (env *Zlisp) NewStack(size int) *Stack {
return &Stack{
tos: -1,
// elements: make([]StackElem, size),
elements: make([]StackElem, 0),
env: env,
}
}
func (stack *Stack) Clone() *Stack {
ret := &Stack{}
ret.tos = stack.tos
ret.env = stack.env
ret.elements = make([]StackElem, len(stack.elements))
for i := range stack.elements {
ret.elements[i] = stack.elements[i]
}
return ret
}
func (stack *Stack) Top() int {
return stack.tos
}
func (stack *Stack) PushAllTo(target *Stack) int {
if stack.tos < 0 {
return 0
}
for _, v := range stack.elements[0 : stack.tos+1] {
target.Push(v)
}
return stack.tos + 1
}
func (stack *Stack) IsEmpty() bool {
return stack.tos < 0
}
func (stack *Stack) Push(elem StackElem) {
// we have to lazily recover from errors here where the stack
// didn't get unwound...
n := len(stack.elements)
// n-1 is the last legal entry, which should be top of stack too.
switch {
case stack.tos == n-1:
// normal, 99% of the time.
stack.tos++
stack.elements = append(stack.elements, elem)
case stack.tos > n-1:
// really irretreivably problematic
panic(fmt.Sprintf("stack %p is really messed up! starting size=%v > "+
"len(stack.elements)=%v:\n here is stack: '%s'\n",
stack, stack.tos-1, n, stack.SexpString(nil)))
default:
// INVAR stack.tos < n-1
// We might get here if an error caused the last operation to abort,
// resulting in the call stack pop upon returning never happening.
// So we'll lazily cleanup now and carry on.
stack.TruncateToSize(stack.tos + 1)
stack.tos++
stack.elements = append(stack.elements, elem)
}
}
func (stack *Stack) GetTop() StackElem {
s, err := stack.Get(0)
if err != nil {
panic(err)
}
return s
}
func (stack *Stack) Size() int {
return stack.tos + 1
}
var StackUnderFlowErr = fmt.Errorf("invalid stack access: underflow")
func (stack *Stack) Get(n int) (StackElem, error) {
if stack.tos-n < 0 {
err := StackUnderFlowErr
return nil, err
}
return stack.elements[stack.tos-n], nil
}
func (stack *Stack) Pop() (StackElem, error) {
// always make a new array,
// so we can use for the closure stack-of-scopes.
elem, err := stack.Get(0)
if err != nil {
return nil, err
}
// invar n > 0
n := stack.Size()
if n == 0 {
return nil, fmt.Errorf("Stack.Pop() on emtpy stack")
}
el := make([]StackElem, n-1)
copy(el, stack.elements)
stack.elements = el
stack.tos--
return elem, nil
}
func (stack *Stack) IsStackElem() {}
func (stack *Stack) Show(env *Zlisp, ps *PrintState, label string) (string, error) {
//P("debug: Stack.Show starting with stack = %p, ps = %p, Package: '%s', IsPkg: %v", stack, ps, stack.PackageName, stack.IsPackage)
if ps.GetSeen(stack) {
return fmt.Sprintf("already-saw Stack %p in Show", stack), nil
} else {
ps.SetSeen(stack, "Stack in Show")
}
s := ""
rep := strings.Repeat(" ", ps.GetIndent())
s += fmt.Sprintf("%s %s\n", rep, label)
n := stack.Top()
for i := 0; i <= n; i++ {
ele, err := stack.Get(n - i)
if err != nil {
panic(fmt.Errorf("stack access error on %v: %v", i, err))
}
showme, canshow := ele.(Showable)
if canshow {
r, err := showme.Show(env, ps.AddIndent(4),
fmt.Sprintf("elem %v of %s:", i, label))
if err != nil {
return "", err
}
s += r
}
}
return s, nil
}
// set newsize to 0 to truncate everything
func (stack *Stack) TruncateToSize(newsize int) {
el := make([]StackElem, newsize)
copy(el, stack.elements)
stack.elements = el
stack.tos = newsize - 1
}
// nestedPathGetSet does a top-down lookup, as opposed to LexicalLookupSymbol which is bottom up
func (s *Stack) nestedPathGetSet(env *Zlisp, dotpaths []string, setVal *Sexp) (Sexp, error) {
if len(dotpaths) == 0 {
return SexpNull, fmt.Errorf("internal error: in nestedPathGetSet() dotpaths" +
" had zero length")
}
curStack := s
var ret Sexp = SexpNull
var err error
var scop *Scope
lenpath := len(dotpaths)
//P("\n in nestedPathGetSet, dotpaths=%#v\n", dotpaths)
for i := range dotpaths {
curSym := env.MakeSymbol(stripAnyDotPrefix(dotpaths[i]))
if !curStack.IsPackage {
return SexpNull, fmt.Errorf("error locating symbol '%s': current Stack is not a package", curSym.name)
}
ret, err, scop = curStack.LookupSymbol(curSym, nil)
if err != nil {
return SexpNull, fmt.Errorf("could not find symbol '%s' in current package '%v'",
curSym.name, curStack.PackageName)
}
if setVal != nil && i == lenpath-1 {
// check if private
err = errIfPrivate(curSym.name, curStack)
if err != nil {
return SexpNull, err
}
// assign now
scop.Map[curSym.number] = *setVal
// done with SET
return *setVal, nil
}
if i == lenpath-1 {
// final element
switch ret.(type) {
case *Stack:
// allow package within package to be inspected.
// done with GET
return ret, nil
default:
// don't allow private value within package to be inspected.
err = errIfPrivate(curSym.name, curStack)
if err != nil {
return SexpNull, err
}
}
// done with GET
return ret, nil
}
// invar: i < lenpath-1, so go deeper
switch x := ret.(type) {
case *SexpHash:
err = errIfPrivate(curSym.name, curStack)
if err != nil {
return SexpNull, err
}
//P("\n found hash in x at i=%d, looping to next i\n", i)
return x.nestedPathGetSet(env, dotpaths[1:], setVal)
case *Stack:
curStack = x
default:
return SexpNull, fmt.Errorf("not a record or scope: cannot get field '%s'"+
" out of type %T)", dotpaths[i+1][1:], x)
}
}
return ret, nil
}