-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokenise.go
204 lines (184 loc) · 3.98 KB
/
tokenise.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
package mathparse
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type Parser struct {
letterBuffer string
numberBuffer string
expression string
tokens []Token
}
type Token struct {
Type TokenType
Value string
ParseValue float64
Children []Token
}
type TokenType uint
const (
undefined TokenType = iota // 0
space // 1
literal // 2
variable // 3
operation // 4
function // 5
lparen // 6
rparen // 7
funcDelim // 8
)
// "89sin(45) + 2.2x/7"
func (p *Parser) ReadExpression(str string) {
p.expression = str
p.tokens = []Token{}
p.tokenise()
}
func (p *Parser) ReadMultipartExpression(str []string) {
p.expression = strings.Join(str, " ")
p.tokens = []Token{}
p.tokenise()
}
func (p *Parser) tokenise() {
dumpLetter := func(p *Parser) {
for i, ch := range p.letterBuffer {
p.tokens = append(p.tokens, newToken(variable, string(ch)))
if i < len(p.letterBuffer)-1 {
p.tokens = append(p.tokens, newToken(operation, "*"))
}
}
p.letterBuffer = ""
}
dumpNumber := func(p *Parser) {
if len(p.numberBuffer) > 0 {
p.tokens = append(p.tokens, newToken(literal, p.numberBuffer))
p.numberBuffer = ""
}
}
for _, ch := range p.expression {
switch getTokenType(ch) {
case space:
continue
case literal:
p.numberBuffer += string(ch)
case variable:
if len(p.numberBuffer) > 0 {
dumpNumber(p)
p.tokens = append(p.tokens, newToken(operation, "*"))
}
p.letterBuffer += string(ch)
case operation:
dumpNumber(p)
dumpLetter(p)
p.tokens = append(p.tokens, newToken(operation, string(ch)))
case lparen:
if len(p.numberBuffer) > 0 {
dumpNumber(p)
p.tokens = append(p.tokens, newToken(operation, "*"))
}
if len(p.letterBuffer) > 0 {
p.tokens = append(p.tokens, newToken(function, p.letterBuffer))
p.letterBuffer = ""
}
p.tokens = append(p.tokens, newToken(lparen, "("))
case rparen:
dumpLetter(p)
dumpNumber(p)
p.tokens = append(p.tokens, newToken(rparen, ")"))
case funcDelim:
dumpNumber(p)
dumpLetter(p)
p.tokens = append(p.tokens, newToken(funcDelim, ","))
}
}
if len(p.numberBuffer) > 0 {
dumpNumber(p)
}
if len(p.letterBuffer) > 0 {
dumpLetter(p)
}
p.tokens, _ = buildTree(p.tokens)
}
func buildTree(set []Token) ([]Token, int) {
toks := []Token{}
for i := 0; i < len(set); i++ {
tok := set[i]
switch tok.Type {
case function:
child, offset := buildTree(set[i+2:])
tok.Children = child
i += 2 + offset
toks = append(toks, tok)
case lparen:
child, offset := buildTree(set[i+1:])
tok.Children = child
toks = append(toks, tok)
i += 1 + offset
case rparen:
// toks = append(toks, tok)
return toks, i
default:
toks = append(toks, tok)
}
}
return toks, len(set)
}
func newToken(typ TokenType, value string) Token {
tok := Token{
Type: typ,
Value: value,
}
if typ == literal {
tok.ParseValue, _ = strconv.ParseFloat(value, 64)
}
return tok
}
func getTokenType(ch rune) TokenType {
let := string(ch)
if let == " " {
return space
} else if isDigit(let) {
return literal
} else if isLetter(let) {
return variable
} else if isOperator(let) {
return operation
} else if isOpenParen(let) {
return lparen
} else if isCloseParen(let) {
return rparen
}
return undefined
}
func isComma(let string) bool {
return let == ","
}
func isDigit(let string) bool {
res, err := regexp.MatchString(`[0-9\.]`, let)
if err != nil {
fmt.Print(err)
}
return res
}
func isLetter(let string) bool {
res, err := regexp.MatchString("[a-zA-Z]", let)
if err != nil {
fmt.Print(err)
}
return res
}
func isOperator(let string) bool {
res, err := regexp.MatchString(`\*|\/|\+|\^|\-`, let)
// "\x43|\x47|\x42|\x94|\x45"
if err != nil {
fmt.Print(err)
}
return res
}
func isOpenParen(let string) bool {
return let == "("
}
func isCloseParen(let string) bool {
return let == ")"
}