-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatement.go
177 lines (151 loc) · 3.51 KB
/
statement.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
package main
import (
"fmt"
)
const (
BoiOpCall = 1
BoiOpIf = 2
BoiOpLoop = 3
BoiOpFuncDef = 4
)
type BoiStatement struct {
Operation int
Tokens []Token
Children []*BoiStatement
}
func NewCallStatement(fname string, tokens []Token) *BoiStatement {
functionToken := Token{
BoiType: BoiTokenValue,
BoiValue: []byte(fname),
BoiSource: BoiSourceLocal,
}
tokens = append([]Token{functionToken}, tokens...)
return &BoiStatement{
BoiOpCall, tokens, nil,
}
}
func (boi *BoiInterpreter) ExecStmt(stmt *BoiStatement) error {
switch stmt.Operation {
case BoiOpCall:
if len(stmt.Tokens) < 1 {
return fmt.Errorf("boi! must have at least one token")
}
args := []BoiVar{}
for _, tok := range stmt.Tokens {
value, _ := boi.getValueOf(tok)
args = append(args, value)
}
identifier := string(args[0].data)
return boi.Call(identifier, args[1:])
case BoiOpIf:
if len(stmt.Tokens) < 1 {
return fmt.Errorf("boi? must have at least one token")
}
args := []BoiVar{}
for _, tok := range stmt.Tokens {
value, _ := boi.getValueOf(tok)
args = append(args, value)
}
// Call statement
identifier := string(args[0].data)
err := boi.Call(identifier, args[1:])
if err != nil {
return err
}
// Execute subsequent statements if output is true
exitVar, exists := boi.context.returnCtx.variables["exit"]
// Check for falsy values
switch true {
case !exists:
fallthrough
case len(exitVar.data) == 0:
fallthrough
case string(exitVar.data) == "false":
// Falsey value encountered - do not execute aggregate statements
return nil
}
// Scope down for aggregate statements
boi.subContext()
defer boi.returnContext()
// Execute aggregate statements
for _, stmt := range stmt.Children {
if stmt != nil {
err := boi.ExecStmt(stmt)
if err != nil {
return err
}
}
}
return nil
case BoiOpLoop:
if len(stmt.Tokens) < 1 {
return fmt.Errorf("bloop must have at least one token")
}
continueLoop := true
for continueLoop {
// Recalculate arguments
args := []BoiVar{}
for _, tok := range stmt.Tokens {
value, _ := boi.getValueOf(tok)
args = append(args, value)
}
// Call statement
identifier := string(args[0].data)
err := boi.Call(identifier, args[1:])
if err != nil {
return err
}
// Execute subsequent statements if output is true
exitVar, exists := boi.context.returnCtx.variables["exit"]
// Check for falsy values
switch true {
case !exists:
fallthrough
case len(exitVar.data) == 0:
fallthrough
case string(exitVar.data) == "false":
// Falsey value encountered - do not execute aggregate statements
continueLoop = false
}
if !continueLoop {
break
}
// Scope down for aggregate statements
err = func() error {
boi.subContext()
defer boi.returnContext()
// Execute aggregate statements
for _, stmt := range stmt.Children {
if stmt != nil {
err := boi.ExecStmt(stmt)
if err != nil {
return err
}
}
}
return nil
}()
if err != nil {
return err
}
}
return nil
case BoiOpFuncDef:
var identifier string
if len(stmt.Tokens) < 1 {
identifier = ""
} else {
identifierBoi, _ := boi.getValueOf(stmt.Tokens[0])
identifier = string(identifierBoi.data)
}
boi.RegisterGoFunctionStruct(
identifier,
NewBoiStatementsFunction(stmt.Children, boi),
)
return nil
}
return fmt.Errorf(
"internal error (aka boi is broken): invalid op code: %d",
stmt.Operation,
)
}