forked from zdebeer99/goexpression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.go
221 lines (180 loc) · 4.41 KB
/
tokens.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
package goexpression
import (
"fmt"
"strconv"
"strings"
)
type Token interface {
Category() TokenCategory
SetError(err error)
Error() error
String() string
}
type TokenCategory int
const (
CatOther TokenCategory = iota
CatFunction
CatValue
)
type EmptyToken struct {
tokencat TokenCategory
err error
}
func NewEmptyToken() *EmptyToken {
return &EmptyToken{CatOther, nil}
}
func (this *EmptyToken) Category() TokenCategory {
return this.tokencat
}
func (this *EmptyToken) Error() error {
return this.err
}
func (this *EmptyToken) SetError(err error) {
this.err = err
}
func (this *EmptyToken) String() string {
return "Base()"
}
type ErrorToken struct {
EmptyToken
}
func NewErrorToken(err string) *ErrorToken {
return &ErrorToken{EmptyToken{CatOther, fmt.Errorf(err)}}
}
type NumberToken struct {
EmptyToken
Value float64
}
func NewNumberToken(value string) *NumberToken {
node := &NumberToken{EmptyToken{CatValue, nil}, 0}
val1, err := strconv.ParseFloat(value, 64)
if err != nil {
panic("Number node failed to parse string to number. (" + value + ")")
return node
}
node.Value = val1
return node
}
func (this *NumberToken) String() string {
return fmt.Sprintf("Number(%v)", this.Value)
}
type IdentityToken struct {
EmptyToken
Name string
}
func NewIdentityToken(name string) *IdentityToken {
return &IdentityToken{EmptyToken{CatValue, nil}, name}
}
func (this *IdentityToken) String() string {
return fmt.Sprintf("Identity(%s)", this.Name)
}
type FuncToken struct {
EmptyToken
Name string
Arguments []*TreeNode
}
func NewFuncToken(name string) *FuncToken {
return &FuncToken{EmptyToken{CatFunction, nil}, name, make([]*TreeNode, 0)}
}
func (this *FuncToken) AddArgument(arg *TreeNode) {
this.Arguments = append(this.Arguments, arg)
}
func (this *FuncToken) String() string {
args := make([]string, len(this.Arguments))
for i, v := range this.Arguments {
args[i] = fmt.Sprintf("%s", strings.Replace(v.String(), "\n", ",", -1))
}
return fmt.Sprintf("Func %s(%s)", this.Name, args)
}
type OperatorToken struct {
EmptyToken
Operator string
lvl int
}
func NewOperatorToken(operator string) *OperatorToken {
op := &OperatorToken{EmptyToken{CatFunction, nil}, "", -1}
op.SetOperator(operator)
return op
}
func (this *OperatorToken) SetOperator(operator string) {
this.Operator = operator
this.lvl = operators.Level(operator)
if this.lvl < 0 {
panic(fmt.Errorf("Invalid Operator %q", operator))
}
}
// OperatorPrecedence return true if the operator argument is lower than the current operator.
func (this *OperatorToken) Precedence(operator string) int {
lvl := operators.Level(operator)
switch {
case lvl == this.lvl:
return 0
case lvl > this.lvl:
return 1
case lvl < this.lvl:
return -1
}
panic("Unreachable code")
}
func (this *OperatorToken) String() string {
return fmt.Sprintf("Func(%s)", this.Operator)
}
type OperatorPrecedence [][]string
func (this OperatorPrecedence) Level(operator string) int {
for level, operators := range this {
for _, op := range operators {
if op == operator {
return 5 - level
}
}
}
return -1
}
func (this OperatorPrecedence) All() []string {
out := make([]string, 0)
for _, operators := range this {
for _, op := range operators {
out = append(out, op)
}
}
return out
}
var operators OperatorPrecedence = OperatorPrecedence{
{"^"},
{"*", "/", "%"},
{"+", "-"},
{"==", "!=", ">", "<", ">=", "<="},
{"&&", "and"},
{"||", "or"},
}
var operatorList []string = operators.All()
type LRFuncToken struct {
EmptyToken
Name string
}
func NewLRFuncToken(name string) *LRFuncToken {
return &LRFuncToken{EmptyToken{CatFunction, nil}, name}
}
func (this *LRFuncToken) String() string {
return fmt.Sprintf("Func(%s)", this.Name)
}
type GroupToken struct {
EmptyToken
GroupType string
}
func NewGroupToken(group string) *GroupToken {
return &GroupToken{EmptyToken{CatOther, nil}, group}
}
func (this *GroupToken) String() string {
return fmt.Sprintf("Group(%s)", this.GroupType)
}
type TextToken struct {
EmptyToken
Text string
}
func NewTextToken(text string) *TextToken {
return &TextToken{EmptyToken{CatValue, nil}, text}
}
func (this *TextToken) String() string {
return fmt.Sprintf("%q", this.Text)
}