-
Notifications
You must be signed in to change notification settings - Fork 5
/
basic.go
214 lines (170 loc) · 3.54 KB
/
basic.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
package exec
import (
qlang "qlang.io/spec"
)
// -----------------------------------------------------------------------------
// Rem
type iRem struct {
File string
Line int
Code string
}
func (p *iRem) Exec(stk *Stack, ctx *Context) {}
func Rem(file string, line int, code string) Instr {
return &iRem{file, line, code}
}
// -----------------------------------------------------------------------------
// Push/Pop
var (
OnPop func(v interface{})
)
type iPush struct {
v interface{}
}
type iPop int
type iPopEx int
func (p *iPush) Exec(stk *Stack, ctx *Context) {
stk.Push(p.v)
}
func (p iPop) Exec(stk *Stack, ctx *Context) {
stk.Pop()
}
func (p iPopEx) Exec(stk *Stack, ctx *Context) {
v, _ := stk.Pop()
OnPop(v)
}
func Push(v interface{}) Instr {
return &iPush{v}
}
func PopEx() Instr {
if OnPop != nil {
return popEx
}
return Pop
}
var (
Nil Instr = Push(nil)
Pop Instr = iPop(0)
popEx Instr = iPopEx(0)
)
// -----------------------------------------------------------------------------
// Clear
type iClear int
func (p iClear) Exec(stk *Stack, ctx *Context) {
stk.data = stk.data[:ctx.base]
}
var (
Clear Instr = iClear(0)
)
// -----------------------------------------------------------------------------
// Or/And
type iOr int
type iAnd int
func (delta iOr) Exec(stk *Stack, ctx *Context) {
a, _ := stk.Pop()
if a1, ok := a.(bool); ok {
if a1 {
stk.Push(true)
ctx.ip += int(delta)
}
} else {
panic("left operand of || operator isn't a boolean expression")
}
}
func (delta iAnd) Exec(stk *Stack, ctx *Context) {
a, _ := stk.Pop()
if a1, ok := a.(bool); ok {
if !a1 {
stk.Push(false)
ctx.ip += int(delta)
}
} else {
panic("left operand of && operator isn't a boolean expression")
}
}
func Or(delta int) Instr {
return iOr(delta)
}
func And(delta int) Instr {
return iAnd(delta)
}
// -----------------------------------------------------------------------------
// Jmp
type iJmp int
func (delta iJmp) Exec(stk *Stack, ctx *Context) {
ctx.ip += int(delta)
}
func Jmp(delta int) Instr {
return iJmp(delta)
}
// -----------------------------------------------------------------------------
// JmpIfFalse
type iJmpIfFalse int
func (delta iJmpIfFalse) Exec(stk *Stack, ctx *Context) {
a, _ := stk.Pop()
if a1, ok := a.(bool); ok {
if !a1 {
ctx.ip += int(delta)
}
} else {
panic("condition isn't a boolean expression")
}
}
func JmpIfFalse(delta int) Instr {
return iJmpIfFalse(delta)
}
// -----------------------------------------------------------------------------
// Case/Default
type iCase int
func (delta iCase) Exec(stk *Stack, ctx *Context) {
b, _ := stk.Pop()
a, _ := stk.Top()
cond := qlang.EQ(a, b)
if cond1, ok := cond.(bool); ok {
if cond1 {
stk.Pop()
} else {
ctx.ip += int(delta)
}
} else {
panic("operator == return non-boolean value?")
}
}
func Case(delta int) Instr {
return iCase(delta)
}
var (
Default Instr = Pop
)
// -----------------------------------------------------------------------------
// SubSlice
type iOp3 struct {
op func(v, a, b interface{}) interface{}
arity int
hasA bool
hasB bool
}
func (p *iOp3) Exec(stk *Stack, ctx *Context) {
var i = 1
var a, b interface{}
args := stk.PopNArgs(p.arity)
if p.hasA {
a = args[i]
i++
}
if p.hasB {
b = args[i]
}
stk.Push(p.op(args[0], a, b))
}
func Op3(op func(v, a, b interface{}) interface{}, hasA, hasB bool) Instr {
n := 1
if hasA {
n++
}
if hasB {
n++
}
return &iOp3{op, n, hasA, hasB}
}
// -----------------------------------------------------------------------------