-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
46 lines (40 loc) · 910 Bytes
/
main.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
package mathparse
func NewParser(expression string) Parser {
parse := Parser{}
parse.ReadExpression(expression)
return parse
}
func (p *Parser) FoundResult() bool {
return len(p.tokens) <= 1 && p.tokens[0].Type == literal
}
func (p *Parser) GetValueResult() float64 {
return p.tokens[0].ParseValue
}
func (p *Parser) GetExpressionResult() string {
return getStringExpression(p.tokens)
}
func getStringExpression(set []Token) string {
str := ""
for _, tok := range set {
switch tok.Type {
case space:
str += " "
case literal:
str += tok.Value
case variable:
str += tok.Value
case operation:
str += tok.Value
case function:
str += tok.Value + "(" + getStringExpression(tok.Children) + ")"
case lparen:
str += "(" + getStringExpression(tok.Children) + ")"
case funcDelim:
str += ","
}
}
return str
}
func (p *Parser) GetTokens() []Token {
return p.tokens
}