-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
154 lines (132 loc) · 3.57 KB
/
parser.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
package rocket
import(
"strconv"
)
var precedanceTable = map[string]Operator{
"STAR": {3, "LEFT"},
"SLASH": {3, "LEFT"},
"PLUS": {2, "LEFT"},
"MIN": {2, "LEFT"},
"RIGHT_PARENTHESES": {110, "LEFT"},
"LEFT_PARENTHESES": {110, "LEFT"},
}
func NewParser() *Parser {
return &Parser{
units: UnitTable{},
}
}
func (p *Parser) Run(ln Program) int {
units := lex(ln)
p.units = units;
return p.parse()
}
func (p *Parser) toPostFix() UnitTable {
outputQue := UnitTable{}
operatorStack := UnitTable{}
//evaluate to postfix notation
for i := 0; i < len(p.units); i++ {
currentUnit := p.units[i]
switch currentUnit.unitType {
case "INTERGER":
outputQue = append(outputQue, currentUnit)
break
case "OPERATOR":
if len(operatorStack) >= 1 && operatorStack.parenIsNotTop() {
for len(operatorStack) != 0 {
if operatorStack.top().hasHigherPrecedance(currentUnit) && operatorStack.top().unitType != "SYMBOL"{
outputQue = append(outputQue, operatorStack.top())
operatorStack.pop()
} else {
break
}
}
}
operatorStack = append(operatorStack, currentUnit)
break
case "SYMBOL": //find a way to refacor this
switch currentUnit.symbolType() {
case "LEFT_PARENTHESES":
operatorStack = append(operatorStack, currentUnit)
break
case "RIGHT_PARENTHESES":
if(operatorStack.top().symbolType() != "LEFT_PARENTHESES") {
for operatorStack.top().symbolType() != "LEFT_PARENTHESES" {
outputQue = append(outputQue, operatorStack.top())
operatorStack.pop()
}
} else {
operatorStack.pop()
operatorStack.pop()
break
}
}
break
}
}
for len(operatorStack) != 0 {
outputQue = append(outputQue, operatorStack[len(operatorStack)-1])
operatorStack.pop()
}
return outputQue
}
func (stack UnitTable) parenIsNotTop() bool {
return stack[len(stack)-1].unitType != "SYMBOL"
}
func (unit Unit) symbolType() string {
v := OneCharacterSymbols.getBaseUnit(unit.cargo).notation
return v
}
func postFixToOutcome (postFix UnitTable) int {
stack := UnitTable{}
for i := 0; i < len(postFix); i++ {
currentUnit := postFix[i]
switch currentUnit.unitType {
case "INTERGER":
stack = append(stack, currentUnit)
break
case "OPERATOR":
temp := stack.top()
stack.pop()
res := exec(temp, currentUnit, stack.top())
stack.pop()
stack = append(stack, res)
break
}
}
result, _ :=strconv.Atoi(stack.top().cargo)
return result
}
func (p *Parser) parse() int {
postFix := p.toPostFix()
return postFixToOutcome(postFix)
//evaluate postix to outcome
}
func exec(right Unit, operator Unit, left Unit) Unit {
rhs, _ := strconv.Atoi(right.cargo)
lhs, _ := strconv.Atoi(left.cargo)
switch operator.cargo {
case "+":
return Unit{strconv.Itoa(lhs + rhs), "INTERGER", "INTERGER"}
case "-":
return Unit{strconv.Itoa(lhs - rhs), "INTERGER", "INTERGER"}
case "*":
return Unit{strconv.Itoa(lhs * rhs), "INTERGER", "INTERGER"}
case "/":
return Unit{strconv.Itoa(lhs / rhs), "INTERGER", "INTERGER"}
}
return Unit{"0", "0", "0"}
}
func (stack UnitTable) top() Unit {
if(len(stack) < 1) {
panic("top of empty stack")
}
return stack[len(stack)-1]
}
func (s *UnitTable) pop() {
*s = (*s)[:len(*s)-1]
}
func (unitA Unit) hasHigherPrecedance(unitB Unit) bool {
operatorA := precedanceTable[unitA.notation]
operatorB := precedanceTable[unitB.notation]
return (operatorA.association == "LEFT" && operatorB.precedence <= operatorA.precedence)|| (operatorA.association == "RIGHT" && operatorB.precedence > operatorA.precedence)
}