forked from mgeck64/pcalc-mgeck64.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
225 lines (206 loc) · 6.26 KB
/
parser.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
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Lexer = Me.imports.lexer.Lexer;
const {
TOK_UNKNOWN, TOK_END, TOK_NUM, TOK_ADD, TOK_SUB, TOK_MUL, TOK_DIV, TOK_EXP,
TOK_LPAREN, TOK_RPAREN, TOK_IDENT, TOK_COMMA
} = Me.imports.lexer;
// Parser exceptions------------------------------------------------------------
// note: if add or delete an exception then update imports in calculator.js
const NoExpression = class NoExpression {};
const UndefinedIdent = class UndefinedIdent {
constructor (ident) {
this.ident = ident; // identifier string
}
};
const CantConvertNumber = class CantConvertNumber {
constructor (numStr) {
this.numStr = numStr;
}
};
// Parser-----------------------------------------------------------------------
// evaluates expressions
const Parser = class Parser {
constructor () {
this._last_val = 0.0;
}
eval (expr) {
this._lexer = new Lexer(expr);
if (this._lexer.peekToken().id === TOK_END) {
throw new NoExpression();
}
const val = this._expression();
if (this._lexer.peekToken().id !== TOK_END) {
throw new SyntaxError();
}
this._last_val = val;
return val;
}
_expression () {
// <_expression> ::= <_multiplicitiveExpr> [ ( "+" | "-" ) <_multiplicitiveExpr> ]...
let lhs = this._multiplicitiveExpr();
for (;;) {
if (this._lexer.peekToken().id === TOK_ADD) {
this._lexer.getToken();
lhs += this._multiplicitiveExpr();
} else if (this._lexer.peekToken().id === TOK_SUB) {
this._lexer.getToken();
lhs -= this._multiplicitiveExpr();
} else {
break;
}
}
return lhs;
}
_multiplicitiveExpr () {
// <_multiplicitiveExpr> ::= <_exponentialExpr> [ ( "*" | "/" ) <_exponentialExpr> ]...
let lhs = this._exponentialExpr();
for (;;) {
if (this._lexer.peekToken().id === TOK_MUL) {
this._lexer.getToken();
lhs *= this._exponentialExpr();
} else if (this._lexer.peekToken().id === TOK_DIV) {
this._lexer.getToken();
lhs /= this._exponentialExpr();
} else {
break;
}
}
return lhs;
}
_exponentialExpr () {
// <_exponentialExpr> ::= ( "+" | "-" ) <_exponentialExpr>
// | <_baseExpr> [ ( "**" | "^" ) <_exponentialExpr> ]
// note: exponentation is right-associative
if (this._lexer.peekToken().id === TOK_ADD) {
this._lexer.getToken();
return +this._exponentialExpr();
}
if (this._lexer.peekToken().id === TOK_SUB) {
this._lexer.getToken();
return -this._exponentialExpr();
}
let lhs = this._baseExpr();
if (this._lexer.peekToken().id === TOK_EXP) {
this._lexer.getToken();
lhs **= this._exponentialExpr();
}
return lhs;
}
_baseExpr () {
// <_baseExpr> ::= <number> | <_groupExpr> | <_identExpr>
const id = this._lexer.peekToken().id;
if (id === TOK_NUM) { // <number>
const val = +this._lexer.peekToken().val; // converts to internal numeric
if (isNaN(val)) {
throw new CantConvertNumber(this._lexer.peekToken().val);
}
this._lexer.getToken();
return val;
}
if (id === TOK_LPAREN) {
return this._groupExpr();
}
return this._identExpr();
}
_groupExpr () {
// <_groupExpr> ::= "(" <_expression> ")"
if (this._lexer.getToken().id !== TOK_LPAREN) {
throw new SyntaxError();
}
const val = this._expression();
if (this._lexer.getToken().id !== TOK_RPAREN) {
throw new SyntaxError();
}
return val;
}
_emptyGroupExpr () {
// <_emptyGroupExpr> ::= "()"
if (this._lexer.getToken().id !== TOK_LPAREN || this._lexer.getToken().id !== TOK_RPAREN) {
throw new SyntaxError();
}
}
_binaryGroupExpr () {
if (this._lexer.getToken().id !== TOK_LPAREN) {
throw new SyntaxError();
}
const arg1 = this._expression();
if (this._lexer.getToken().id !== TOK_COMMA) {
throw new SyntaxError();
}
const arg2 = this._expression();
if (this._lexer.getToken().id !== TOK_RPAREN) {
throw new SyntaxError();
}
return { arg1, arg2 };
}
_identExpr () {
// <_identExpr> ::= "pi" | "e" | "last" | <function call>
// <function call> ::= <nullary fn ident> <_emptyGroupExpr>
// | <unary fn ident> <_groupExpr>
// | <binary fn ident> <_binaryGroupExpr>
const token = this._lexer.getToken();
if (token.id !== TOK_IDENT) {
throw new SyntaxError();
}
switch (token.val) {
case 'pi':
return Math.PI;
case 'e':
return Math.E;
case 'last':
return this._last_val;
case 'abs':
return Math.abs(this._groupExpr());
case 'acos':
return Math.acos(this._groupExpr());
case 'acosh':
return Math.acosh(this._groupExpr());
case 'asin':
return Math.asin(this._groupExpr());
case 'asinh':
return Math.asinh(this._groupExpr());
case 'atan':
return Math.atan(this._groupExpr());
case 'atan2': {
const args = this._binaryGroupExpr();
return Math.atan2(args.arg1, args.arg2);
}
case 'atanh':
return Math.atanh(this._groupExpr());
case 'cbrt':
return Math.cbrt(this._groupExpr());
case 'ceil':
return Math.ceil(this._groupExpr());
case 'cos':
return Math.cos(this._groupExpr());
case 'cosh':
return Math.cosh(this._groupExpr());
case 'exp':
return Math.exp(this._groupExpr());
case 'floor':
return Math.floor(this._groupExpr());
case 'log':
case 'ln':
return Math.log(this._groupExpr());
case 'random':
this._emptyGroupExpr(); return Math.random();
case 'round':
return Math.round(this._groupExpr());
case 'sin':
return Math.sin(this._groupExpr());
case 'sinh':
return Math.sinh(this._groupExpr());
case 'sqrt':
return Math.sqrt(this._groupExpr());
case 'tan':
return Math.tan(this._groupExpr());
case 'tanh':
return Math.tanh(this._groupExpr());
case 'trunc':
return Math.trunc(this._groupExpr());
default:
throw new UndefinedIdent(token.val);
}
}
};