-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
445 lines (363 loc) · 10.4 KB
/
app.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var SpaceTime_FunctionsDIR = './SpaceTime_Functions/';
var SpaceTime_coreFile = 'map.js';
console.log('{src f} src -f-> ??');
console.log('');
console.log('SpaceTime modlue loading...');
//var M = require(SpaceTime_FunctionsDIR + SpaceTime_coreFile);
//require(STRING) must be raw STRING to be read by Browserify
var M = require('./SpaceTime_Functions/map.js');
module.exports = M;
console.log('Function: map(core)');
var loadModulesFactory;
if (typeof window === 'undefined')
{
var modulePathHiddenFromBrowserify = './loadModulesFactory';
loadModulesFactory = require(modulePathHiddenFromBrowserify);
}
else
{
loadModulesFactory = require('./loadModulesFactoryBrowserify');
}
var obj = loadModulesFactory(SpaceTime_FunctionsDIR, SpaceTime_coreFile, M);
M = obj.M;
var loadModules = M.loadModules = obj.func;
//=====================================
String.prototype.replaceAll = function(org, dest)
{
return this
.split(org)
.join(dest);
};
var sqSymbol = '_____sq_____';
var sqKey = 'sq___';
//==========================
var cmtprts = ['//', '/*', '*/', '(', ')', '\n', ' ', ' '];
var cmtprtsE = cmtprts.map(function(s, i)
{
return '______mark______' + i;
});
//==========================
var restoreMark = function(src)
{
// "thisisareplacement"+i restore to normal string
var src1 = [];
cmtprtsE.map(function(mark, mark_i)
{
var data;
if (mark_i === 0)
{
data = src;
}
else
{
data = src1[mark_i - 1];
}
src1[mark_i] = data.replaceAll(mark, cmtprts[mark_i]);
});
return src1[src1.length - 1];
};
var trim = function(src)
{
var strgs = src.match(/"(?:[^\\"]|\\.)*"/ig);
var strgs1 = [];
var src1 = [];
var src2;
// escape cmtprts during each Strings
if (strgs)
{
strgs.map(function(str, str_i)
{
var str1 = [];
cmtprts.map(function(mark, mark_i)
{
var data;
if (mark_i === 0)
{
data = str;
}
else
{
data = str1[mark_i - 1];
}
str1[mark_i] = data.replaceAll(mark, cmtprtsE[mark_i]);
});
strgs1[str_i] = str1[str1.length - 1];
var data1;
if (str_i === 0)
{
data1 = src;
}
else
{
data1 = src1[str_i - 1];
}
src1[str_i] = data1.replaceAll(strgs[str_i], strgs1[str_i]);
});
src2 = src1[src1.length - 1];
}
else
{
src2 = src;
}
// comment out removing
var re2 = new RegExp('//.*?(?=[\\n\\r]+|$)|/[*](.|\n)*?[*]/', 'g');
var src3 = src2
.replace(re2, '');
//beautifly
var src4 = src3.replaceAll(' ', ' '); //zenkaku>hankaku
var src5 = src4.replaceAll('\n', ' '); //linebreak -> single space
var src6 = src5.replace(/(\S)\(/g, '$1 ('); //foo( -> foo (
var src7 = src6.replace(/\)(\S)/g, ') $1'); //)foo -> ) foo
var src8 = src7.replace(/[ \s]+/g, ' '); // trim extra spaces
var src9 = src8.replace(/(\()\s+|\s+(\))/g, '$1$2'); //trim a space after '(' and before ')'.
var src10 = src9.replace(/(\[)\s+|\s+(\])/g, '$1$2'); //trim a space after '[' and before ']'.
return src10;
};
var arrays = function(src)
{
M.$W('------------- parse ----------------');
M.$W(src);
var f1 = function(src)
{
// M.$W('some seq');
// M.$W(src);
var indexHead;
var indexTail;
var count = [];
var previousCount;
var space = [];
for (var i = 0; i < src.length; i++)
{
// wt('> ' + src[i]);
if (i === 0)
previousCount = 0;
else
previousCount = count[i - 1];
if ((previousCount === 1) && (src[i] === ' '))
space[space.length] = i;
if (src[i] === '(')
count[i] = previousCount + 1;
else if (src[i] === ')')
count[i] = previousCount - 1;
else
count[i] = previousCount;
if ((previousCount === 0) && (count[i] === 1))
{
indexHead = i;
}
if ((previousCount === 1) && (count[i] === 0))
{
indexTail = i;
}
// wt('count ' + count[i]);
}
// M.$W(indexHead);
// M.$W(indexTail);
// M.$W(space);
var src1 = src.substring(indexHead + 1, indexTail);
var array = [];
if (space.length === 0)
{
array[0] = arrays(src1);
// M.$W('---return');
// M.$W(array);
return array;
}
else
{
for (var j = 0; j < space.length; j++)
{
if (j === 0)
{
array[array.length] = arrays(src.substring(indexHead + 1, space[j]));
}
if (j === space.length - 1)
{
array[array.length] = arrays(src.substring(space[j] + 1, indexTail));
}
else
{
array[array.length] = arrays(src.substring(space[j] + 1, space[j + 1]));
}
}
// M.$W('---return');
// M.$W(array);
return array;
}
};
//------------------
if (src === '')
return src;
else if (!src)
return src;
else if (src.indexOf('(') === -1)
{
return src;
}
else
{
var count = 0;
var otherCharactor = false;
for (var i = 0; i < src.length; i++)
{
if (src[i] === '(')
{
count++;
}
else if (src[i] === ')')
{
count--;
}
else if (src[i] === ' ')
{
}
else
{
otherCharactor = true;
}
}
if (count !== 0)
{
return 'error';
}
else if (otherCharactor)
{
return f1(src);
}
else
{
return [];
}
}
};
var maybeNumberString = function(src)
{
console.log('maybeNumberString');
console.log(src);
var s1 = src * 1;
var s2 = '' + s1;
if (src === s2) // naked number
{
return s1;
}
else if (src.indexOf('"') !== -1) // '"some string"'
{
return restoreMark(src.substring(1, src.length - 1));
}
else
{
var src1;
if (src === '+')
src1 = 'plus';
else if (src === '-')
src1 = 'minus';
else if (src === 'if')
src1 = 'ifF';
else if (src === '==')
src1 = 'boolEqual';
else if (src === '>')
src1 = 'boolGreater';
else if (src === '<')
src1 = 'boolLess';
else if (src === '>=')
src1 = 'boolGreaterEqual';
else if (src === '<=')
src1 = 'boolLessEqual';
else
src1 = src;
console.log(src1);
return M[src1];
}
};
var walk = function(src)
{
// console.log('=======');
// console.log(src);
if (M.$type(src) !== 'Array')
{
return maybeNumberString(src);
}
else
{
var array = [];
for (var i = 0; i < src.length; i++)
{
// console.log('-------');
// console.log(src[i]);
array[i] = walk(src[i]);
}
return array;
}
};
var $parse = M.$parse = function(src)
{
return walk(arrays(trim(src)));
};
//======================================
var $construct = M.$construct = function(result)
{
var result1;
if (M.$type(result) === 'Array')
{
result1 = '( ';
for (var i = 0; i < result.length; i++)
{
result1 += $construct(result[i]);
result1 += ' ';
}
result1 += ')';
}
else if (M.$type(result) === 'String')
{
result1 = '"' + result + '"';
}
else if (M.$type(result) === 'Function')
{
result1 = 'Function';
}
else
{
result1 = result;
}
return result1;
};
if (typeof describe === 'undefined')
{
loadModules(function()
{
M.$W('------------- SpaceTime Module is Ready ----------------');
//============================================
// var src = [1, [M.plus, [2]], [M.map, [M.CONSOLE]]];
//var src = ' ( (( ) ) 8 ) ';
// var src = '';
// var src = '(("hello world"))';
// var src = ' ( 2 (+(9)) ) ';
// var src = ' (FIB (take(10)) (map(CONSOLE))) ';
//var src = ' (SEQ (iterate ()) (take(10)) (map(CONSOLE))) ';
// var src = ' (NATURAL (take(10)) (map(CONSOLE))) ';
/* (
FUNCTION_COMPOSITION VAL0(get(i(-(2))))(+(VAL0(get(i(-(1))))))
(ifF((i <= 1)(1))))
)*/
// var src = '( (1 2 3 4 5) (take(4)) (take(2)))';
// var src = '( () (iterate ( I ) ) (take(10)) )';
var src = '( 3 (==(1)) ( if ( ("match") ("unmatch") ) ) )';
// var src = '( () (iterate (I (if ((I (<= (4) )) (1)) ) ) ) (take(10)) )';
//var src = '(3 (== (3)))';
// var src = '("foo" if(true) ("bar") ))';
M.debug = false;
var src1 = $parse(src);
/* src1 = [
5,
[M.ifF, [[false], [1]]]
];*/
// src1 = [10, [M.bool, [M.GREATEREQUAL, [10]]]];
console.log(src1);
var result = M.map(src1, [M.EVAL]);
M.$W(' === result === ');
M.$W(result);
M.$W($construct(result));
//------------------------------------------------------------------
});
}
//=========================================