-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.ts
More file actions
207 lines (171 loc) · 4.84 KB
/
Copy pathtokenizer.ts
File metadata and controls
207 lines (171 loc) · 4.84 KB
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
export interface Token<T> {
value: T;
toString: () => string;
}
export type BinaryOperatorSymbol = "-" | "+" | "*" | "/" | "^";
type ParenthesisSymbol = "(" | ")";
export class TNum implements Token<number> {
value;
constructor(value: number) {
this.value = value;
}
toString() {
return this.value.toString();
}
}
export class TBinaryOperator implements Token<BinaryOperatorSymbol> {
value;
constructor(value: BinaryOperatorSymbol) {
this.value = value;
}
toString() {
return this.value;
}
}
export class TParenthesis implements Token<ParenthesisSymbol> {
value;
constructor(value: ParenthesisSymbol) {
this.value = value;
}
toString() {
return this.value;
}
}
const grammar = {
num: /^([0-9]*[.])?[0-9]+/,
binaryOperator: /^[\-\+\*\/\^]/,
parenthesis: /^[\(\)]/,
space: /^(\s+)/,
};
enum OperatorPrecedence {
Low,
Medium,
High,
}
const getOperatorPrecendence = (
op: BinaryOperatorSymbol,
): OperatorPrecedence => {
if (op === "^") {
return OperatorPrecedence.High;
} else if (op === "*" || op === "/") {
return OperatorPrecedence.Medium;
} else {
return OperatorPrecedence.Low;
}
};
/**
* Converts tokens in infix notation to tokens in postfix notation
*/
export const convertToPostfix = (tokens: Token<any>[]): Token<any>[] => {
const stack: Token<any>[] = [];
const postfixTokens: Token<any>[] = [];
tokens.forEach((t) => {
if (t instanceof TNum) {
postfixTokens.push(t);
} else if (t instanceof TParenthesis) {
const isOpeningParen = t.value === "(";
const isClosingParen = t.value === ")";
// @TODO: handle case when there's closing paren but no open paren
// @TODO: handle case when there's open paren but no closing paren
if (isOpeningParen) {
stack.push(t);
return;
}
if (isClosingParen) {
let operatorOrParen = stack.pop();
while (operatorOrParen) {
if (operatorOrParen.value === "(") {
break;
}
if (operatorOrParen.value === ")") {
throw Error(
"whoops. We found ) while searching for (. But we shouldn't found it. Looks like arithmetic expression is wrong",
);
}
postfixTokens.push(operatorOrParen);
operatorOrParen = stack.pop();
}
}
} else if (t instanceof TBinaryOperator) {
// @TODO: handle two ^^
const isEmpty = stack.length == 0;
const precedence = getOperatorPrecendence(t.value);
if (isEmpty) {
stack.push(t);
} else {
const lastTokenInStack = stack[stack.length - 1];
if (lastTokenInStack.value === "(") {
stack.push(t);
return;
}
const lastTokenPrecedence = getOperatorPrecendence(
lastTokenInStack.value,
);
if (precedence > lastTokenPrecedence) {
stack.push(t);
} else if (precedence <= lastTokenPrecedence) {
let poppedOp = stack.pop();
while (poppedOp) {
let poppedOpPrecedence = getOperatorPrecendence(poppedOp.value);
if (poppedOpPrecedence < precedence) {
break;
}
postfixTokens.push(poppedOp);
const last = stack[stack.length - 1];
if (last && last.value === "(") {
// terminate while loop if we encountered (
// we don't want to pop out of parenthesis
break;
}
poppedOp = stack.pop();
}
stack.push(t);
}
}
}
});
if (stack.length !== 0) {
let popped = stack.pop();
while (popped) {
postfixTokens.push(popped);
popped = stack.pop();
}
}
return postfixTokens;
};
// @TODO handle space. As tokens? As raws?
export const tokenize = (str: string) => {
const loop = (s: string, tokens: Token<any>[]): Token<any>[] => {
if (s.length <= 0) {
return tokens;
}
if (grammar.space.exec(s)) {
const [val] = grammar.space.exec(s) as RegExpExecArray;
return loop(s.substring(val.length), [...tokens]);
}
if (grammar.num.exec(s)) {
const [val] = grammar.num.exec(s) as RegExpExecArray;
return loop(s.substring(val.length), [
...tokens,
new TNum(parseFloat(val)),
]);
}
if (grammar.binaryOperator.exec(s)) {
const [val] = grammar.binaryOperator.exec(s) as RegExpExecArray;
return loop(s.substring(1), [
...tokens,
new TBinaryOperator(val as BinaryOperatorSymbol),
]);
}
if (grammar.parenthesis.exec(s)) {
const [val] = grammar.parenthesis.exec(s) as RegExpExecArray;
return loop(s.substring(1), [
...tokens,
new TParenthesis(val as ParenthesisSymbol),
]);
}
throw Error(`unknown token: ${s}`);
};
return loop(str, []);
};
export default tokenize;