/
local_variable_stack.go
61 lines (48 loc) · 1.2 KB
/
local_variable_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
package vm
import (
"errors"
"fmt"
"github.com/grubby/grubby/interpreter/vm/builtins"
)
type frame map[string]builtins.Value
type LocalVariableStack struct {
frames []frame
}
func NewLocalVariableStack() *LocalVariableStack {
return &LocalVariableStack{
frames: make([]frame, 0),
}
}
func newEmptyFrame() []frame {
return []frame{{}}
}
func (stack *LocalVariableStack) Unshift() {
stack.frames = append(newEmptyFrame(), stack.frames...)
}
func (stack *LocalVariableStack) Shift() {
stack.frames = stack.frames[0 : len(stack.frames)-1]
}
func (stack *LocalVariableStack) UnshiftCopyingCurrentFrame() {
var lastFrame frame
if len(stack.frames) >= 1 {
lastFrame = stack.frames[0]
} else {
lastFrame = newEmptyFrame()[0]
}
newFrame := newEmptyFrame()
for key, value := range lastFrame {
newFrame[0][key] = value
}
stack.frames = append(newFrame, stack.frames...)
}
func (stack *LocalVariableStack) Store(key string, value builtins.Value) {
stack.frames[0][key] = value
}
func (stack *LocalVariableStack) Retrieve(key string) (builtins.Value, error) {
maybe, ok := stack.frames[0][key]
if !ok {
return nil, errors.New(fmt.Sprintf("No such key '%s'", key))
} else {
return maybe, nil
}
}