-
Notifications
You must be signed in to change notification settings - Fork 1
/
expr.go
170 lines (150 loc) · 3.41 KB
/
expr.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
package gmars
import (
"fmt"
gtoken "go/token"
"go/types"
"strconv"
)
func expandValue(key string, values, resolved map[string][]token, graph map[string][]string) ([]token, error) {
// load key value or error
value, valOk := values[key]
if !valOk {
return nil, fmt.Errorf("symbol '%s' key not found", key)
}
// return resolved value if exists. on principle
if res, ok := resolved[key]; ok {
return res, nil
}
// recursively expand dependent values if not already resolved
deps, ok := graph[key]
if ok {
for _, dep := range deps {
_, resOk := resolved[dep]
if !resOk {
_, err := expandValue(dep, values, resolved, graph)
if err != nil {
return nil, err
}
}
}
}
// create new token slice and append tokens from the symbol value
// while replacing reference tokens with their resolved values
output := make([]token, 0)
for _, token := range value {
if token.typ == tokText {
depVal, depOk := resolved[token.val]
if depOk {
// variable names will be resolved
output = append(output, depVal...)
} else {
// otherwise it is a label
output = append(output, token)
}
} else {
output = append(output, token)
}
}
resolved[key] = output
return output, nil
}
func expandExpressions(values map[string][]token, graph map[string][]string) (map[string][]token, error) {
resolved := make(map[string][]token)
for key := range values {
_, ok := resolved[key]
if ok {
continue
}
expanded, err := expandValue(key, values, resolved, graph)
if err != nil {
return nil, err
}
resolved[key] = expanded
}
return resolved, nil
}
func combineSigns(expr []token) []token {
out := make([]token, 0, len(expr))
lastOut := token{typ: tokEOF}
// please forgive me for this lol
for i := 0; i < len(expr); i++ {
if lastOut.typ == tokSymbol {
negativeFound := false
for ; i < len(expr); i++ {
if !(expr[i].val == "-" || expr[i].val == "+") {
break
}
if expr[i].val == "-" {
negativeFound = true
}
}
if negativeFound {
out = append(out, token{tokSymbol, "-"})
}
if i < len(expr) {
out = append(out, expr[i])
lastOut = expr[i]
}
} else {
if i < len(expr) {
out = append(out, expr[i])
lastOut = expr[i]
}
}
}
return out
}
func flipDoubleNegatives(expr []token) []token {
out := make([]token, 0, len(expr))
for i := 0; i < len(expr); i++ {
if expr[i].val == "-" {
if i+1 < len(expr) && expr[i+1].val == "-" {
out = append(out, token{tokSymbol, "+"})
i += 1
continue
}
}
out = append(out, expr[i])
}
return out
}
func evaluateExpression(expr []token) (int, error) {
for _, tok := range expr {
if tok.typ == tokText || !tok.IsExpressionTerm() {
return 0, fmt.Errorf("unexpected token in expressoin: '%s'", tok)
}
}
combinedExpr := combineSigns(expr)
flippedExpr := flipDoubleNegatives(combinedExpr)
exprStr := ""
for _, tok := range flippedExpr {
exprStr += tok.val
}
fs := gtoken.NewFileSet()
tv, err := types.Eval(fs, nil, gtoken.NoPos, exprStr)
if err != nil {
return 0, err
}
valStr := tv.Value.String()
if valStr == "true" {
return 1, nil
} else if valStr == "false" {
return 0, nil
}
val, err := strconv.ParseInt(valStr, 10, 32)
if err != nil {
return 0, err
}
return int(val), nil
}
func exprEqual(a, b []token) bool {
if len(a) != len(b) {
return false
}
for i, aval := range a {
if b[i] != aval {
return false
}
}
return true
}