-
Notifications
You must be signed in to change notification settings - Fork 4
/
NumJS.Parser.js
284 lines (254 loc) · 7.14 KB
/
NumJS.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* NumJS -- a JavaScript library for numerical computing
*
* Copyright (C) 2011 Clifford Wolf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// "use strict";
NumJS.Parse = function(text, args, defs)
{
var code = "(function(" + (typeof(args) != "undefined" ? args : "") + "){\n";
var idx = 0;
if (typeof(defs) != "undefined") {
for (i in defs)
code += "var " + i + " = " + defs[i] + ";\n";
}
// lexical pass: used to identify literals and extract 'non-trivial'
// literals. I.e. literals that contain '+' and '-' such as "1.23e-5"
// that would confuse the actual parser.
var tokens = text.match(/(\.?[0-9][0-9.]*e[+-]?[0-9]+i?|[a-zA-Z_0-9.]+|.)/g);
text = "";
for (var i in tokens) {
if (tokens[i].search(/^\.?[0-9][0-9.]*e[+-]?[0-9]+i?$/) >= 0) {
text += "$" + idx;
code += "var $" + (idx++) + " = ";
if (tokens[i].search(/(.*)i$/) >= 0)
code += "NumJS.C(0, " + RegExp.$1 + ");\n";
else
code += tokens[i] + ";\n";
} else
text += tokens[i];
}
// this function reduces a textual expression to
// a single variable index and returns the corrspondig
// basic "$n" expression, writing the actual js code
// to the code variable on the way.
function reduce(text)
{
var pos;
while (1)
{
// blocks and function calls
pos = text.search(/[a-zA-Z_0-9.]*\(/);
if (pos >= 0)
{
var prefix = text.slice(0, pos);
var postfix = text.slice(pos);
var funcname = postfix.match(/^[a-zA-Z_0-9.]*/)[0];
postfix = postfix.slice(funcname.length + 1);
var i, pcount = 1;
for (i = 0; pcount > 0; i++) {
if (i >= postfix.length)
throw "NumJS.Parse parser error.";
var ch = postfix.substr(i, 1);
if (ch == "(")
pcount++;
if (ch == ")")
pcount--;
}
var inner = postfix.slice(0, i-1);
postfix = postfix.slice(i);
// handle a function call
if (funcname != "")
{
var fcall = funcname + "(";
if (inner != "") {
var start = 0;
for (i = 0; i < inner.length; i++) {
var ch = inner.substr(i, 1);
if (ch == "(")
pcount++;
if (ch == ")")
pcount--;
if (ch == "," && pcount == 0) {
var arg = inner.slice(start, i);
if (start != 0)
fcall += ", ";
fcall += reduce(arg);
start = i + 1;
}
}
var arg = inner.slice(start, i);
if (start != 0)
fcall += ", ";
fcall += reduce(arg);
}
text = prefix + "$" + idx + postfix;
code += "var $" + (idx++) + " = " + fcall + ");\n";
continue;
}
text = prefix + reduce(inner) + postfix;
continue;
}
// matrix notation
pos = text.search(/\[/);
if (pos >= 0)
{
var prefix = text.slice(0, pos);
var postfix = text.slice(pos + 1);
var i, pcount = 1;
for (i = 0; pcount > 0; i++) {
if (i >= postfix.length)
throw "NumJS.Parse parser error.";
var ch = postfix.substr(i, 1);
if (ch == "[")
pcount++;
if (ch == "]")
pcount--;
}
var inner = postfix.slice(0, i-1);
postfix = postfix.slice(i);
var start = 0;
var cellData = new Object();
var rowNum = 0, colNum = 0;
var rowId = 0, colId = 0;
function addCell(txt, term) {
if (rowId >= rowNum)
rowNum = rowId + 1;
if (colId >= colNum)
colNum = colId + 1;
cellData[rowId + " " + colId] = reduce(txt);
if (term == ",")
colId++;
if (term == ";")
rowId++, colId = 0;
}
for (i = 0; i < inner.length; i++) {
var ch = inner.substr(i, 1);
if (ch == "[")
pcount++;
if (ch == "]")
pcount--;
if ((ch == "," || ch == ";") && pcount == 0) {
addCell(inner.slice(start, i), ch);
start = i + 1;
}
}
if (start != i)
addCell(inner.slice(start, i), ch);
code += "var $" + idx + " = NumJS.MAT(" + rowNum + ", " + colNum + ", [";
for (var i = 0; i < rowNum; i++)
for (var j = 0; j < colNum; j++) {
if (i != 0 || j != 0)
code += ", ";
if ((i + " " + j) in cellData)
code += cellData[i + " " + j];
else
code += "0";
}
code += " ]);\n";
text = prefix + "$" + (idx++) + postfix;
continue;
}
// prefix '+' and '-'
pos = text.search(/(^|[^$a-zA-Z_0-9.])(\+|-)([$a-zA-Z_0-9.]+)/);
if (pos >= 0)
{
pos += RegExp.$1.length;
var prefix = text.slice(0, pos);
var op = RegExp.$2
pos += op.length;
var val = RegExp.$3
pos += val.length;
var postfix = text.slice(pos);
val = reduce(val);
text = prefix + "$" + idx + postfix;
code += "var $" + (idx++) + " = ";
if (op == "+")
code += val + ";\n";
if (op == "-")
code += "NumJS.NEG(" + val + ");\n";
continue;
}
// multiply and divide
pos = text.search(/([$a-zA-Z_0-9.]+)(\*|\/|\\)([$a-zA-Z_0-9.]+)/);
if (pos >= 0)
{
var prefix = text.slice(0, pos);
var val1 = RegExp.$1;
pos += val1.length;
var op = RegExp.$2
pos += op.length;
var val2 = RegExp.$3
pos += val2.length;
var postfix = text.slice(pos);
val1 = reduce(val1);
val2 = reduce(val2);
text = prefix + "$" + idx + postfix;
code += "var $" + (idx++) + " = ";
if (op == "*")
code += "NumJS.MUL";
if (op == ".")
code += "NumJS.DOT";
if (op == "/")
code += "NumJS.DIV";
if (op == "\\")
code += "NumJS.SOLVE";
code += "(" + val1 + ", " + val2 + ");\n";
continue;
}
// add and subtract
pos = text.search(/([$a-zA-Z_0-9.]+)(\+|-)([$a-zA-Z_0-9.]+)/);
if (pos >= 0)
{
var prefix = text.slice(0, pos);
var val1 = RegExp.$1;
pos += val1.length;
var op = RegExp.$2
pos += op.length;
var val2 = RegExp.$3
pos += val2.length;
var postfix = text.slice(pos);
val1 = reduce(val1);
val2 = reduce(val2);
text = prefix + "$" + idx + postfix;
code += "var $" + (idx++) + " = ";
if (op == "+")
code += "NumJS.ADD";
if (op == "-")
code += "NumJS.SUB";
code += "(" + val1 + ", " + val2 + ");\n";
continue;
}
// handle imaginary literals
pos = text.search(/^([0-9][0-9.]*)i$/);
if (pos >= 0) {
code += "var $" + idx + " = NumJS.C(0, " + RegExp.$1 + ");\n";
return "$" + (idx++);
}
// we are done
pos = text.search(/^[$a-zA-Z_0-9.]+$/);
if (pos >= 0)
return text;
throw "NumJS.Parse parser error.";
}
}
text = text.replace(new RegExp("[ \t\r\n]", "g"), "");
var result = reduce(text);
code += "return " + result + ";\n})";
return code;
};