-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
245 lines (202 loc) · 6.01 KB
/
main.js
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const AST_TYPE = {
'MULTIPLICATIVEEXPRESSION': 'MultiplicativeExpression',
'ADDITIVEEXPRESSION': 'AdditiveExpression',
'NUMBER': 'Number',
'FACTOR': 'Factor',
'EXPRESSION': 'Expression',
}
const LEX_TYPE = {
'NUMBER': 'Number',
'OPERATOR': 'Operator',
'BRACKETS': 'Brackets',
'EOF': Symbol.for('EOF'),
}
const tokens = []
const emitTokens = (type, value, index) => {
tokens.push({
type, value, index
})
}
const numberOrOperator = (() => {
let token = []
return (char, index) => {
if (/^[0-9]$/.test(char)) {
token.push(char)
return numberOrOperator
} else if (token.length === 0 && char === '-'){
token.push(char)
return numberOrOperator
}else{
if (token.length === 1 && token[0] === '-'){
emitTokens(LEX_TYPE.OPERATOR, token[0], index-1 )
}else{
emitTokens(LEX_TYPE.NUMBER, token.join(''), index )
}
token = []
return start(char)
}
}
})()
const token = []
const start = (char, index) => {
if (/^[0-9\-]$/.test(char)) {
return numberOrOperator(char)
}
if (/^\+$/.test(char)) {
emitTokens(LEX_TYPE.OPERATOR, char, index)
return start
}
if (/^\*$/.test(char)) {
emitTokens(LEX_TYPE.OPERATOR, char, index)
return start
}
if (/^\/$/.test(char)) {
emitTokens(LEX_TYPE.OPERATOR, char, index)
return start
}
if (/^\n$/.test(char)) {
emitTokens(LEX_TYPE.EOF, LEX_TYPE.EOF, index)
}
return start
}
/**
* BSL Define
* <Expression> ::=
* <AdditiveExpression><EOF>
*
* <AdditiveExpression> ::=
* <MultiplicativeExpression> | <AdditiveExpression><+><MultiplicativeExpression> | <AdditiveExpression><-><MultiplicativeExpression>
*
* <MultiplicativeExpression> ::=
* <Factor> | <MultiplicativeExpression><*><Factor> | <MultiplicativeExpression></><Factor>
*
* <Factor> ::=
* <Number> | <(><AdditiveExpression><)>
*/
const Expression = source => {
if (source[0].type === AST_TYPE.ADDITIVEEXPRESSION && source[1].type === Symbol.for('EOF')) {
const node = {
type: AST_TYPE.EXPRESSION,
children: [
source.shift(),
source.shift(),
]
}
source.unshift()
return node
}
AdditiveExpression(source)
return Expression(source)
}
const AdditiveExpression = source => {
if (source[0].type === AST_TYPE.MULTIPLICATIVEEXPRESSION) {
const node = {
type: AST_TYPE.ADDITIVEEXPRESSION,
children: [source[0]]
}
source[0] = node
return AdditiveExpression(source)
}
if (source[0].type === AST_TYPE.ADDITIVEEXPRESSION && source[1] && source[1].type === LEX_TYPE.OPERATOR && (source[1].value === '+' || source[1].value === '-')) {
const node = {
type: AST_TYPE.ADDITIVEEXPRESSION,
operator: source[1].value,
children: [
source.shift(),
source.shift(),
]
}
MultiplicativeExpression(source)
if (source[0].type !== AST_TYPE.MULTIPLICATIVEEXPRESSION) {
throw new TypeError(`Unexpected token '${source[0].value === LEX_TYPE.EOF ? 'EOF' : source[0].value}' at the ${source[0].index}st character, it should be a Number or other expressions`)
}
node.children.push(source.shift())
source.unshift(node)
return AdditiveExpression(source)
}
if (source[0].type === AST_TYPE.ADDITIVEEXPRESSION) {
return source[0]
}
MultiplicativeExpression(source)
if (source[0].type === LEX_TYPE.OPERATOR){
throw new TypeError(`Unexpected token '${source[0].value}' at the ${source[0].index}st character, the operator must be preceded by a number`)
}
return AdditiveExpression(source)
}
const MultiplicativeExpression = source => {
if (source[0].type === AST_TYPE.NUMBER) {
const node = {
type: AST_TYPE.MULTIPLICATIVEEXPRESSION,
children: [source[0]]
}
source[0] = node
return MultiplicativeExpression(source)
}
if (source[0].type === AST_TYPE.MULTIPLICATIVEEXPRESSION && source[1] && source[1].type === LEX_TYPE.OPERATOR && (source[1].value === '*' || source[1].value === '/')) {
if (source[2].type !== LEX_TYPE.NUMBER) {
throw new TypeError(`Unexpected token '${source[2].value}' at the ${source[2].index}st character, it should be a Number`)
}
const node = {
type: AST_TYPE.MULTIPLICATIVEEXPRESSION,
operator: source[1].value,
children: [
source.shift(),
source.shift(),
source.shift()
]
}
source.unshift(node)
return MultiplicativeExpression(source)
}
if (source[0].type === AST_TYPE.MULTIPLICATIVEEXPRESSION) {
return source[0]
}
}
const Factor = source =>{
if (source[0].type === AST_TYPE.NUMBER) {
const node = {
type: AST_TYPE.FACTOR,
children: [source[0]]
}
source[0] = node
return Factor(source)
}
}
const evaluate = node => {
switch (node.type) {
case AST_TYPE.EXPRESSION:
return evaluate(node.children[0])
case AST_TYPE.ADDITIVEEXPRESSION:
if (node.operator === '+') {
return evaluate(node.children[0]) + evaluate(node.children[2])
}
if (node.operator === '-') {
return evaluate(node.children[0]) - evaluate(node.children[2])
}
return evaluate(node.children[0])
case AST_TYPE.MULTIPLICATIVEEXPRESSION:
if (node.operator === '*') {
return evaluate(node.children[0]) * evaluate(node.children[2])
}
if (node.operator === '/') {
return evaluate(node.children[0]) / evaluate(node.children[2])
}
return evaluate(node.children[0])
case AST_TYPE.NUMBER:
return Number(node.value)
default:
throw new Error(`Unhandled AST type: ${node && node.type}`)
}
}
const complexCase = '2 + 32 * 44 + 4 / 54 * 4 - 654\n'
const MUTCase = '4 / 3 * 34 * 2 /33 * 4 * 3 \n'
const errCase = '2 + 3 -\n'
const negativeCase = '-1 + 1 - -1 + -1\n'
const negativeErrorCase = '--1'
const testCase = negativeErrorCase
for (let state = start, index = 0; index < testCase.length; index++) {
const char = testCase[index];
state = state(char, index)
}
const ast = Expression(tokens)
console.log(evaluate(ast))