-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
228 lines (194 loc) · 6.6 KB
/
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
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
222
223
224
225
226
227
228
package main
import (
"fmt"
"log"
"strconv"
)
type state int
const (
STAT_0 state = iota
STAT_NUM
)
type unit struct {
eletype string
lit string
}
func generate_units(str string) []unit {
units := []unit{}
word := ""
current_state := STAT_0
if str == "" {
return units
}
i := 0
for {
if i >= len(str) {
if word != "" {
units = append(units, unit{eletype:"ET_NUM", lit:word})
word = ""
}
units = append(units, unit{eletype: "ET_EOF", lit:""})
return units
}
current_char := str[i]
fmt.Printf("current_char: %c\n", current_char)
switch current_state {
case STAT_0:
if current_char == '(' {
units = append(units, unit{eletype: "ET_SEP", lit:"("})
} else if current_char == ')' {
units = append(units, unit{eletype: "ET_SEP", lit:")"})
} else if current_char == '+' {
units = append(units, unit{eletype: "ET_OP", lit:"+"})
} else if current_char == '-' {
units = append(units, unit{eletype: "ET_OP", lit:"-"})
} else if current_char == '*' {
units = append(units, unit{eletype: "ET_OP", lit:"*"})
} else if current_char == '/' {
units = append(units, unit{eletype: "ET_OP", lit:"/"})
} else if current_char >= '0' && current_char <= '9' {
word += string(current_char)
current_state = STAT_NUM
}
case STAT_NUM:
if current_char >= '0' && current_char <= '9' {
word += string(current_char)
} else {
units = append(units, unit{eletype: "ET_NUM", lit:word})
current_state = STAT_0
word = ""
i--
}
}
i++
}
}
// <Exp> ::= <Exp> + <Term> | <Exp> - <Term> | <Term>
// <Term> ::= <Term> * <Factor> | <Term> / <Factor> | <Factor>
// <Factor> ::= x | y | ... | ( <Exp> ) | - <Factor>
type Expresstion struct {
op string
left *Term
right *Expresstion
}
func (e *Expresstion) value() int {
if e.op == "+" {
return e.left.value() + e.right.value()
} else if e.op == "-" {
return e.left.value() - e.right.value()
} else if e.op == "" {
return e.left.value()
}
return 0
}
type Term struct {
op string
left *Factor
right *Term
}
func (t *Term) value() int {
if t.op == "*" {
return t.left.value() * t.right.value()
} else if t.op == "/" {
return t.left.value() / t.right.value()
} else if t.op == "" {
return t.left.value()
}
return 0
}
type Factor struct {
kind int // 0 -> int value, 1 -> expresstion, 2 -> - Factor
val int
quotedExpresstion *Expresstion
}
func (f *Factor) value() int {
if f.kind == 0 {
return f.val
} else if f.kind == 1 {
return f.quotedExpresstion.value()
}
return 0
}
func parse_E(units []unit) (*Expresstion, []unit) {
log.Printf("Enter the parse_E, rest_units = %v\n", units)
if len(units) == 0 {
return nil, []unit{}
}
var prightExpress *Expresstion
var rest_units1 []unit
pterm, rest_units := parse_T(units)
if len(rest_units) == 1 {
return &Expresstion{op:"", left:pterm, right: nil}, rest_units
}
if rest_units[0].eletype == "ET_OP" && (rest_units[0].lit == "+") {
prightExpress, rest_units1 = parse_E(rest_units[1:])
log.Printf("Exit parse_E, rest_units = %v\n", rest_units)
return &Expresstion{op:rest_units[0].lit, left: pterm, right: prightExpress}, rest_units1
} else if rest_units[0].eletype == "ET_OP" && (rest_units[0].lit == "-") {
prightTerm, rest_units2 := parse_T(rest_units[1:])
fuckExpresstion := &Expresstion{op:"", left: prightTerm, right: nil}
totalLeft := &Expresstion{op:rest_units[0].lit, left: pterm, right: fuckExpresstion}
finalFctor := &Factor{kind: 1, val: 0, quotedExpresstion: totalLeft}
finalTerm := &Term{op:"", left: finalFctor, right: nil}
ptotalTerm, rest_final := parse_E(rest_units2[1:])
return &Expresstion{op:rest_units2[0].lit, left: finalTerm, right: ptotalTerm}, rest_final
} else {
log.Printf("Error parse_E, rest_units = %v\n", rest_units)
return &Expresstion{op:"", left: pterm, right: nil}, rest_units
}
}
func parse_T(units []unit) (*Term, []unit) {
log.Printf("Enter the parse_T, rest_units = %v\n", units)
if len(units) == 0 {
return nil, []unit{}
}
var prightTerm *Term
var rest_units1 []unit
pfactor, rest_units := parse_F(units)
if len(rest_units) == 1 {
return &Term{op: "", left: pfactor, right: nil}, rest_units
}
if rest_units[0].eletype == "ET_OP" && (rest_units[0].lit == "*" || rest_units[0].lit == "/") {
prightTerm, rest_units1 = parse_T(rest_units[1:])
log.Printf("Exit the parse_T, rest_units = %v\n", rest_units)
return &Term{op: rest_units[0].lit, left: pfactor, right: prightTerm}, rest_units1
} else {
log.Printf("Error the parse_T, rest_units = %v\n", rest_units)
return &Term{op: "", left: pfactor, right: nil}, rest_units
}
}
func parse_F(units []unit) (*Factor, []unit) {
log.Printf("Enter parse_F, rest_units = %v\n", units)
if len(units) == 0 {
return nil, []unit{}
}
current_unit := units[0]
if current_unit.eletype == "ET_EOF" {
return nil, []unit{}
}
if current_unit.eletype == "ET_NUM" {
v, err := strconv.Atoi(current_unit.lit)
if err != nil {
log.Fatalf("parse_F error: %s\n", current_unit.lit)
}
log.Printf("Exit the parse_F, rest_units = %v\n", units[1:])
return &Factor{kind: 0, val: v, quotedExpresstion: nil}, units[1:]
} else if current_unit.eletype == "ET_SEP" && current_unit.lit == "(" {
pexpresstion, rest_units := parse_E(units[1:])
if rest_units[0].lit == ")" {
log.Printf("Exit the parse_F, rest_units = %v\n", units[1:])
return &Factor{kind: 1, val: 0, quotedExpresstion: pexpresstion}, rest_units[1:]
} else {
log.Printf("Exit the parse_F, rest_units = %v\n", units[1:])
return nil, rest_units
}
}
return nil, units
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
units := generate_units("1-(2+5)*(11-1+1)")
pexpresstion, rest_units := parse_E(units)
fmt.Printf("%v, %v\n", *pexpresstion, rest_units)
fmt.Printf("eval result = %v\n", pexpresstion.value())
}