-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.go
93 lines (78 loc) · 1.44 KB
/
extractor.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
package equation
import (
"strconv"
)
func selectBlock(reader Reader) string {
result := ""
count := 0
current, _ := reader(1, true)
if current != "(" {
reader(1, false)
return current
}
for {
next, _ := reader(1, false)
if next == "" {
break
}
if next == ")" {
count = count - 1
if count == 0 {
break
}
result = result + next
} else if next == "(" {
if count != 0 {
result = result + next
}
count = count + 1
} else if count != 0 {
result = result + next
}
}
return result
}
func isNumber(str string) bool {
_, err := strconv.ParseFloat(str, 64)
return err == nil
}
type sign struct {
symbol string
innerExpression string
startIndex int
endIndex int
}
func extractOperators(reader Reader) []sign {
result := make([]sign, 0)
for {
current, index := reader(1, false)
if current == "" {
break
}
if is := isNumber(current); is {
continue
}
prev, _ := reader(-1, true)
if isNumber(prev) || prev == ")" {
result = append(result, sign{
symbol: current,
innerExpression: "",
startIndex: index,
endIndex: index,
})
} else {
if current == "(" {
reader(-1, false)
}
inner := selectBlock(reader)
_, end := reader(0, true)
result = append(result, sign{
symbol: current,
innerExpression: inner,
startIndex: index,
endIndex: end,
})
}
}
return result
}