force-lang 0.0.12 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/native_lib.js CHANGED
@@ -1,1230 +1,1702 @@
1
- const log = require('bunny-logger');
2
- //const request = require('request-promise-native');
3
- const request = require('sync-request');
4
- const JSON5 = require('json5');
5
- const env = require('./env');
6
- const err = require('./error');
7
- const eval = require('./eval');
8
- const loadfile = require('./load-file');
9
- const obj_utils = require('./obj_utils');
10
- const vsprintf = require('format').vsprintf;
11
-
12
- class NativeLib{
13
- constructor(){
14
- this.populate();
15
- }
16
- make_func_obj(func_name){
17
- return {
18
- "_type":"TC_COMP_FUNC",
19
- "_datum":func_name
20
- }
21
- }
22
-
23
- test_func(){
24
- log.info('...hai detto pippo?');
25
- }
26
- bye_func(){
27
- process.exit(0);
28
- }
29
- noop_func(){
30
- //do nothing (no operation)
31
- }
32
- print_stack_func(){
33
- env.print_stack();
34
- }
35
- print_env_func(){
36
- env.print_debug();
37
- }
38
- print_words_func(){
39
- let x = env._dict;
40
- //x=x.filter(item => item._type == 'TC_NATIVE_FUNC');
41
- for(var item of x){
42
- if(item._datum._type == 'TC_NATIVE_FUNC'|| item._datum._type == 'TC_COMP_FUNC'){
43
- //log.info(`${item._name} `);
44
- process.stdout.write(`${item._name} `);
45
- }
46
- }
47
- log.info('');
48
- }
49
- emit_func(){
50
- const x = env.s.pop();
51
- //log.info(x);
52
- if(x._type === 'TC_NUM'){
53
- process.stdout.write(String.fromCharCode(x._datum));
54
- return;
55
- }
56
- env.s.push(err.throw(`unknown item type ${x._type} of ${x._datum}`));
57
- }
58
- see_func(func_name){
59
- var x=env.lookup(func_name);
60
- if(!x){
61
- log.info('no word found...');
62
- return;
63
- }
64
- switch(x._datum._type){
65
- case 'TC_NATIVE_FUNC':
66
- log.info(`: ${x._name}`);
67
- log.info('<native func> ;');
68
- break;
69
- case 'TC_COMP_FUNC':
70
- log.info(`: ${x._name}`);
71
- process.stdout.write(` `);
72
- for(var n of x._datum._datum){
73
- process.stdout.write(`${n._datum} `);
74
- }
75
- log.info('');
76
- break;
77
- default:
78
- log.info('not a word...');
79
- break;
80
- }
81
- }
82
- print_tos_func(){
83
- const x = env.s.pop();
84
- if(!x) return;
85
- //log.info(x);
86
- switch(x._type){
87
- case 'TC_NUM':
88
- case 'TC_STR':
89
- case 'TC_BOOL':
90
- log.info(x._datum);
91
- break;
92
- case 'TC_JSON':
93
- log.info(obj_utils.stringify(x._datum));
94
- break;
95
- case 'TC_FUNC_JS':
96
- log.info('#-<js func>');
97
- break;
98
- case 'TC_LAMBDA_FUNC':
99
- log.info('#-<lambda func>');
100
- break;
101
- case 'TC_VAR':
102
- log.info(x._name);
103
- break;
104
- case 'TC_ERR':
105
- log.info(`ERR: ${x._datum.msg}`);
106
- break;
107
- default:
108
- log.info(`unknown: { ${x._type} ${x._datum} }`);
109
- break;
110
- }
111
- }
112
- print_debug_tos_func(){
113
- const x = env.s.pop();
114
- if(!x) return;
115
- //log.info(x);
116
- switch(x._type){
117
- case 'TC_NUM':
118
- case 'TC_STR':
119
- case 'TC_BOOL':
120
- log.info(`{ ${x._type} ${x._datum} }`);
121
- break;
122
- case 'TC_JSON':
123
- log.info(`{ ${x._type} ${obj_utils.stringify(x._datum)} }`);
124
- break;
125
- case 'TC_FUNC_JS':
126
- log.info('#-<js func>');
127
- break;
128
- case 'TC_LAMBDA_FUNC':
129
- log.info('#-<lambda func>');
130
- break;
131
- case 'TC_VAR':
132
- log.info(`{ ${x._type} ${x._name} ${obj_utils.stringify(x._datum._datum)} }`);
133
- break;
134
- case 'TC_ERR':
135
- log.info(`ERR: ${x._datum.msg}`);
136
- break;
137
- default:
138
- log.info(`unknown: { ${x._type} ${x._datum} }`);
139
- break;
140
- }
141
- }
142
- assign_var_func(){
143
- const varx = env.s.pop();
144
- const val = env.s.pop();
145
- if(!varx || !val) {
146
- env.s.push(err.throw('no items on top 2 elements of the stack... aborting'));
147
- return;
148
- }
149
- //log.info(varx);
150
- //log.info(val);
151
- switch(val._type){
152
- case'TC_NUM':
153
- case 'TC_STR':
154
- case 'TC_JSON':
155
- case 'TC_PROMISE':
156
- varx._datum = val;
157
- //env.set(varx._name, val, varx._type, varx._where);
158
- break;
159
- default:
160
- break;
161
- }
162
- }
163
- read_var_func(){
164
- const varx = env.s.pop();
165
- if(!varx) {
166
- env.s.push(err.throw('no element on top of the stack... aborting'));
167
- return;
168
- }
169
- //log.info(varx);
170
- switch(varx._datum._type){
171
- case'TC_NUM':
172
- case 'TC_STR':
173
- case 'TC_JSON':
174
- case 'TC_PROMISE':
175
- env.s.push(varx._datum);
176
- break;
177
- default:
178
- break;
179
- }
180
- }
181
- not_func(){
182
- if(!env.is_bool(env.TOS())){
183
- env.s.push(err.throw('TOS is not a bool. aborting operation...'));
184
- return;
185
- }
186
- let x = env.get_bool_val(env.s.pop());
187
- env.s.push(env.set_bool_val(!x));
188
- }
189
- and_func(){
190
- if(!env.is_bool(env.TOS())){
191
- env.s.push(err.throw('TOS is not a bool. aborting operation...'));
192
- return;
193
- }
194
- if(!env.is_bool(env.TOS2())){
195
- env.s.push(err.throw('TOS" is not a bool. aborting operation...'));
196
- return;
197
- }
198
- let a=env.get_bool_val(env.s.pop());
199
- let b=env.get_bool_val(env.s.pop());
200
- env.s.push(env.set_bool_val(a&&b));
201
- }
202
- or_func(){
203
- if(!env.is_bool(env.TOS())){
204
- env.s.push(err.throw('TOS is not a bool. aborting operation...'));
205
- return;
206
- }
207
- if(!env.is_bool(env.TOS2())){
208
- env.s.push(err.throw('TOS2 is not a bool. aborting operation...'));
209
- return;
210
- }
211
- let a=env.get_bool_val(env.s.pop());
212
- let b=env.get_bool_val(env.s.pop());
213
- env.s.push(env.set_bool_val(a||b));
214
- }
215
- is_num_func(){
216
- if(env.is_num(env.s.pop())){
217
- env.s.push(env.true_obj());
218
- return;
219
- }
220
- env.s.push(env.false_obj());
221
- }
222
- is_string_func(){
223
- if(env.is_string(env.s.pop())){
224
- env.s.push(env.true_obj());
225
- return;
226
- }
227
- env.s.push(env.false_obj());
228
- }
229
- is_list_func(){
230
- if(env.is_list(env.s.pop())){
231
- env.s.push(env.true_obj());
232
- return;
233
- }
234
- env.s.push(env.false_obj());
235
- }
236
- is_falsy_func(){
237
- let x = env.s.pop();
238
- if(x){
239
- switch(x._type){
240
- case 'TC_NUM':
241
- if(x._datum==0) return env.s.push(env.true_obj()); else return env.s.push(env.false_obj());
242
- break;
243
- case 'TC_STR':
244
- if(x._datum == '') return env.s.push(env.true_obj());
245
- break;
246
- case 'TC_BOOL':
247
- if(env.is_false(x)) return env.s.push(env.true_obj());
248
- break;
249
- case 'TC_JLIST':
250
- case 'TC_JOBJ':
251
- default:
252
- return env.s.push(env.false_obj());
253
- break;
254
- }
255
- }
256
- return env.s.push(env.true_obj());
257
- }
258
- dup_func(){
259
- env.s.push(env.TOS());
260
- }
261
- swap_func(){
262
- if(env.TOS() && env.TOS2()){
263
- let x = env.s.pop();
264
- let y = env.s.pop();
265
- env.s.push(x);
266
- env.s.push(y);
267
- return;
268
- }
269
- env.s.push(err.throw('not 2 elemets in the stack... aborting...'));
270
- }
271
- drop_func(){
272
- env.s.pop();
273
- }
274
- ndrop_func(){
275
-
276
- }
277
- nbye_func(){
278
- if(env.TOS() && env.TOS()._type == 'TC_NUM'){
279
- process.exit(env.s.pop()._datum);
280
- }
281
- env.s.push(err.throw('TOS not a number... aborting'));
282
- }
283
- over_func(){
284
- env.s.push(env.s.look_at(1));
285
- }
286
- num_plus_func(){
287
- if(env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM'){
288
- env.s.push({_type: 'TC_NUM', _datum: env.s.pop()._datum + env.s.pop()._datum});
289
- return;
290
- }
291
- env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
292
- }
293
- num_minus_func(){
294
- if(env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM'){
295
- let x = env.s.pop()._datum;
296
- let y = env.s.pop()._datum;
297
- env.s.push({_type: 'TC_NUM', _datum: y - x});
298
- return;
299
- }
300
- env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
301
- }
302
- num_times_func(){
303
- if(env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM'){
304
- env.s.push({_type: 'TC_NUM', _datum: env.s.pop()._datum * env.s.pop()._datum});
305
- return;
306
- }
307
- env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
308
- }
309
- num_div_func(){
310
- if(env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM'){
311
- let x = env.s.pop()._datum;
312
- let y = env.s.pop()._datum;
313
- env.s.push({_type: 'TC_NUM', _datum: y / x});
314
- return;
315
- }
316
- env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
317
- }
318
- plus_func(){
319
- eval.eval('f+');
320
- }
321
- minus_func(){
322
- eval.eval('n:-');
323
- }
324
- times_func(){
325
- eval.eval('n:*');
326
- }
327
- division_func(){
328
- eval.eval('n:/');
329
- }
330
- module_func(){
331
- eval.eval('n:%');
332
- }
333
- string_plus_func(){
334
- if(env.is_string(env.TOS()) || env.is_string(env.TOS2())){
335
- if(env.is_num(env.TOS()) || env.is_num(env.TOS2()) || env.is_string(env.TOS()) || env.is_string(env.TOS2())){
336
- const x= env.s.pop();
337
- const y= env.s.pop();
338
- env.s.push({"_type":"TC_STR","_datum":y._datum+x._datum});
339
- return;
340
- }
341
- }
342
- env.s.push(err.throw("invalid arguments type"));
343
- }
344
- array_plus_func(){
345
- if(env.is_list(env.TOS()) || env.is_list(env.TOS2())){
346
- const x= env.s.pop();
347
- const y= env.s.pop();
348
- env.s.push({"_type":"TC_JSON","_datum":y._datum.concat(x._datum)});
349
- return;
350
- }
351
- env.s.push(err.throw("invalid arguments type"));
352
- }
353
- array_at_func(){
354
- if(env.is_num(env.TOS()) && env.is_list(env.TOS2())){
355
- try{
356
- var index=env.s.pop()._datum;
357
- var arr=env.s.pop()._datum;
358
- if(index<0) index=arr.length+index;
359
- if(index>=arr.length || index<0){
360
- env.s.push(err.throw("invalid index bound"));
361
- return;
362
- }
363
- const value=arr[index];
364
- const xval=env.adj_bool_val(value);
365
- env.s.push({"_type":env.guess_type(value), "_datum":xval});
366
- return;
367
- }catch(e){
368
- env.s.push(err.throw(e));
369
- return;
370
- }
371
- }
372
- env.s.push(err.throw("invalid arguments type"));
373
- }
374
- array_set_at_func(){
375
- if(env.TOS() && env.is_num(env.TOS2()) && env.is_list(env.s.look_at(2))){
376
- try{
377
- var value=env.s.pop()._datum;
378
- var index=env.s.pop()._datum;
379
- var arr=env.s.pop()._datum;
380
- if(index<0) index=arr.length+index;
381
- if(index>=arr.length || index<0){
382
- env.s.push(err.throw("invalid index bound"));
383
- return;
384
- }
385
- arr[index]=value;
386
- env.s.push({"_type":"TC_JSON", "_datum":arr});
387
- return;
388
- }catch(e){
389
- env.s.push(err.throw(e));
390
- return;
391
- }
392
- }
393
- env.s.push(err.throw("invalid arguments type"));
394
- }
395
- object_at_func(){
396
- if(env.is_string(env.TOS()) && env.is_obj(env.TOS2())){
397
- try{
398
- var key=env.s.pop()._datum;
399
- var obj=env.s.pop()._datum;
400
- var value=obj[key];
401
- if(value){
402
- const xval=env.adj_bool_val(value);
403
- env.s.push({"_type":env.guess_type(value), "_datum":xval});
404
- return;
405
- }
406
- env.s.push(err.throw("invalid object key"));
407
- return;
408
- }catch(e){
409
- env.s.push(err.throw(e));
410
- return;
411
- }
412
- }
413
- env.s.push(err.throw("invalid arguments type"));
414
- }
415
- object_set_at_func(){
416
- if(env.TOS() && env.is_string(env.TOS2()) && env.is_obj(env.s.look_at(2))){
417
- try{
418
- var value=env.s.pop()._datum;
419
- var key=env.s.pop()._datum;
420
- var obj=env.s.pop()._datum;
421
- obj[key]=value;
422
- env.s.push({"_type":'TC_JSON', "_datum":obj});
423
- return;
424
- }catch(e){
425
- env.s.push(err.throw(e));
426
- return;
427
- }
428
- }
429
- env.s.push(err.throw("invalid arguments type"));
430
- }
431
- array_length_func(){
432
- if(env.is_list(env.TOS())){
433
- try{
434
- var x=env.s.pop()._datum.length;
435
- env.s.push({"_type":"TC_NUM","_datum":x});
436
- return;
437
- }catch(e){
438
- env.s.push(err.throw(e));
439
- return;
440
- }
441
- }
442
- env.s.push(err.throw("invalid arguments type. TOS not a list"));
443
- }
444
- array_push_func(){
445
- if(env.is_list(env.TOS2()) && env.TOS()){
446
- try{
447
- var value=env.s.pop()._datum;
448
- var arr=env.s.pop()._datum;
449
- arr.push(value);
450
- env.s.push({"_type":"TC_JSON","_datum":arr});
451
- return;
452
- }catch(e){
453
- env.s.push(err.throw(e));
454
- return;
455
- }
456
- }
457
- env.s.push(err.throw("invalid arguments type."));
458
- }
459
- array_pop_func(){
460
- if(env.is_list(env.TOS())){
461
- try{
462
- var value=env.s.pop()._datum.pop();
463
- const xval=env.adj_bool_val(value);
464
- env.s.push({"_type":env.guess_type(value), "_datum":xval});
465
- return;
466
- }catch(e){
467
- env.s.push(err.throw(e));
468
- return;
469
- }
470
- }
471
- env.s.push(err.throw("invalid arguments type. TOS not a list"));
472
- }
473
- object_keys_func(){
474
- if(env.is_obj(env.TOS())){
475
- try{
476
- var x=env.s.pop()._datum;
477
- env.s.push({"_type":"TC_JSON","_datum":Object.keys(x)});
478
- return;
479
- }catch(e){
480
- env.s.push(err.throw(e));
481
- return;
482
- }
483
- }
484
- env.s.push(err.throw("invalid arguments type."));
485
- }
486
- object_values_func(){
487
- if(env.is_obj(env.TOS())){
488
- try{
489
- var x=env.s.pop()._datum;
490
- env.s.push({"_type":"TC_JSON","_datum":Object.values(x)});
491
- return;
492
- }catch(e){
493
- env.s.push(err.throw(e));
494
- return;
495
- }
496
- }
497
- env.s.push(err.throw("invalid arguments type."));
498
- }
499
- string_split_func(){
500
- if(env.TOS() && env.is_string(env.TOS2())){
501
- try{
502
- var x=env.s.pop();
503
- var y=env.s.pop()._datum;
504
- var z;
505
- if(env.is_string(x)){
506
- z=y.split(x._datum);
507
- }
508
- if(env.is_obj(x)){
509
- z=y.split(x._datum.separator, x._datum.limit);
510
- }
511
- if(z){
512
- env.s.push({"_type":"TC_JSON","_datum":z});
513
- return;
514
- }
515
- }catch(e){
516
- env.s.push(err.throw(e));
517
- return;
518
- }
519
- }
520
- env.s.push(err.throw("invalid arguments type."));
521
- }
522
- string_join_func(){
523
- if(env.is_string(env.TOS()) && env.is_list(env.TOS2())){
524
- try{
525
- var separator=env.s.pop()._datum;
526
- var list=env.s.pop()._datum;
527
- env.s.push({"_type":"TC_STR","_datum":list.join(separator)});
528
- return;
529
- }catch(e){
530
- env.s.push(err.throw(e));
531
- return;
532
- }
533
- }
534
- env.s.push(err.throw("invalid arguments type."));
535
- }
536
- json_stringify_func(){
537
- if(env.is_json(env.TOS())){
538
- try{
539
- env.s.push({"_type":"TC_STR","_datum":JSON5.stringify(env.s.pop()._datum)});
540
- }catch(e){
541
- env.s.push(err.throw(e));
542
- }
543
- return;
544
- }
545
- env.s.push(err.throw("invalid arguments type."));
546
- }
547
- json_parse_func(){
548
- if(env.is_string(env.TOS())){
549
- try{
550
- env.s.push({"_type":"TC_JSON","_datum":JSON5.parse(env.s.pop()._datum)});
551
- }catch(e){
552
- env.s.push(err.throw(e));
553
- }
554
- return;
555
- }
556
- env.s.push(err.throw("invalid arguments type."));
557
- }
558
- string_at_func(){
559
- if(env.is_num(env.TOS()) && env.is_string(env.TOS2())){
560
- try{
561
- var index=env.s.pop()._datum;
562
- var str=env.s.pop()._datum;
563
- if(index<0) index=str.length+index;
564
- env.s.push({"_type":"TC_STR","_datum":str[index]});
565
- }catch(e){
566
- env.s.push(err.throw(e));
567
- }
568
- return;
569
- }
570
- env.s.push(err.throw("invalid arguments type."));
571
- }
572
- string_set_at_func(){
573
- if(env.is_string(env.TOS()) && env.is_num(env.TOS2()) && env.is_string(env.s.look_at(2))){
574
- try{
575
- var replacement=env.s.pop()._datum;
576
- var index=env.s.pop()._datum;
577
- var str=env.s.pop()._datum;
578
- if(index<0) index=str.length+index;
579
- var result=str.substr(0, index) + replacement+ str.substr(index + replacement.length);
580
- env.s.push({"_type":"TC_STR","_datum":result});
581
- }catch(e){
582
- env.s.push(err.throw(e));
583
- }
584
- return;
585
- }
586
- env.s.push(err.throw("invalid arguments type."));
587
- }
588
- equal_func(){
589
- if(env.TOS() && env.TOS2()){
590
- try{
591
- var x=env.s.pop()._datum;
592
- var y=env.s.pop()._datum;
593
- env.s.push({"_type":"TC_BOOL","_datum":y===x ? 'T' : 'F'});
594
- }catch(e){
595
- env.s.push(err.throw(e));
596
- }
597
- return;
598
- }
599
- env.s.push(err.throw("invalid arguments type."));
600
- }
601
- eq_func(){
602
- if(env.TOS() && env.TOS2()){
603
- try{
604
- var x=env.s.pop()._datum;
605
- var y=env.s.pop()._datum;
606
- env.s.push({"_type":"TC_BOOL","_datum":y==x ? 'T' : 'F'});
607
- }catch(e){
608
- env.s.push(err.throw(e));
609
- }
610
- return;
611
- }
612
- env.s.push(err.throw("invalid arguments type."));
613
- }
614
- num_minor_func(){
615
- if(env.is_num(env.TOS()) && env.is_num(env.TOS2())){
616
- try{
617
- var x=env.s.pop()._datum;
618
- var y=env.s.pop()._datum;
619
- env.s.push({"_type":"TC_BOOL","_datum":y<x ? 'T' : 'F'});
620
- }catch(e){
621
- env.s.push(err.throw(e));
622
- }
623
- return;
624
- }
625
- env.s.push(err.throw("invalid arguments type."));
626
- }
627
- num_major_func(){
628
- if(env.is_num(env.TOS()) && env.is_num(env.TOS2())){
629
- try{
630
- var x=env.s.pop()._datum;
631
- var y=env.s.pop()._datum;
632
- env.s.push({"_type":"TC_BOOL","_datum":y>x ? 'T' : 'F'});
633
- }catch(e){
634
- env.s.push(err.throw(e));
635
- }
636
- return;
637
- }
638
- env.s.push(err.throw("invalid arguments type."));
639
- }
640
- num_min_eq_func(){
641
- if(env.is_num(env.TOS()) && env.is_num(env.TOS2())){
642
- try{
643
- var x=env.s.pop()._datum;
644
- var y=env.s.pop()._datum;
645
- env.s.push({"_type":"TC_BOOL","_datum":y<=x ? 'T' : 'F'});
646
- }catch(e){
647
- env.s.push(err.throw(e));
648
- }
649
- return;
650
- }
651
- env.s.push(err.throw("invalid arguments type."));
652
- }
653
- num_maj_eq_func(){
654
- if(env.is_num(env.TOS()) && env.is_num(env.TOS2())){
655
- try{
656
- var x=env.s.pop()._datum;
657
- var y=env.s.pop()._datum;
658
- env.s.push({"_type":"TC_BOOL","_datum":y>=x ? 'T' : 'F'});
659
- }catch(e){
660
- env.s.push(err.throw(e));
661
- }
662
- return;
663
- }
664
- env.s.push(err.throw("invalid arguments type."));
665
- }
666
- delete_dict_func(){
667
- if(env.TOS() && env.TOS()._type=='TC_STR'){
668
- try{
669
- env.delete(env.s.pop()._datum);
670
- }catch(e){
671
- env.s.push(err.throw(e));
672
- }
673
- return;
674
- }
675
- env.s.push(err.throw("invalid arguments type."));
676
- }
677
- net_request_func(){ //async....
678
- if(env.is_obj(env.TOS())){
679
- try{
680
- //let resp= await request(env.s.pop()._datum);
681
- let x=env.s.pop()._datum;
682
- let resp= request(x.method,x.url,x);
683
- //env.s.push({"_type":"TC_STR","_datum":resp});
684
- env.s.push({"_type":"TC_STR","_datum":resp.body.toString('utf8')});
685
- }catch(e){
686
- env.s.push(err.throw(e));
687
- }
688
- return;
689
- }
690
- env.s.push(err.throw("invalid arguments type."));
691
- }
692
- included_func(){
693
- if(env.is_string(env.TOS())){
694
- try{
695
- const arg= env.s.pop()._datum;
696
- const filename = loadfile.resolve_path(arg);
697
- var x = loadfile.loadsync(filename);
698
- eval.eval(x);
699
- return;
700
- }catch(e){
701
- env.s.push(err.throw(e));
702
- return;
703
- }
704
- }
705
- env.s.push(err.throw("invalid arguments type"));
706
- }
707
- file_slurp_func(){
708
- if(env.is_string(env.TOS())){
709
- try{
710
- const arg= env.s.pop()._datum;
711
- const filename = loadfile.resolve_path(arg);
712
- var x = loadfile.loadsync(filename);
713
- env.s.push({"_type":"TC_STR","_datum":x});
714
- return;
715
- }catch(e){
716
- env.s.push(err.throw(e));
717
- return;
718
- }
719
- }
720
- env.s.push(err.throw("invalid arguments type"));
721
- }
722
- throw_func(){
723
- if(env.TOS() && env.TOS()._type == 'TC_STR'){
724
- env.s.push(err.throw(env.s.pop()._datum));
725
- return;
726
- }
727
- if(env.TOS() && env.TOS()._type == 'TC_JSON'){
728
- const err_arg = env.s.pop();
729
- env.s.push(err.throw(err_arg._datum.msg, err_arg._datum.code));
730
- return;
731
- }
732
- env.s.push(err.throw('invalid throw argument in TOS'));
733
- }
734
- require_js_func(){
735
- if(env.TOS() && env.TOS()._type == 'TC_STR'){
736
- try{
737
- const arg= env.s.pop()._datum;
738
- const filename = loadfile.resolve_path(arg);
739
- var js=require(filename);
740
- env.s.push({"_type":env.guess_type(js),"_datum":js});
741
- return;
742
- }catch(e){
743
- env.s.push(err.throw(e));
744
- return;
745
- }
746
- }
747
- env.s.push(err.throw('invalid request-js argument type'));
748
- }
749
- funcjs_exec_func(){
750
- try{
751
- if(env.TOS() && env.TOS()._type=='TC_LAMBDA_FUNC'){
752
- eval.eval_parsed(env.s.pop()._datum);
753
- return;
754
- }
755
- if(env.TOS() && env.TOS()._type == 'TC_FUNC_JS'){
756
- var x=env.s.pop()._datum;
757
- var arity=x.length;
758
- var args=[];
759
- //log.info(arity);
760
- while(arity>0){
761
- args.unshift(env.s.pop()._datum);
762
- arity--;
763
- }
764
- //log.info(args);
765
- var y=x.apply(null,args);
766
- //log.info(y);
767
- if(y) env.s.push({"_type":env.guess_type(y),"_datum":y});
768
- return;
769
- }
770
- if(env.TOS() && env.TOS()._type == 'TC_STR' && env.TOS2() && env.TOS2()._type=='TC_JSON'){
771
- var method=env.s.pop()._datum;
772
- var method_list=method.split(".");
773
- var x=env.s.pop()._datum;
774
- var funcall=x;
775
- for(var item of method_list){
776
- funcall=funcall[item];
777
- }
778
- //log.info(funcall);
779
- var arity=funcall.length;
780
- var args=[];
781
- //log.info(arity);
782
- while(arity>0){
783
- args.unshift(env.s.pop()._datum);
784
- arity--;
785
- }
786
- //log.info(args);
787
- var y=funcall.apply(x,args);
788
- //log.info(y);
789
- if(y) env.s.push({"_type":env.guess_type(y),"_datum":y});
790
- return;
791
- }
792
- env.s.push(err.throw('invalid funcall-js argument type'));
793
- }catch(e){
794
- env.s.push(err.throw(e));
795
- }
796
- }
797
- handle_repl_func(){
798
- err.handle_repl();
799
- }
800
- handle_standard_func(){
801
- err.handle_standard();
802
- }
803
- regex_test_func(){
804
- try{
805
- if(env.is_obj(env.TOS()) && env.is_string(env.TOS2())){
806
- var rex_obj = env.s.pop(),_datum;
807
- var str = env.s.pop()._datum;
808
- var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
809
- const result = my_rex.test(str);
810
- env.s.push(
811
- result ?
812
- env.true_obj() :
813
- env.false_obj()
814
- );
815
- return;
816
- }
817
- if(env.is_string(env.TOS()) && env.is_string(env.TOS2())){
818
- var rex = env.s.pop()._datum;
819
- var str = env.s.pop()._datum;
820
- var my_rex = new RegExp(rex);
821
- const result = my_rex.test(str);
822
- env.s.push(
823
- result ?
824
- env.true_obj() :
825
- env.false_obj()
826
- );
827
- return;
828
- }
829
- env.s.push(err.throw('invalid regex test argument type'));
830
- }catch(e){
831
- env.s.push(err.throw(e));
832
- }
833
- }
834
- regex_exec_func(){
835
- try{
836
- if(env.is_obj(env.TOS()) && env.is_string(env.TOS2())){
837
- var rex_obj = env.s.pop(),_datum;
838
- var str = env.s.pop()._datum;
839
- var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
840
- const result = my_rex.exec(str);
841
- env.s.push(
842
- result ?
843
- {"_type":"TC_JSON","_datum":result} :
844
- 0
845
- );
846
- return;
847
- }
848
- if(env.is_string(env.TOS()) && env.is_string(env.TOS2())){
849
- var rex = env.s.pop()._datum;
850
- var str = env.s.pop()._datum;
851
- var my_rex = new RegExp(rex);
852
- const result = my_rex.exec(str);
853
- env.s.push(
854
- result ?
855
- {"_type":"TC_JSON","_datum":result} :
856
- 0
857
- );
858
- return;
859
- }
860
- env.s.push(err.throw('invalid regex exec argument type'));
861
- }catch(e){
862
- env.s.push(err.throw(e));
863
- }
864
- }
865
- regex_match_func(){
866
- try{
867
- if(env.is_obj(env.TOS()) && env.is_string(env.TOS2())){
868
- var rex_obj = env.s.pop(),_datum;
869
- var str = env.s.pop()._datum;
870
- var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
871
- const result = str.match(my_rex);
872
- env.s.push(
873
- result ?
874
- {"_type":"TC_JSON","_datum":result} :
875
- 0
876
- );
877
- return;
878
- }
879
- if(env.is_string(env.TOS()) && env.is_string(env.TOS2())){
880
- var rex = env.s.pop()._datum;
881
- var str = env.s.pop()._datum;
882
- var my_rex = new RegExp(rex);
883
- const result = str.match(my_rex);
884
- env.s.push(
885
- result ?
886
- {"_type":"TC_JSON","_datum":result} :
887
- 0
888
- );
889
- return;
890
- }
891
- env.s.push(err.throw('invalid regex match argument type'));
892
- }catch(e){
893
- env.s.push(err.throw(e));
894
- }
895
- }
896
- regex_search_func(){
897
- try{
898
- if(env.is_obj(env.TOS()) && env.is_string(env.TOS2())){
899
- var rex_obj = env.s.pop(),_datum;
900
- var str = env.s.pop()._datum;
901
- var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
902
- const result = str.search(my_rex);
903
- env.s.push({"_type":"TC_NUM","_datum":result});
904
- return;
905
- }
906
- if(env.is_string(env.TOS()) && env.is_string(env.TOS2())){
907
- var rex = env.s.pop()._datum;
908
- var str = env.s.pop()._datum;
909
- var my_rex = new RegExp(rex);
910
- const result = str.search(my_rex);
911
- env.s.push({"_type":"TC_NUM","_datum":result});
912
- return;
913
- }
914
- env.s.push(err.throw('invalid regex search argument type'));
915
- }catch(e){
916
- env.s.push(err.throw(e));
917
- }
918
- }
919
- regex_replace_func(){
920
- try{
921
- if(env.is_obj(env.TOS()) && env.is_string(env.TOS2()) && env.is_string(env.s.look_at(2))){
922
- var rex_obj = env.s.pop(),_datum;
923
- var replace = env.s.pop()._datum;
924
- var str = env.s.pop()._datum;
925
- var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
926
- const result = str.replace(my_rex, replace);
927
- env.s.push({"_type":"TC_JSON","_datum":result});
928
- return;
929
- }
930
- if(env.is_string(env.TOS()) && env.is_string(env.TOS2()) && env.is_string(env.s.look_at(2))){
931
- var rex = env.s.pop()._datum;
932
- var replace = env.s.pop()._datum;
933
- var str = env.s.pop()._datum;
934
- var my_rex = new RegExp(rex);
935
- const result = str.replace(my_rex, replace);
936
- env.s.push({"_type":"TC_STR","_datum":result});
937
- return;
938
- }
939
- env.s.push(err.throw('invalid regex replace argument type'));
940
- }catch(e){
941
- env.s.push(err.throw(e));
942
- }
943
- }
944
- array_shift_func(){
945
- try{
946
- if(env.is_list(env.TOS())){
947
- let value = env.s.pop()._datum.shift();
948
- const xval=env.adj_bool_val(value);
949
- env.s.push({"_type":env.guess_type(value), "_datum":xval});
950
- return;
951
- }
952
- env.s.push(err.throw('invalid operation. TOS is not an list'));
953
- }catch(e){
954
- env.s.push(err.throw(e));
955
- }
956
- }
957
- array_unshift_func(){
958
- try{
959
- if(env.TOS() && env.is_list(env.TOS2())){
960
- let value = env.s.pop()._datum;
961
- let array = env.s.pop()._datum;
962
- array.unshift(value);
963
- env.s.push({"_type":"TC_JSON", "_datum":array});
964
- return;
965
- }
966
- env.s.push(err.throw('invalid operation. TOS2 is not an list'));
967
- }catch(e){
968
- env.s.push(err.throw(e));
969
- }
970
- }
971
- array_each_func(){
972
- try{
973
- if(env.TOS() && env.TOS()._type=='TC_LAMBDA_FUNC' && env.is_list(env.TOS2())){
974
- let funcall = env.s.pop()._datum;
975
- let array = env.s.pop()._datum;
976
- //Vx€array, !!funcall
977
- while(array.length >0 ){
978
- var value = array.shift();
979
- const xval=env.adj_bool_val(value);
980
- env.s.push({"_type":env.guess_type(value), "_datum":xval});
981
- eval.eval_parsed(funcall);
982
- }
983
- return;
984
- }
985
- env.s.push(err.throw('invalid arguments. TOS should be a LAMBDA function and TOS2 should be a list'));
986
- }catch(e){
987
- env.s.push(err.throw(e));
988
- }
989
- }
990
- parse_args_func(){
991
- try{
992
- if(env.is_list(env.TOS())){
993
- const rx_double_dash = /^\-\-[a-zA-Z0-9]+/;
994
- const rx_single_dash = /^\-[a-zA-Z0-9]+/;
995
- const rx_no_dash = /^[^\-]/;
996
- const rx_two_dashes = /^\-\-/;
997
- let args = {argv:[]};
998
- const params_data = env.s.pop()._datum;
999
- //log.info(params_data);
1000
- let param_found=false;
1001
- let test_col, is_dash;
1002
- let argv = env.lookup('os:argv')._datum._datum;
1003
- argv.shift(); argv.shift(); argv.shift();
1004
- //log.info(argv);
1005
- while(argv.length > 0) {
1006
- let item = argv.shift();
1007
- is_dash=0;
1008
- // log.info(item);
1009
- // if(rx_double_dash.test(item)){
1010
- // test_col= 1;
1011
- // is_dash = true;
1012
- // }
1013
- // if(rx_single_dash.test(item)) {
1014
- // test_col = 0;
1015
- // is_dash = true;
1016
- // }
1017
-
1018
- // if(is_dash) {
1019
- // param_found = false;
1020
- // for(var param_item of params_data){
1021
- // log.info(param_item[test_col]);
1022
- // if(param_item[test_col]=== (test_col === 0)? item.match(rx_single_dash)[0] : item.match(rx_double_dash[0])){
1023
- // log.info('d: ',param_item);
1024
- // param_found = true;
1025
- // break;
1026
- // }
1027
- // }
1028
- // //if ! invalid parameter
1029
- // if(!param_found) log.info(`invalid parameter ${item}`);
1030
- // }
1031
-
1032
- if(rx_double_dash.test(item)){
1033
- param_found = false;
1034
- for(var param_item of params_data){
1035
- if(param_item[1]===item.match(rx_double_dash)[0]){
1036
- //log.info('dd: ',param_item);
1037
- param_found = true;
1038
- switch(param_item[2]){
1039
- case 'y':
1040
- //log.info('ha param');
1041
- if(argv[0] && rx_no_dash.test(argv[0])){
1042
- args[param_item[1].replace(rx_two_dashes,'')] = argv.shift();
1043
- } else {
1044
- log.info(`error, parameter ${item} needs value...`);
1045
- //error needs param...
1046
- }
1047
- break;
1048
- case 'n':
1049
- //log.info('no param');
1050
- args[param_item[1].replace(rx_two_dashes,'')] = true;
1051
- break;
1052
- case '?':
1053
- //log.info('opt param');
1054
- if(argv[0] && rx_no_dash.test(argv[0])){
1055
- args[param_item[1].replace(rx_two_dashes,'')] = argv[0];
1056
- }else{
1057
- args[param_item[1].replace(rx_two_dashes,'')] = true;
1058
- }
1059
- break;
1060
- default:
1061
-
1062
- break;
1063
- }
1064
- break;
1065
- }
1066
- }
1067
- //if ! invalid parameter
1068
- if(!param_found) log.info(`invalid parameter ${item}`);
1069
- }else if(rx_single_dash.test(item)) {
1070
- param_found= false;
1071
- for(var param_item of params_data){
1072
- if(param_item[0]===item.match(rx_single_dash)[0]){
1073
- //log.info('sd: ',param_item);
1074
- param_found = true;
1075
- switch(param_item[2]){
1076
- case 'y':
1077
- //log.info('ha param');
1078
- if(argv[0] && rx_no_dash.test(argv[0])){
1079
- args[param_item[1].replace(rx_two_dashes,'')] = argv.shift();
1080
- } else {
1081
- log.info(`error, parameter ${item} needs value...`);
1082
- //error needs param...
1083
- }
1084
- break;
1085
- case 'n':
1086
- //log.info('no param');
1087
- args[param_item[1].replace(rx_two_dashes,'')] = true;
1088
- break;
1089
- case '?':
1090
- //log.info('opt param');
1091
- if(argv[0] && rx_no_dash.test(argv[0])){
1092
- args[param_item[1].replace(rx_two_dashes,'')] = argv[0];
1093
- }else{
1094
- args[param_item[1].replace(rx_two_dashes,'')] = true;
1095
- }
1096
- break;
1097
- default:
1098
-
1099
- break;
1100
- }
1101
- break;
1102
- }
1103
- }
1104
- if(!param_found) log.info(`invalid parameter ${item}`);
1105
- }else {
1106
- //else{
1107
- //normal argv...
1108
- args.argv.push(item);
1109
- }
1110
- }
1111
- log.info(args);
1112
- return;
1113
- }
1114
- env.s.push(err.throw('invalid arguments. TOS should be a list'));
1115
- }catch(e){
1116
- env.s.push(err.throw(e));
1117
- }
1118
- }
1119
- await_func(){
1120
- if(env.TOS() && env.TOS()._type=== 'TC_PROMISE'){
1121
- try{
1122
- env.s.pop()._datum.then(x => {
1123
- env.s.push({"_type":env.guess_type(x), "_datum":x});
1124
- });
1125
- return;
1126
- }catch(e){
1127
- env.s.push(err.throw(e));
1128
- return;
1129
- }
1130
- }
1131
- env.s.push(err.throw("invalid arguments type. a Promise was expected."));
1132
- }
1133
- string_format_func(){
1134
- if(env.TOS() && env.TOS2() && env.is_string(env.TOS2()) && env.is_list(env.TOS())){
1135
- try{
1136
- const args_list=env.s.pop()._datum;
1137
- const fmt=env.s.pop()._datum;
1138
- env.s.push({"_type":"TC_STR", "_datum":vsprintf(fmt,args_list)});
1139
- return;
1140
- }catch(e){
1141
- env.s.push(err.throw(e));
1142
- return;
1143
- }
1144
- }
1145
- env.s.push(err.throw("invalid arguments type. a Promise was expected."));
1146
- }
1147
- populate_repl(){
1148
- env.set('handle',{_type: 'TC_NATIVE_FUNC', _datum: this.handle_repl_func}, 'TC_WORD');
1149
- }
1150
- populate(){
1151
- env.set('pippo',{_type: 'TC_NATIVE_FUNC', _datum: this.test_func}, 'TC_WORD');
1152
- env.set('bye',{_type: 'TC_NATIVE_FUNC', _datum: this.bye_func}, 'TC_WORD');
1153
- env.set('noop',{_type: 'TC_NATIVE_FUNC', _datum: this.noop_func}, 'TC_WORD');
1154
- env.set('.s',{_type: 'TC_NATIVE_FUNC', _datum: this.print_stack_func}, 'TC_WORD');
1155
- env.set('.e',{_type: 'TC_NATIVE_FUNC', _datum: this.print_env_func}, 'TC_WORD');
1156
- env.set('words',{_type: 'TC_NATIVE_FUNC', _datum: this.print_words_func}, 'TC_WORD');
1157
- env.set('emit',{_type: 'TC_NATIVE_FUNC', _datum: this.emit_func}, 'TC_WORD');
1158
- env.set('.',{_type: 'TC_NATIVE_FUNC', _datum: this.print_tos_func}, 'TC_WORD');
1159
- env.set('.?',{_type: 'TC_NATIVE_FUNC', _datum: this.print_debug_tos_func}, 'TC_WORD');
1160
- env.set('!',{_type: 'TC_NATIVE_FUNC', _datum: this.assign_var_func}, 'TC_WORD');
1161
- env.set('@',{_type: 'TC_NATIVE_FUNC', _datum: this.read_var_func}, 'TC_WORD');
1162
- env.set('not',{_type: 'TC_NATIVE_FUNC', _datum: this.not_func}, 'TC_WORD');
1163
- env.set('and',{_type: 'TC_NATIVE_FUNC', _datum: this.and_func}, 'TC_WORD');
1164
- env.set('or',{_type: 'TC_NATIVE_FUNC', _datum: this.or_func}, 'TC_WORD');
1165
- env.set('is_num',{_type: 'TC_NATIVE_FUNC', _datum: this.is_num_func}, 'TC_WORD');
1166
- env.set('is_string',{_type: 'TC_NATIVE_FUNC', _datum: this.is_string_func}, 'TC_WORD');
1167
- env.set('is_list',{_type: 'TC_NATIVE_FUNC', _datum: this.is_list_func}, 'TC_WORD');
1168
- env.set('is_falsy',{_type: 'TC_NATIVE_FUNC', _datum: this.is_falsy_func}, 'TC_WORD');
1169
- env.set('dup',{_type: 'TC_NATIVE_FUNC', _datum: this.dup_func}, 'TC_WORD');
1170
- env.set('swap',{_type: 'TC_NATIVE_FUNC', _datum: this.swap_func}, 'TC_WORD');
1171
- env.set('drop',{_type: 'TC_NATIVE_FUNC', _datum: this.drop_func}, 'TC_WORD');
1172
- env.set('ndrop',{_type: 'TC_NATIVE_FUNC', _datum: this.ndrop_func}, 'TC_WORD');
1173
- env.set('nbye',{_type: 'TC_NATIVE_FUNC', _datum: this.nbye_func}, 'TC_WORD');
1174
- env.set('over',{_type: 'TC_NATIVE_FUNC', _datum: this.over_func}, 'TC_WORD');
1175
- env.set('n:+',{_type: 'TC_NATIVE_FUNC', _datum: this.num_plus_func}, 'TC_WORD');
1176
- env.set('n:-',{_type: 'TC_NATIVE_FUNC', _datum: this.num_minus_func}, 'TC_WORD');
1177
- env.set('n:*',{_type: 'TC_NATIVE_FUNC', _datum: this.num_times_func}, 'TC_WORD');
1178
- env.set('n:/',{_type: 'TC_NATIVE_FUNC', _datum: this.num_div_func}, 'TC_WORD');
1179
- env.set('+',{_type: 'TC_NATIVE_FUNC', _datum: this.plus_func}, 'TC_WORD');
1180
- env.set('-',{_type: 'TC_NATIVE_FUNC', _datum: this.minus_func}, 'TC_WORD');
1181
- env.set('*',{_type: 'TC_NATIVE_FUNC', _datum: this.times_func}, 'TC_WORD');
1182
- env.set('/',{_type: 'TC_NATIVE_FUNC', _datum: this.division_func}, 'TC_WORD');
1183
- env.set('%',{_type: 'TC_NATIVE_FUNC', _datum: this.module_func}, 'TC_WORD');
1184
- env.set('handle',{_type: 'TC_NATIVE_FUNC', _datum: this.handle_standard_func}, 'TC_WORD');
1185
- env.set('throw',{_type: 'TC_NATIVE_FUNC', _datum: this.throw_func}, 'TC_WORD');
1186
- env.set('s:+',{_type: 'TC_NATIVE_FUNC', _datum: this.string_plus_func}, 'TC_WORD');
1187
- env.set('a:+',{_type: 'TC_NATIVE_FUNC', _datum: this.array_plus_func}, 'TC_WORD');
1188
- env.set('included',{_type: 'TC_NATIVE_FUNC', _datum: this.included_func}, 'TC_WORD');
1189
- env.set('a:@',{_type: 'TC_NATIVE_FUNC', _datum: this.array_at_func}, 'TC_WORD');
1190
- env.set('a:!',{_type: 'TC_NATIVE_FUNC', _datum: this.array_set_at_func}, 'TC_WORD');
1191
- env.set('m:@',{_type: 'TC_NATIVE_FUNC', _datum: this.object_at_func}, 'TC_WORD');
1192
- env.set('m:!',{_type: 'TC_NATIVE_FUNC', _datum: this.object_set_at_func}, 'TC_WORD');
1193
- env.set('a:length',{_type: 'TC_NATIVE_FUNC', _datum: this.array_length_func}, 'TC_WORD');
1194
- env.set('a:push',{_type: 'TC_NATIVE_FUNC', _datum: this.array_push_func}, 'TC_WORD');
1195
- env.set('a:pop',{_type: 'TC_NATIVE_FUNC', _datum: this.array_pop_func}, 'TC_WORD');
1196
- env.set('m:keys',{_type: 'TC_NATIVE_FUNC', _datum: this.object_keys_func}, 'TC_WORD');
1197
- env.set('m:values',{_type: 'TC_NATIVE_FUNC', _datum: this.object_values_func}, 'TC_WORD');
1198
- env.set('s:split',{_type: 'TC_NATIVE_FUNC', _datum: this.string_split_func}, 'TC_WORD');
1199
- env.set('s:join',{_type: 'TC_NATIVE_FUNC', _datum: this.string_join_func}, 'TC_WORD');
1200
- env.set('j:stringify',{_type: 'TC_NATIVE_FUNC', _datum: this.json_stringify_func}, 'TC_WORD');
1201
- env.set('j:parse',{_type: 'TC_NATIVE_FUNC', _datum: this.json_parse_func}, 'TC_WORD');
1202
- env.set('s:@',{_type: 'TC_NATIVE_FUNC', _datum: this.string_at_func}, 'TC_WORD');
1203
- env.set('s:!',{_type: 'TC_NATIVE_FUNC', _datum: this.string_set_at_func}, 'TC_WORD');
1204
- env.set('=',{_type: 'TC_NATIVE_FUNC', _datum: this.equal_func}, 'TC_WORD');
1205
- env.set('===',{_type: 'TC_NATIVE_FUNC', _datum: this.equal_func}, 'TC_WORD');
1206
- env.set('==',{_type: 'TC_NATIVE_FUNC', _datum: this.eq_func}, 'TC_WORD');
1207
- env.set('<',{_type: 'TC_NATIVE_FUNC', _datum: this.num_minor_func}, 'TC_WORD');
1208
- env.set('>',{_type: 'TC_NATIVE_FUNC', _datum: this.num_major_func}, 'TC_WORD');
1209
- env.set('<=',{_type: 'TC_NATIVE_FUNC', _datum: this.num_min_eq_func}, 'TC_WORD');
1210
- env.set('>=',{_type: 'TC_NATIVE_FUNC', _datum: this.num_maj_eq_func}, 'TC_WORD');
1211
- env.set('f:slurp',{_type: 'TC_NATIVE_FUNC', _datum: this.file_slurp_func}, 'TC_WORD');
1212
- env.set('net:request',{_type: 'TC_NATIVE_FUNC', _datum: this.net_request_func}, 'TC_WORD');
1213
- env.set('j:require-js',{_type: 'TC_NATIVE_FUNC', _datum: this.require_js_func}, 'TC_WORD');
1214
- env.set('!!',{_type: 'TC_NATIVE_FUNC', _datum: this.funcjs_exec_func}, 'TC_WORD');
1215
- env.set('G:delete',{_type: 'TC_NATIVE_FUNC', _datum: this.delete_dict_func}, 'TC_WORD');
1216
- env.set('rx:test',{_type: 'TC_NATIVE_FUNC', _datum: this.regex_test_func}, 'TC_WORD');
1217
- env.set('rx:exec',{_type: 'TC_NATIVE_FUNC', _datum: this.regex_exec_func}, 'TC_WORD');
1218
- env.set('rx:match',{_type: 'TC_NATIVE_FUNC', _datum: this.regex_match_func}, 'TC_WORD');
1219
- env.set('rx:search',{_type: 'TC_NATIVE_FUNC', _datum: this.regex_search_func}, 'TC_WORD');
1220
- env.set('rx:replace',{_type: 'TC_NATIVE_FUNC', _datum: this.regex_replace_func}, 'TC_WORD');
1221
- env.set('a:shift',{_type: 'TC_NATIVE_FUNC', _datum: this.array_shift_func}, 'TC_WORD');
1222
- env.set('a:unshift',{_type: 'TC_NATIVE_FUNC', _datum: this.array_unshift_func}, 'TC_WORD');
1223
- env.set('a:each',{_type: 'TC_NATIVE_FUNC', _datum: this.array_each_func}, 'TC_WORD');
1224
- env.set('os:parse-args',{_type: 'TC_NATIVE_FUNC', _datum: this.parse_args_func}, 'TC_WORD');
1225
- env.set('await',{_type: 'TC_NATIVE_FUNC', _datum: this.await_func}, 'TC_WORD');
1226
- env.set('s:format',{_type: 'TC_NATIVE_FUNC', _datum: this.string_format_func}, 'TC_WORD');
1227
- }
1228
- };
1229
-
1230
- module.exports = new NativeLib();
1
+ import log from 'bunny-logger';
2
+ // const request = require('request-promise-native');
3
+ import JSON5 from 'json5';
4
+ import { vsprintf } from 'sprintf-js';
5
+ import * as cheerio from 'cheerio';
6
+ import { execSync } from 'child_process';
7
+ import got from 'got';
8
+ import env from './env.js';
9
+ import err from './error.js';
10
+ import evalx from './eval.js';
11
+ import loadfile from './load-file.js';
12
+ import obj_utils from './obj_utils.js';
13
+
14
+ class NativeLib {
15
+ constructor() {
16
+ this.populate();
17
+ }
18
+
19
+ make_func_obj(func_name) {
20
+ return {
21
+ _type: 'TC_COMP_FUNC',
22
+ _datum: func_name,
23
+ };
24
+ }
25
+
26
+ test_func() {
27
+ log.info('...hai detto pippo?');
28
+ }
29
+
30
+ bye_func() {
31
+ process.exit(0);
32
+ }
33
+
34
+ noop_func() {
35
+ // do nothing (no operation)
36
+ }
37
+
38
+ print_stack_func() {
39
+ env.print_stack();
40
+ }
41
+
42
+ print_env_func() {
43
+ env.print_debug();
44
+ }
45
+
46
+ print_words_func() {
47
+ const x = env._dict;
48
+ // x=x.filter(item => item._type == 'TC_NATIVE_FUNC');
49
+ for (const item of x) {
50
+ if (item._datum._type == 'TC_NATIVE_FUNC' || item._datum._type == 'TC_COMP_FUNC') {
51
+ // log.info(`${item._name} `);
52
+ process.stdout.write(`${item._name} `);
53
+ }
54
+ }
55
+ log.info('');
56
+ }
57
+
58
+ emit_func() {
59
+ const x = env.s.pop();
60
+ // log.info(x);
61
+ if (x._type === 'TC_NUM') {
62
+ process.stdout.write(String.fromCharCode(x._datum));
63
+ return;
64
+ }
65
+ env.s.push(err.throw(`unknown item type ${x._type} of ${x._datum}`));
66
+ }
67
+
68
+ see_func(func_name) {
69
+ const x = env.lookup(func_name);
70
+ if (!x) {
71
+ log.info('no word found...');
72
+ return;
73
+ }
74
+ switch (x._datum._type) {
75
+ case 'TC_NATIVE_FUNC':
76
+ log.info(`: ${x._name}`);
77
+ log.info('<native func> ;');
78
+ break;
79
+ case 'TC_COMP_FUNC':
80
+ log.info(`: ${x._name}`);
81
+ process.stdout.write(` `);
82
+ for (const n of x._datum._datum) {
83
+ process.stdout.write(`${n._datum} `);
84
+ }
85
+ log.info('');
86
+ break;
87
+ default:
88
+ log.info('not a word...');
89
+ break;
90
+ }
91
+ }
92
+
93
+ print_tos_func() {
94
+ const x = env.s.pop();
95
+ if (!x) {
96
+ return;
97
+ }
98
+ // log.info(x);
99
+ switch (x._type) {
100
+ case 'TC_NUM':
101
+ case 'TC_STR':
102
+ case 'TC_BOOL':
103
+ log.info(x._datum);
104
+ break;
105
+ case 'TC_JSON':
106
+ log.info(obj_utils.stringify(x._datum));
107
+ break;
108
+ case 'TC_FUNC_JS':
109
+ log.info('#-<js func>');
110
+ break;
111
+ case 'TC_LAMBDA_FUNC':
112
+ log.info('#-<lambda func>');
113
+ break;
114
+ case 'TC_VAR':
115
+ log.info(x._name);
116
+ break;
117
+ case 'TC_ERR':
118
+ log.info(`ERR: ${x._datum.msg}`);
119
+ break;
120
+ default:
121
+ log.info(`unknown: { ${x._type} ${x._datum} }`);
122
+ break;
123
+ }
124
+ }
125
+
126
+ print_debug_tos_func() {
127
+ const x = env.s.pop();
128
+ if (!x) {
129
+ return;
130
+ }
131
+ // log.info(x);
132
+ switch (x._type) {
133
+ case 'TC_NUM':
134
+ case 'TC_STR':
135
+ case 'TC_BOOL':
136
+ log.info(`{ ${x._type} ${x._datum} }`);
137
+ break;
138
+ case 'TC_JSON':
139
+ log.info(`{ ${x._type} ${obj_utils.stringify(x._datum)} }`);
140
+ break;
141
+ case 'TC_FUNC_JS':
142
+ log.info('#-<js func>');
143
+ break;
144
+ case 'TC_LAMBDA_FUNC':
145
+ log.info('#-<lambda func>');
146
+ break;
147
+ case 'TC_VAR':
148
+ log.info(`{ ${x._type} ${x._name} ${obj_utils.stringify(x._datum._datum)} }`);
149
+ break;
150
+ case 'TC_ERR':
151
+ log.info(`ERR: ${x._datum.msg}`);
152
+ break;
153
+ default:
154
+ log.info(`unknown: { ${x._type} ${x._datum} }`);
155
+ break;
156
+ }
157
+ }
158
+
159
+ assign_var_func() {
160
+ const varx = env.s.pop();
161
+ const val = env.s.pop();
162
+ if (!varx || !val) {
163
+ env.s.push(err.throw('no items on top 2 elements of the stack... aborting'));
164
+ return;
165
+ }
166
+ // log.info(varx);
167
+ // log.info(val);
168
+ switch (val._type) {
169
+ case 'TC_NUM':
170
+ case 'TC_STR':
171
+ case 'TC_JSON':
172
+ case 'TC_BOOL':
173
+ case 'TC_PROMISE':
174
+ varx._datum = val;
175
+ // env.set(varx._name, val, varx._type, varx._where);
176
+ break;
177
+ default:
178
+ break;
179
+ }
180
+ }
181
+
182
+ read_var_func() {
183
+ const varx = env.s.pop();
184
+ if (!varx) {
185
+ env.s.push(err.throw('no element on top of the stack... aborting'));
186
+ return;
187
+ }
188
+ // log.info(varx);
189
+ switch (varx._datum._type) {
190
+ case 'TC_NUM':
191
+ case 'TC_STR':
192
+ case 'TC_JSON':
193
+ case 'TC_BOOL':
194
+ case 'TC_PROMISE':
195
+ env.s.push(varx._datum);
196
+ break;
197
+ default:
198
+ break;
199
+ }
200
+ }
201
+
202
+ not_func() {
203
+ if (!env.is_bool(env.TOS())) {
204
+ env.s.push(err.throw('TOS is not a bool. aborting operation...'));
205
+ return;
206
+ }
207
+ const x = env.get_bool_val(env.s.pop());
208
+ env.s.push(env.set_bool_val(!x));
209
+ }
210
+
211
+ and_func() {
212
+ if (!env.is_bool(env.TOS())) {
213
+ env.s.push(err.throw('TOS is not a bool. aborting operation...'));
214
+ return;
215
+ }
216
+ if (!env.is_bool(env.TOS2())) {
217
+ env.s.push(err.throw('TOS" is not a bool. aborting operation...'));
218
+ return;
219
+ }
220
+ const a = env.get_bool_val(env.s.pop());
221
+ const b = env.get_bool_val(env.s.pop());
222
+ env.s.push(env.set_bool_val(a && b));
223
+ }
224
+
225
+ or_func() {
226
+ if (!env.is_bool(env.TOS())) {
227
+ env.s.push(err.throw('TOS is not a bool. aborting operation...'));
228
+ return;
229
+ }
230
+ if (!env.is_bool(env.TOS2())) {
231
+ env.s.push(err.throw('TOS2 is not a bool. aborting operation...'));
232
+ return;
233
+ }
234
+ const a = env.get_bool_val(env.s.pop());
235
+ const b = env.get_bool_val(env.s.pop());
236
+ env.s.push(env.set_bool_val(a || b));
237
+ }
238
+
239
+ is_num_func() {
240
+ if (env.is_num(env.s.pop())) {
241
+ env.s.push(env.true_obj());
242
+ return;
243
+ }
244
+ env.s.push(env.false_obj());
245
+ }
246
+
247
+ is_string_func() {
248
+ if (env.is_string(env.s.pop())) {
249
+ env.s.push(env.true_obj());
250
+ return;
251
+ }
252
+ env.s.push(env.false_obj());
253
+ }
254
+
255
+ is_list_func() {
256
+ if (env.is_list(env.s.pop())) {
257
+ env.s.push(env.true_obj());
258
+ return;
259
+ }
260
+ env.s.push(env.false_obj());
261
+ }
262
+
263
+ is_falsy_func() {
264
+ const x = env.s.pop();
265
+ if (x) {
266
+ switch (x._type) {
267
+ case 'TC_NUM':
268
+ if (x._datum == 0) {
269
+ return env.s.push(env.true_obj());
270
+ }
271
+ return env.s.push(env.false_obj());
272
+ break;
273
+ case 'TC_STR':
274
+ if (x._datum == '') {
275
+ return env.s.push(env.true_obj());
276
+ }
277
+ break;
278
+ case 'TC_BOOL':
279
+ if (env.is_false(x)) {
280
+ return env.s.push(env.true_obj());
281
+ }
282
+ break;
283
+ case 'TC_JLIST':
284
+ case 'TC_JOBJ':
285
+ default:
286
+ return env.s.push(env.false_obj());
287
+ break;
288
+ }
289
+ }
290
+ return env.s.push(env.true_obj());
291
+ }
292
+
293
+ dup_func() {
294
+ env.s.push(env.TOS());
295
+ }
296
+
297
+ swap_func() {
298
+ if (env.TOS() && env.TOS2()) {
299
+ const x = env.s.pop();
300
+ const y = env.s.pop();
301
+ env.s.push(x);
302
+ env.s.push(y);
303
+ return;
304
+ }
305
+ env.s.push(err.throw('not 2 elemets in the stack... aborting...'));
306
+ }
307
+
308
+ drop_func() {
309
+ env.s.pop();
310
+ }
311
+
312
+ ndrop_func() {
313
+ if (env.TOS() && env.TOS()._type === 'TC_NUM') {
314
+ try {
315
+ let n = env.s.pop()._datum;
316
+ while (n > 0) {
317
+ env.s.pop();
318
+ n--;
319
+ }
320
+ return;
321
+ } catch (e) {
322
+ env.s.push(err.throw(e));
323
+ return;
324
+ }
325
+ }
326
+ env.s.push(err.throw('invalid arguments type. TOS is not a number.'));
327
+ }
328
+
329
+ nbye_func() {
330
+ if (env.TOS() && env.TOS()._type == 'TC_NUM') {
331
+ process.exit(env.s.pop()._datum);
332
+ }
333
+ env.s.push(err.throw('TOS not a number... aborting'));
334
+ }
335
+
336
+ over_func() {
337
+ env.s.push(env.s.look_at(1));
338
+ }
339
+
340
+ num_plus_func() {
341
+ if (env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM') {
342
+ env.s.push({
343
+ _type: 'TC_NUM',
344
+ _datum: env.s.pop()._datum + env.s.pop()._datum,
345
+ });
346
+ return;
347
+ }
348
+ env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
349
+ }
350
+
351
+ num_minus_func() {
352
+ if (env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM') {
353
+ const x = env.s.pop()._datum;
354
+ const y = env.s.pop()._datum;
355
+ env.s.push({ _type: 'TC_NUM', _datum: y - x });
356
+ return;
357
+ }
358
+ env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
359
+ }
360
+
361
+ num_times_func() {
362
+ if (env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM') {
363
+ env.s.push({
364
+ _type: 'TC_NUM',
365
+ _datum: env.s.pop()._datum * env.s.pop()._datum,
366
+ });
367
+ return;
368
+ }
369
+ env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
370
+ }
371
+
372
+ num_div_func() {
373
+ if (env.TOS() && env.TOS()._type == 'TC_NUM' && env.TOS2() && env.TOS2()._type == 'TC_NUM') {
374
+ const x = env.s.pop()._datum;
375
+ const y = env.s.pop()._datum;
376
+ env.s.push({ _type: 'TC_NUM', _datum: y / x });
377
+ return;
378
+ }
379
+ env.s.push(err.throw('no numbers on top 2 elements of the stack... aborting'));
380
+ }
381
+
382
+ async plus_func() {
383
+ await evalx.eval('f+');
384
+ }
385
+
386
+ async minus_func() {
387
+ await evalx.eval('n:-');
388
+ }
389
+
390
+ async times_func() {
391
+ await evalx.eval('n:*');
392
+ }
393
+
394
+ async division_func() {
395
+ await evalx.eval('n:/');
396
+ }
397
+
398
+ async module_func() {
399
+ await evalx.eval('n:%');
400
+ }
401
+
402
+ string_plus_func() {
403
+ if (env.is_string(env.TOS()) || env.is_string(env.TOS2())) {
404
+ if (env.is_num(env.TOS()) || env.is_num(env.TOS2()) || env.is_string(env.TOS()) || env.is_string(env.TOS2())) {
405
+ const x = env.s.pop();
406
+ const y = env.s.pop();
407
+ env.s.push({ _type: 'TC_STR', _datum: y._datum + x._datum });
408
+ return;
409
+ }
410
+ }
411
+ env.s.push(err.throw('invalid arguments type'));
412
+ }
413
+
414
+ array_plus_func() {
415
+ if (env.is_list(env.TOS()) || env.is_list(env.TOS2())) {
416
+ const x = env.s.pop();
417
+ const y = env.s.pop();
418
+ env.s.push({ _type: 'TC_JSON', _datum: y._datum.concat(x._datum) });
419
+ return;
420
+ }
421
+ env.s.push(err.throw('invalid arguments type'));
422
+ }
423
+
424
+ array_at_func() {
425
+ if (env.is_num(env.TOS()) && env.is_list(env.TOS2())) {
426
+ try {
427
+ let index = env.s.pop()._datum;
428
+ const arr = env.s.pop()._datum;
429
+ if (index < 0) {
430
+ index = arr.length + index;
431
+ }
432
+ if (index >= arr.length || index < 0) {
433
+ env.s.push(err.throw('invalid index bound'));
434
+ return;
435
+ }
436
+ const value = arr[index];
437
+ const xval = env.adj_bool_val(value);
438
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
439
+ return;
440
+ } catch (e) {
441
+ env.s.push(err.throw(e));
442
+ return;
443
+ }
444
+ }
445
+ env.s.push(err.throw('invalid arguments type'));
446
+ }
447
+
448
+ array_set_at_func() {
449
+ if (env.TOS() && env.is_num(env.TOS2()) && env.is_list(env.s.look_at(2))) {
450
+ try {
451
+ const value = env.s.pop()._datum;
452
+ let index = env.s.pop()._datum;
453
+ const arr = env.s.pop()._datum;
454
+ if (index < 0) {
455
+ index = arr.length + index;
456
+ }
457
+ if (index >= arr.length || index < 0) {
458
+ env.s.push(err.throw('invalid index bound'));
459
+ return;
460
+ }
461
+ arr[index] = value;
462
+ env.s.push({ _type: 'TC_JSON', _datum: arr });
463
+ return;
464
+ } catch (e) {
465
+ env.s.push(err.throw(e));
466
+ return;
467
+ }
468
+ }
469
+ env.s.push(err.throw('invalid arguments type'));
470
+ }
471
+
472
+ object_at_func() {
473
+ if (env.is_string(env.TOS()) && env.is_obj(env.TOS2())) {
474
+ try {
475
+ const key = env.s.pop()._datum;
476
+ const obj = env.s.pop()._datum;
477
+ const value = obj[key];
478
+ if (value) {
479
+ const xval = env.adj_bool_val(value);
480
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
481
+ return;
482
+ }
483
+ // env.s.push(err.throw("invalid object key"));
484
+ env.s.push({ _type: 'TC_UNDEF', _datum: 'undefined' });
485
+ return;
486
+ } catch (e) {
487
+ env.s.push(err.throw(e));
488
+ return;
489
+ }
490
+ }
491
+ env.s.push(err.throw('invalid arguments type'));
492
+ }
493
+
494
+ object_set_at_func() {
495
+ if (env.TOS() && env.is_string(env.TOS2()) && env.is_obj(env.s.look_at(2))) {
496
+ try {
497
+ const value = env.s.pop()._datum;
498
+ const key = env.s.pop()._datum;
499
+ const obj = env.s.pop()._datum;
500
+ obj[key] = value;
501
+ env.s.push({ _type: 'TC_JSON', _datum: obj });
502
+ return;
503
+ } catch (e) {
504
+ env.s.push(err.throw(e));
505
+ return;
506
+ }
507
+ }
508
+ env.s.push(err.throw('invalid arguments type'));
509
+ }
510
+
511
+ array_length_func() {
512
+ if (env.is_list(env.TOS())) {
513
+ try {
514
+ const x = env.s.pop()._datum.length;
515
+ env.s.push({ _type: 'TC_NUM', _datum: x });
516
+ return;
517
+ } catch (e) {
518
+ env.s.push(err.throw(e));
519
+ return;
520
+ }
521
+ }
522
+ env.s.push(err.throw('invalid arguments type. TOS not a list'));
523
+ }
524
+
525
+ array_push_func() {
526
+ if (env.is_list(env.TOS2()) && env.TOS()) {
527
+ try {
528
+ const value = env.s.pop()._datum;
529
+ const arr = env.s.pop()._datum;
530
+ arr.push(value);
531
+ env.s.push({ _type: 'TC_JSON', _datum: arr });
532
+ return;
533
+ } catch (e) {
534
+ env.s.push(err.throw(e));
535
+ return;
536
+ }
537
+ }
538
+ env.s.push(err.throw('invalid arguments type.'));
539
+ }
540
+
541
+ array_pop_func() {
542
+ if (env.is_list(env.TOS())) {
543
+ try {
544
+ const value = env.s.pop()._datum.pop();
545
+ const xval = env.adj_bool_val(value);
546
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
547
+ return;
548
+ } catch (e) {
549
+ env.s.push(err.throw(e));
550
+ return;
551
+ }
552
+ }
553
+ env.s.push(err.throw('invalid arguments type. TOS not a list'));
554
+ }
555
+
556
+ object_keys_func() {
557
+ if (env.is_obj(env.TOS())) {
558
+ try {
559
+ const x = env.s.pop()._datum;
560
+ env.s.push({ _type: 'TC_JSON', _datum: Object.keys(x) });
561
+ return;
562
+ } catch (e) {
563
+ env.s.push(err.throw(e));
564
+ return;
565
+ }
566
+ }
567
+ env.s.push(err.throw('invalid arguments type.'));
568
+ }
569
+
570
+ object_values_func() {
571
+ if (env.is_obj(env.TOS())) {
572
+ try {
573
+ const x = env.s.pop()._datum;
574
+ env.s.push({ _type: 'TC_JSON', _datum: Object.values(x) });
575
+ return;
576
+ } catch (e) {
577
+ env.s.push(err.throw(e));
578
+ return;
579
+ }
580
+ }
581
+ env.s.push(err.throw('invalid arguments type.'));
582
+ }
583
+
584
+ string_split_func() {
585
+ if (env.TOS() && env.is_string(env.TOS2())) {
586
+ try {
587
+ const x = env.s.pop();
588
+ const y = env.s.pop()._datum;
589
+ let z;
590
+ if (env.is_string(x)) {
591
+ z = y.split(x._datum);
592
+ }
593
+ if (env.is_obj(x)) {
594
+ z = y.split(x._datum.separator, x._datum.limit);
595
+ }
596
+ if (z) {
597
+ env.s.push({ _type: 'TC_JSON', _datum: z });
598
+ return;
599
+ }
600
+ } catch (e) {
601
+ env.s.push(err.throw(e));
602
+ return;
603
+ }
604
+ }
605
+ env.s.push(err.throw('invalid arguments type.'));
606
+ }
607
+
608
+ string_join_func() {
609
+ if (env.is_string(env.TOS()) && env.is_list(env.TOS2())) {
610
+ try {
611
+ const separator = env.s.pop()._datum;
612
+ const list = env.s.pop()._datum;
613
+ env.s.push({ _type: 'TC_STR', _datum: list.join(separator) });
614
+ return;
615
+ } catch (e) {
616
+ env.s.push(err.throw(e));
617
+ return;
618
+ }
619
+ }
620
+ env.s.push(err.throw('invalid arguments type.'));
621
+ }
622
+
623
+ json_stringify_func() {
624
+ if (env.is_json(env.TOS())) {
625
+ try {
626
+ env.s.push({
627
+ _type: 'TC_STR',
628
+ _datum: JSON5.stringify(env.s.pop()._datum),
629
+ });
630
+ } catch (e) {
631
+ env.s.push(err.throw(e));
632
+ }
633
+ return;
634
+ }
635
+ env.s.push(err.throw('invalid arguments type.'));
636
+ }
637
+
638
+ json_parse_func() {
639
+ if (env.is_string(env.TOS())) {
640
+ try {
641
+ env.s.push({
642
+ _type: 'TC_JSON',
643
+ _datum: JSON5.parse(env.s.pop()._datum),
644
+ });
645
+ } catch (e) {
646
+ env.s.push(err.throw(e));
647
+ }
648
+ return;
649
+ }
650
+ env.s.push(err.throw('invalid arguments type.'));
651
+ }
652
+
653
+ string_at_func() {
654
+ if (env.is_num(env.TOS()) && env.is_string(env.TOS2())) {
655
+ try {
656
+ let index = env.s.pop()._datum;
657
+ const str = env.s.pop()._datum;
658
+ if (index < 0) {
659
+ index = str.length + index;
660
+ }
661
+ env.s.push({ _type: 'TC_STR', _datum: str[index] });
662
+ } catch (e) {
663
+ env.s.push(err.throw(e));
664
+ }
665
+ return;
666
+ }
667
+ env.s.push(err.throw('invalid arguments type.'));
668
+ }
669
+
670
+ string_set_at_func() {
671
+ if (env.is_string(env.TOS()) && env.is_num(env.TOS2()) && env.is_string(env.s.look_at(2))) {
672
+ try {
673
+ const replacement = env.s.pop()._datum;
674
+ let index = env.s.pop()._datum;
675
+ const str = env.s.pop()._datum;
676
+ if (index < 0) {
677
+ index = str.length + index;
678
+ }
679
+ const result = str.substr(0, index) + replacement + str.substr(index + replacement.length);
680
+ env.s.push({ _type: 'TC_STR', _datum: result });
681
+ } catch (e) {
682
+ env.s.push(err.throw(e));
683
+ }
684
+ return;
685
+ }
686
+ env.s.push(err.throw('invalid arguments type.'));
687
+ }
688
+
689
+ equal_func() {
690
+ if (env.TOS() && env.TOS2()) {
691
+ try {
692
+ const x = env.s.pop()._datum;
693
+ const y = env.s.pop()._datum;
694
+ env.s.push({ _type: 'TC_BOOL', _datum: y === x ? 'T' : 'F' });
695
+ } catch (e) {
696
+ env.s.push(err.throw(e));
697
+ }
698
+ return;
699
+ }
700
+ env.s.push(err.throw('invalid arguments type.'));
701
+ }
702
+
703
+ eq_func() {
704
+ if (env.TOS() && env.TOS2()) {
705
+ try {
706
+ const x = env.s.pop()._datum;
707
+ const y = env.s.pop()._datum;
708
+ env.s.push({ _type: 'TC_BOOL', _datum: y == x ? 'T' : 'F' });
709
+ } catch (e) {
710
+ env.s.push(err.throw(e));
711
+ }
712
+ return;
713
+ }
714
+ env.s.push(err.throw('invalid arguments type.'));
715
+ }
716
+
717
+ num_minor_func() {
718
+ if (env.is_num(env.TOS()) && env.is_num(env.TOS2())) {
719
+ try {
720
+ const x = env.s.pop()._datum;
721
+ const y = env.s.pop()._datum;
722
+ env.s.push({ _type: 'TC_BOOL', _datum: y < x ? 'T' : 'F' });
723
+ } catch (e) {
724
+ env.s.push(err.throw(e));
725
+ }
726
+ return;
727
+ }
728
+ env.s.push(err.throw('invalid arguments type.'));
729
+ }
730
+
731
+ num_major_func() {
732
+ if (env.is_num(env.TOS()) && env.is_num(env.TOS2())) {
733
+ try {
734
+ const x = env.s.pop()._datum;
735
+ const y = env.s.pop()._datum;
736
+ env.s.push({ _type: 'TC_BOOL', _datum: y > x ? 'T' : 'F' });
737
+ } catch (e) {
738
+ env.s.push(err.throw(e));
739
+ }
740
+ return;
741
+ }
742
+ env.s.push(err.throw('invalid arguments type.'));
743
+ }
744
+
745
+ num_min_eq_func() {
746
+ if (env.is_num(env.TOS()) && env.is_num(env.TOS2())) {
747
+ try {
748
+ const x = env.s.pop()._datum;
749
+ const y = env.s.pop()._datum;
750
+ env.s.push({ _type: 'TC_BOOL', _datum: y <= x ? 'T' : 'F' });
751
+ } catch (e) {
752
+ env.s.push(err.throw(e));
753
+ }
754
+ return;
755
+ }
756
+ env.s.push(err.throw('invalid arguments type.'));
757
+ }
758
+
759
+ num_maj_eq_func() {
760
+ if (env.is_num(env.TOS()) && env.is_num(env.TOS2())) {
761
+ try {
762
+ const x = env.s.pop()._datum;
763
+ const y = env.s.pop()._datum;
764
+ env.s.push({ _type: 'TC_BOOL', _datum: y >= x ? 'T' : 'F' });
765
+ } catch (e) {
766
+ env.s.push(err.throw(e));
767
+ }
768
+ return;
769
+ }
770
+ env.s.push(err.throw('invalid arguments type.'));
771
+ }
772
+
773
+ delete_dict_func() {
774
+ if (env.TOS() && env.TOS()._type == 'TC_STR') {
775
+ try {
776
+ env.delete(env.s.pop()._datum);
777
+ } catch (e) {
778
+ env.s.push(err.throw(e));
779
+ }
780
+ return;
781
+ }
782
+ env.s.push(err.throw('invalid arguments type.'));
783
+ }
784
+
785
+ async net_request_func() {
786
+ // async....
787
+ if (env.is_obj(env.TOS())) {
788
+ try {
789
+ // //let resp= await request(env.s.pop()._datum);
790
+ const x = env.s.pop()._datum;
791
+ const { url } = x;
792
+ delete x.url;
793
+ // let resp= request(x.method,x.url,x);
794
+ const resp = await got(url, x).text();
795
+ // //env.s.push({"_type":"TC_STR","_datum":resp});
796
+ // env.s.push({"_type":"TC_STR","_datum":resp.body.toString('utf8')});
797
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
798
+ } catch (e) {
799
+ const errObj = {
800
+ code: e.response?.statusCode,
801
+ body: e.response?.body,
802
+ error: { name: e.name, code: e.code, msg: e.message },
803
+ };
804
+ // env.s.push({"_type":env.guess_type(errObj),"_datum":errObj});
805
+ env.s.push(err.throw(JSON.stringify(errObj)));
806
+ }
807
+ return;
808
+ }
809
+ env.s.push(err.throw('invalid arguments type.'));
810
+ }
811
+
812
+ async included_func() {
813
+ if (env.is_string(env.TOS())) {
814
+ try {
815
+ const arg = env.s.pop()._datum;
816
+ const filename = loadfile.resolve_path(arg);
817
+ const x = loadfile.loadsync(filename);
818
+ await evalx.eval(x);
819
+ return;
820
+ } catch (e) {
821
+ env.s.push(err.throw(e));
822
+ return;
823
+ }
824
+ }
825
+ env.s.push(err.throw('invalid arguments type'));
826
+ }
827
+
828
+ file_slurp_func() {
829
+ if (env.is_string(env.TOS())) {
830
+ try {
831
+ const arg = env.s.pop()._datum;
832
+ const filename = loadfile.resolve_path(arg);
833
+ const x = loadfile.loadsync(filename);
834
+ env.s.push({ _type: 'TC_STR', _datum: x });
835
+ return;
836
+ } catch (e) {
837
+ env.s.push(err.throw(e));
838
+ return;
839
+ }
840
+ }
841
+ env.s.push(err.throw('invalid arguments type'));
842
+ }
843
+
844
+ file_write_func() {
845
+ if (env.TOS() && env.TOS2() && env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
846
+ try {
847
+ const arg = env.s.pop()._datum;
848
+ const filename = loadfile.resolve_path(arg);
849
+ const data = env.s.pop()._datum;
850
+ loadfile.writesync(filename, data);
851
+ return;
852
+ } catch (e) {
853
+ env.s.push(err.throw(e));
854
+ return;
855
+ }
856
+ }
857
+ env.s.push(err.throw('invalid arguments type. 2 Strings are expected.'));
858
+ }
859
+
860
+ file_append_func() {
861
+ if (env.TOS() && env.TOS2() && env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
862
+ try {
863
+ const arg = env.s.pop()._datum;
864
+ const filename = loadfile.resolve_path(arg);
865
+ const data = env.s.pop()._datum;
866
+ loadfile.appendsync(filename, data);
867
+ return;
868
+ } catch (e) {
869
+ env.s.push(err.throw(e));
870
+ return;
871
+ }
872
+ }
873
+ env.s.push(err.throw('invalid arguments type. 2 Strings are expected.'));
874
+ }
875
+
876
+ file_exists_func() {
877
+ if (env.TOS() && env.is_string(env.TOS())) {
878
+ try {
879
+ const arg = env.s.pop()._datum;
880
+ const filename = loadfile.resolve_path(arg);
881
+ const exists = loadfile.existssync(filename);
882
+ env.s.push(env.set_bool_val(exists));
883
+ return;
884
+ } catch (e) {
885
+ env.s.push(err.throw(e));
886
+ return;
887
+ }
888
+ }
889
+ env.s.push(err.throw('invalid arguments type. a String is expected.'));
890
+ }
891
+
892
+ throw_func() {
893
+ if (env.TOS() && env.TOS()._type == 'TC_STR') {
894
+ env.s.push(err.throw(env.s.pop()._datum));
895
+ return;
896
+ }
897
+ if (env.TOS() && env.TOS()._type == 'TC_JSON') {
898
+ const err_arg = env.s.pop();
899
+ env.s.push(err.throw(err_arg._datum.msg, err_arg._datum.code));
900
+ return;
901
+ }
902
+ env.s.push(err.throw('invalid throw argument in TOS'));
903
+ }
904
+
905
+ require_js_func() {
906
+ if (env.TOS() && env.TOS()._type == 'TC_STR') {
907
+ try {
908
+ const arg = env.s.pop()._datum;
909
+ const filename = loadfile.resolve_path(arg);
910
+ const js = import(filename);
911
+ env.s.push({ _type: env.guess_type(js), _datum: js });
912
+ return;
913
+ } catch (e) {
914
+ env.s.push(err.throw(e));
915
+ return;
916
+ }
917
+ }
918
+ env.s.push(err.throw('invalid request-js argument type'));
919
+ }
920
+
921
+ async funcjs_exec_func() {
922
+ try {
923
+ if (env.TOS() && env.TOS()._type == 'TC_LAMBDA_FUNC') {
924
+ await evalx.eval_parsed(env.s.pop()._datum);
925
+ return;
926
+ }
927
+ if (env.TOS() && env.TOS()._type == 'TC_FUNC_JS') {
928
+ var x = env.s.pop()._datum;
929
+ var arity = x.length;
930
+ var args = [];
931
+ // log.info(arity);
932
+ while (arity > 0) {
933
+ args.unshift(env.s.pop()._datum);
934
+ arity--;
935
+ }
936
+ // log.info(args);
937
+ var y = x.apply(null, args);
938
+ // log.info(y);
939
+ if (y) {
940
+ env.s.push({ _type: env.guess_type(y), _datum: y });
941
+ }
942
+ return;
943
+ }
944
+ if (env.TOS() && env.TOS()._type == 'TC_STR' && env.TOS2() && env.TOS2()._type == 'TC_JSON') {
945
+ const method = env.s.pop()._datum;
946
+ const method_list = method.split('.');
947
+ var x = env.s.pop()._datum;
948
+ let funcall = x;
949
+ for (const item of method_list) {
950
+ funcall = funcall[item];
951
+ }
952
+ // log.info(funcall);
953
+ var arity = funcall.length;
954
+ var args = [];
955
+ // log.info(arity);
956
+ while (arity > 0) {
957
+ args.unshift(env.s.pop()._datum);
958
+ arity--;
959
+ }
960
+ // log.info(args);
961
+ var y = funcall.apply(x, args);
962
+ // log.info(y);
963
+ if (y) {
964
+ env.s.push({ _type: env.guess_type(y), _datum: y });
965
+ }
966
+ return;
967
+ }
968
+ env.s.push(err.throw('invalid funcall-js argument type'));
969
+ } catch (e) {
970
+ env.s.push(err.throw(e));
971
+ }
972
+ }
973
+
974
+ handle_repl_func() {
975
+ err.handle_repl();
976
+ }
977
+
978
+ handle_standard_func() {
979
+ err.handle_standard();
980
+ }
981
+
982
+ regex_test_func() {
983
+ try {
984
+ if (env.is_obj(env.TOS()) && env.is_string(env.TOS2())) {
985
+ const rex_obj = env.s.pop();
986
+ let _datum;
987
+ var str = env.s.pop()._datum;
988
+ var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
989
+ const result = my_rex.test(str);
990
+ env.s.push(result ? env.true_obj() : env.false_obj());
991
+ return;
992
+ }
993
+ if (env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
994
+ const rex = env.s.pop()._datum;
995
+ var str = env.s.pop()._datum;
996
+ var my_rex = new RegExp(rex);
997
+ const result = my_rex.test(str);
998
+ env.s.push(result ? env.true_obj() : env.false_obj());
999
+ return;
1000
+ }
1001
+ env.s.push(err.throw('invalid regex test argument type'));
1002
+ } catch (e) {
1003
+ env.s.push(err.throw(e));
1004
+ }
1005
+ }
1006
+
1007
+ regex_exec_func() {
1008
+ try {
1009
+ if (env.is_obj(env.TOS()) && env.is_string(env.TOS2())) {
1010
+ const rex_obj = env.s.pop();
1011
+ let _datum;
1012
+ var str = env.s.pop()._datum;
1013
+ var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
1014
+ const result = my_rex.exec(str);
1015
+ env.s.push(result ? { _type: 'TC_JSON', _datum: result } : 0);
1016
+ return;
1017
+ }
1018
+ if (env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
1019
+ const rex = env.s.pop()._datum;
1020
+ var str = env.s.pop()._datum;
1021
+ var my_rex = new RegExp(rex);
1022
+ const result = my_rex.exec(str);
1023
+ env.s.push(result ? { _type: 'TC_JSON', _datum: result } : 0);
1024
+ return;
1025
+ }
1026
+ env.s.push(err.throw('invalid regex exec argument type'));
1027
+ } catch (e) {
1028
+ env.s.push(err.throw(e));
1029
+ }
1030
+ }
1031
+
1032
+ regex_match_func() {
1033
+ try {
1034
+ if (env.is_obj(env.TOS()) && env.is_string(env.TOS2())) {
1035
+ const rex_obj = env.s.pop();
1036
+ let _datum;
1037
+ var str = env.s.pop()._datum;
1038
+ var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
1039
+ const result = str.match(my_rex);
1040
+ env.s.push(result ? { _type: 'TC_JSON', _datum: result } : 0);
1041
+ return;
1042
+ }
1043
+ if (env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
1044
+ const rex = env.s.pop()._datum;
1045
+ var str = env.s.pop()._datum;
1046
+ var my_rex = new RegExp(rex);
1047
+ const result = str.match(my_rex);
1048
+ env.s.push(result ? { _type: 'TC_JSON', _datum: result } : 0);
1049
+ return;
1050
+ }
1051
+ env.s.push(err.throw('invalid regex match argument type'));
1052
+ } catch (e) {
1053
+ env.s.push(err.throw(e));
1054
+ }
1055
+ }
1056
+
1057
+ regex_search_func() {
1058
+ try {
1059
+ if (env.is_obj(env.TOS()) && env.is_string(env.TOS2())) {
1060
+ const rex_obj = env.s.pop();
1061
+ let _datum;
1062
+ var str = env.s.pop()._datum;
1063
+ var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
1064
+ const result = str.search(my_rex);
1065
+ env.s.push({ _type: 'TC_NUM', _datum: result });
1066
+ return;
1067
+ }
1068
+ if (env.is_string(env.TOS()) && env.is_string(env.TOS2())) {
1069
+ const rex = env.s.pop()._datum;
1070
+ var str = env.s.pop()._datum;
1071
+ var my_rex = new RegExp(rex);
1072
+ const result = str.search(my_rex);
1073
+ env.s.push({ _type: 'TC_NUM', _datum: result });
1074
+ return;
1075
+ }
1076
+ env.s.push(err.throw('invalid regex search argument type'));
1077
+ } catch (e) {
1078
+ env.s.push(err.throw(e));
1079
+ }
1080
+ }
1081
+
1082
+ regex_replace_func() {
1083
+ try {
1084
+ if (env.is_obj(env.TOS()) && env.is_string(env.TOS2()) && env.is_string(env.s.look_at(2))) {
1085
+ const rex_obj = env.s.pop();
1086
+ let _datum;
1087
+ var replace = env.s.pop()._datum;
1088
+ var str = env.s.pop()._datum;
1089
+ var my_rex = new RegExp(rex_obj.rex, rex_obj.flags);
1090
+ const result = str.replace(my_rex, replace);
1091
+ env.s.push({ _type: 'TC_JSON', _datum: result });
1092
+ return;
1093
+ }
1094
+ if (env.is_string(env.TOS()) && env.is_string(env.TOS2()) && env.is_string(env.s.look_at(2))) {
1095
+ const rex = env.s.pop()._datum;
1096
+ var replace = env.s.pop()._datum;
1097
+ var str = env.s.pop()._datum;
1098
+ var my_rex = new RegExp(rex);
1099
+ const result = str.replace(my_rex, replace);
1100
+ env.s.push({ _type: 'TC_STR', _datum: result });
1101
+ return;
1102
+ }
1103
+ env.s.push(err.throw('invalid regex replace argument type'));
1104
+ } catch (e) {
1105
+ env.s.push(err.throw(e));
1106
+ }
1107
+ }
1108
+
1109
+ array_shift_func() {
1110
+ try {
1111
+ if (env.is_list(env.TOS())) {
1112
+ const value = env.s.pop()._datum.shift();
1113
+ const xval = env.adj_bool_val(value);
1114
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
1115
+ return;
1116
+ }
1117
+ env.s.push(err.throw('invalid operation. TOS is not an list'));
1118
+ } catch (e) {
1119
+ env.s.push(err.throw(e));
1120
+ }
1121
+ }
1122
+
1123
+ array_unshift_func() {
1124
+ try {
1125
+ if (env.TOS() && env.is_list(env.TOS2())) {
1126
+ const value = env.s.pop()._datum;
1127
+ const array = env.s.pop()._datum;
1128
+ array.unshift(value);
1129
+ env.s.push({ _type: 'TC_JSON', _datum: array });
1130
+ return;
1131
+ }
1132
+ env.s.push(err.throw('invalid operation. TOS2 is not an list'));
1133
+ } catch (e) {
1134
+ env.s.push(err.throw(e));
1135
+ }
1136
+ }
1137
+
1138
+ async array_each_func() {
1139
+ try {
1140
+ if (env.TOS() && env.TOS()._type == 'TC_LAMBDA_FUNC' && env.is_list(env.TOS2())) {
1141
+ const funcall = env.s.pop()._datum;
1142
+ const array = env.s.pop()._datum;
1143
+ // Vx€array, !!funcall
1144
+ while (array.length > 0) {
1145
+ const value = array.shift();
1146
+ const xval = env.adj_bool_val(value);
1147
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
1148
+ await evalx.eval_parsed(funcall);
1149
+ }
1150
+ return;
1151
+ }
1152
+ env.s.push(err.throw('invalid arguments. TOS should be a LAMBDA function and TOS2 should be a list'));
1153
+ } catch (e) {
1154
+ env.s.push(err.throw(e));
1155
+ }
1156
+ }
1157
+
1158
+ parse_args_func() {
1159
+ try {
1160
+ if (env.is_list(env.TOS())) {
1161
+ const rx_double_dash = /^\-\-[a-zA-Z0-9]+/;
1162
+ const rx_single_dash = /^\-[a-zA-Z0-9]+/;
1163
+ const rx_no_dash = /^[^\-]/;
1164
+ const rx_two_dashes = /^\-\-/;
1165
+ const args = { argv: [] };
1166
+ const params_data = env.s.pop()._datum;
1167
+ // log.info(params_data);
1168
+ let param_found = false;
1169
+ let test_col;
1170
+ let is_dash;
1171
+ const argv = env.lookup('os:argv')._datum._datum;
1172
+ argv.shift();
1173
+ argv.shift();
1174
+ argv.shift();
1175
+ // log.info(argv);
1176
+ while (argv.length > 0) {
1177
+ const item = argv.shift();
1178
+ is_dash = 0;
1179
+ // log.info(item);
1180
+ // if(rx_double_dash.test(item)){
1181
+ // test_col= 1;
1182
+ // is_dash = true;
1183
+ // }
1184
+ // if(rx_single_dash.test(item)) {
1185
+ // test_col = 0;
1186
+ // is_dash = true;
1187
+ // }
1188
+
1189
+ // if(is_dash) {
1190
+ // param_found = false;
1191
+ // for(var param_item of params_data){
1192
+ // log.info(param_item[test_col]);
1193
+ // if(param_item[test_col]=== (test_col === 0)? item.match(rx_single_dash)[0] : item.match(rx_double_dash[0])){
1194
+ // log.info('d: ',param_item);
1195
+ // param_found = true;
1196
+ // break;
1197
+ // }
1198
+ // }
1199
+ // //if ! invalid parameter
1200
+ // if(!param_found) log.info(`invalid parameter ${item}`);
1201
+ // }
1202
+
1203
+ if (rx_double_dash.test(item)) {
1204
+ param_found = false;
1205
+ for (var param_item of params_data) {
1206
+ if (param_item[1] === item.match(rx_double_dash)[0]) {
1207
+ // log.info('dd: ',param_item);
1208
+ param_found = true;
1209
+ switch (param_item[2]) {
1210
+ case 'y':
1211
+ // log.info('ha param');
1212
+ if (argv[0] && rx_no_dash.test(argv[0])) {
1213
+ args[param_item[1].replace(rx_two_dashes, '')] = argv.shift();
1214
+ } else {
1215
+ log.info(`error, parameter ${item} needs value...`);
1216
+ // error needs param...
1217
+ }
1218
+ break;
1219
+ case 'n':
1220
+ // log.info('no param');
1221
+ args[param_item[1].replace(rx_two_dashes, '')] = true;
1222
+ break;
1223
+ case '?':
1224
+ // log.info('opt param');
1225
+ if (argv[0] && rx_no_dash.test(argv[0])) {
1226
+ args[param_item[1].replace(rx_two_dashes, '')] = argv[0];
1227
+ } else {
1228
+ args[param_item[1].replace(rx_two_dashes, '')] = true;
1229
+ }
1230
+ break;
1231
+ default:
1232
+ break;
1233
+ }
1234
+ break;
1235
+ }
1236
+ }
1237
+ // if ! invalid parameter
1238
+ if (!param_found) {
1239
+ log.info(`invalid parameter ${item}`);
1240
+ }
1241
+ } else if (rx_single_dash.test(item)) {
1242
+ param_found = false;
1243
+ for (var param_item of params_data) {
1244
+ if (param_item[0] === item.match(rx_single_dash)[0]) {
1245
+ // log.info('sd: ',param_item);
1246
+ param_found = true;
1247
+ switch (param_item[2]) {
1248
+ case 'y':
1249
+ // log.info('ha param');
1250
+ if (argv[0] && rx_no_dash.test(argv[0])) {
1251
+ args[param_item[1].replace(rx_two_dashes, '')] = argv.shift();
1252
+ } else {
1253
+ log.info(`error, parameter ${item} needs value...`);
1254
+ // error needs param...
1255
+ }
1256
+ break;
1257
+ case 'n':
1258
+ // log.info('no param');
1259
+ args[param_item[1].replace(rx_two_dashes, '')] = true;
1260
+ break;
1261
+ case '?':
1262
+ // log.info('opt param');
1263
+ if (argv[0] && rx_no_dash.test(argv[0])) {
1264
+ args[param_item[1].replace(rx_two_dashes, '')] = argv[0];
1265
+ } else {
1266
+ args[param_item[1].replace(rx_two_dashes, '')] = true;
1267
+ }
1268
+ break;
1269
+ default:
1270
+ break;
1271
+ }
1272
+ break;
1273
+ }
1274
+ }
1275
+ if (!param_found) {
1276
+ log.info(`invalid parameter ${item}`);
1277
+ }
1278
+ } else {
1279
+ // else{
1280
+ // normal argv...
1281
+ args.argv.push(item);
1282
+ }
1283
+ }
1284
+ // log.info(args);
1285
+ env.s.push({ _type: 'TC_JSON', _datum: args });
1286
+ return;
1287
+ }
1288
+ env.s.push(err.throw('invalid arguments. TOS should be a list'));
1289
+ } catch (e) {
1290
+ env.s.push(err.throw(e));
1291
+ }
1292
+ }
1293
+
1294
+ async await_func() {
1295
+ if (env.TOS() && env.TOS()._type === 'TC_PROMISE') {
1296
+ try {
1297
+ const x = await env.s.pop()._datum;
1298
+ env.s.push({ _type: env.guess_type(x), _datum: x });
1299
+ // env.s.pop()._datum.then(x => {
1300
+ // env.s.push({"_type":env.guess_type(x), "_datum":x});
1301
+ // });
1302
+ return;
1303
+ } catch (e) {
1304
+ env.s.push(err.throw(e));
1305
+ return;
1306
+ }
1307
+ }
1308
+ env.s.push(err.throw('invalid arguments type. a Promise was expected.'));
1309
+ }
1310
+
1311
+ string_format_func() {
1312
+ if (env.TOS() && env.TOS2() && env.is_string(env.TOS2()) && env.is_list(env.TOS())) {
1313
+ try {
1314
+ const args_list = env.s.pop()._datum;
1315
+ const fmt = env.s.pop()._datum;
1316
+ env.s.push({ _type: 'TC_STR', _datum: vsprintf(fmt, args_list) });
1317
+ return;
1318
+ } catch (e) {
1319
+ env.s.push(err.throw(e));
1320
+ return;
1321
+ }
1322
+ }
1323
+ env.s.push(err.throw('invalid arguments type. a String and a List in TOS and TOS2 was expected.'));
1324
+ }
1325
+
1326
+ string_length_func() {
1327
+ if (env.TOS() && env.is_string(env.TOS())) {
1328
+ try {
1329
+ const str = env.s.pop()._datum;
1330
+ env.s.push({ _type: 'TC_NUM', _datum: str.length });
1331
+ return;
1332
+ } catch (e) {
1333
+ env.s.push(err.throw(e));
1334
+ return;
1335
+ }
1336
+ }
1337
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1338
+ }
1339
+
1340
+ string_to_upper_func() {
1341
+ if (env.TOS() && env.is_string(env.TOS())) {
1342
+ try {
1343
+ const str = env.s.pop()._datum;
1344
+ env.s.push({ _type: 'TC_STR', _datum: str.toUpperCase() });
1345
+ return;
1346
+ } catch (e) {
1347
+ env.s.push(err.throw(e));
1348
+ return;
1349
+ }
1350
+ }
1351
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1352
+ }
1353
+
1354
+ string_to_lower_func() {
1355
+ if (env.TOS() && env.is_string(env.TOS())) {
1356
+ try {
1357
+ const str = env.s.pop()._datum;
1358
+ env.s.push({ _type: 'TC_STR', _datum: str.toLowerCase() });
1359
+ return;
1360
+ } catch (e) {
1361
+ env.s.push(err.throw(e));
1362
+ return;
1363
+ }
1364
+ }
1365
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1366
+ }
1367
+
1368
+ xml_load_func() {
1369
+ if (env.TOS() && env.is_string(env.TOS())) {
1370
+ try {
1371
+ const str = env.s.pop()._datum;
1372
+ const $ = cheerio.load(str);
1373
+ env.s.push({ _type: env.guess_type($), _datum: $ });
1374
+ return;
1375
+ } catch (e) {
1376
+ env.s.push(err.throw(e));
1377
+ return;
1378
+ }
1379
+ }
1380
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1381
+ }
1382
+
1383
+ xml_loadXML_func() {
1384
+ if (env.TOS() && env.is_string(env.TOS())) {
1385
+ try {
1386
+ const str = env.s.pop()._datum;
1387
+ const $ = cheerio.load(str, { xmlMode: true });
1388
+ env.s.push({ _type: env.guess_type($), _datum: $ });
1389
+ return;
1390
+ } catch (e) {
1391
+ env.s.push(err.throw(e));
1392
+ return;
1393
+ }
1394
+ }
1395
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1396
+ }
1397
+
1398
+ xml_select_func() {
1399
+ if (env.TOS() && env.TOS2() && env.TOS2()._type == 'TC_FUNC_JS' && env.is_string(env.TOS())) {
1400
+ try {
1401
+ const arg = env.s.pop()._datum;
1402
+ const $ = env.s.pop()._datum;
1403
+ const resp = $(arg);
1404
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
1405
+ return;
1406
+ } catch (e) {
1407
+ env.s.push(err.throw(e));
1408
+ return;
1409
+ }
1410
+ }
1411
+ env.s.push(err.throw('invalid arguments type. a XML object was expected.'));
1412
+ }
1413
+
1414
+ xml_get_text_func() {
1415
+ if (env.TOS() && env.is_json(env.TOS())) {
1416
+ try {
1417
+ const $ = env.s.pop()._datum;
1418
+ const resp = $.text();
1419
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
1420
+ return;
1421
+ } catch (e) {
1422
+ env.s.push(err.throw(e));
1423
+ return;
1424
+ }
1425
+ }
1426
+ env.s.push(err.throw('invalid arguments type. a JSON object was expected.'));
1427
+ }
1428
+
1429
+ xml_get_html_func() {
1430
+ if (env.TOS() && env.is_json(env.TOS())) {
1431
+ try {
1432
+ const $ = env.s.pop()._datum;
1433
+ const resp = $.html();
1434
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
1435
+ return;
1436
+ } catch (e) {
1437
+ env.s.push(err.throw(e));
1438
+ return;
1439
+ }
1440
+ }
1441
+ env.s.push(err.throw('invalid arguments type. a JSON object was expected.'));
1442
+ }
1443
+
1444
+ xml_get_attr_func() {
1445
+ if (env.TOS() && env.is_string(env.TOS()) && env.TOS2() && env.is_json(env.TOS2())) {
1446
+ try {
1447
+ const attr = env.s.pop()._datum;
1448
+ const $ = env.s.pop()._datum;
1449
+ const resp = $.attr(attr);
1450
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
1451
+ return;
1452
+ } catch (e) {
1453
+ env.s.push(err.throw(e));
1454
+ return;
1455
+ }
1456
+ }
1457
+ env.s.push(err.throw('invalid arguments type. a JSON object was expected.'));
1458
+ }
1459
+
1460
+ xml_has_class_func() {
1461
+ if (env.TOS() && env.is_string(env.TOS()) && env.TOS2() && env.is_json(env.TOS2())) {
1462
+ try {
1463
+ const attr = env.s.pop()._datum;
1464
+ const $ = env.s.pop()._datum;
1465
+ const resp = $.hasClass(attr);
1466
+ env.s.push(env.set_bool_val(resp));
1467
+ return;
1468
+ } catch (e) {
1469
+ env.s.push(err.throw(e));
1470
+ return;
1471
+ }
1472
+ }
1473
+ env.s.push(err.throw('invalid arguments type. a JSON object was expected.'));
1474
+ }
1475
+
1476
+ xml_get_val_func() {
1477
+ if (env.TOS() && env.is_json(env.TOS())) {
1478
+ try {
1479
+ const $ = env.s.pop()._datum;
1480
+ const resp = $.val();
1481
+ env.s.push({ _type: env.guess_type(resp), _datum: resp });
1482
+ return;
1483
+ } catch (e) {
1484
+ env.s.push(err.throw(e));
1485
+ return;
1486
+ }
1487
+ }
1488
+ env.s.push(err.throw('invalid arguments type. a JSON object was expected.'));
1489
+ }
1490
+
1491
+ os_exec() {
1492
+ if (env.TOS() && env.is_string(env.TOS())) {
1493
+ try {
1494
+ const str = env.s.pop()._datum;
1495
+ const output = execSync(str).toString();
1496
+ env.s.push({ _type: env.guess_type(output), _datum: output });
1497
+ return;
1498
+ } catch (e) {
1499
+ env.s.push(err.throw(e));
1500
+ return;
1501
+ }
1502
+ }
1503
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1504
+ }
1505
+
1506
+ os_exec_i() {
1507
+ if (env.TOS() && env.is_string(env.TOS())) {
1508
+ try {
1509
+ const str = env.s.pop()._datum;
1510
+ execSync(str, { stdio: 'inherit' });
1511
+ return;
1512
+ } catch (e) {
1513
+ env.s.push(err.throw(e));
1514
+ return;
1515
+ }
1516
+ }
1517
+ env.s.push(err.throw('invalid arguments type. a String was expected.'));
1518
+ }
1519
+
1520
+ put_r_stack() {
1521
+ if (env.TOS()) {
1522
+ try {
1523
+ const element = env.s.pop();
1524
+ env.s.rpush(element);
1525
+ return;
1526
+ } catch (e) {
1527
+ env.s.push(err.throw(e));
1528
+ return;
1529
+ }
1530
+ }
1531
+ env.s.push(err.throw('empty stack. Cannot move to r-stack.'));
1532
+ }
1533
+
1534
+ get_r_stack() {
1535
+ if (env.RTOS()) {
1536
+ try {
1537
+ const element = env.s.rpop();
1538
+ env.s.push(element);
1539
+ return;
1540
+ } catch (e) {
1541
+ env.s.push(err.throw(e));
1542
+ return;
1543
+ }
1544
+ }
1545
+ env.s.push(err.throw('empty r-stack. Cannot move to stack.'));
1546
+ }
1547
+
1548
+ list_head() {
1549
+ if (env.is_list(env.TOS())) {
1550
+ try {
1551
+ const value = env.s.pop()._datum[0];
1552
+ const xval = env.adj_bool_val(value);
1553
+ env.s.push({ _type: env.guess_type(value), _datum: xval });
1554
+ return;
1555
+ } catch (e) {
1556
+ env.s.push(err.throw(e));
1557
+ return;
1558
+ }
1559
+ }
1560
+ env.s.push(err.throw('invalid arguments type. TOS not a list'));
1561
+ }
1562
+
1563
+ list_tail() {
1564
+ if (env.is_list(env.TOS())) {
1565
+ try {
1566
+ const value = env.s.pop()._datum;
1567
+ value.shift();
1568
+ // const xval=env.adj_bool_val(value);
1569
+ env.s.push({ _type: env.guess_type(value), _datum: value });
1570
+ return;
1571
+ } catch (e) {
1572
+ env.s.push(err.throw(e));
1573
+ return;
1574
+ }
1575
+ }
1576
+ env.s.push(err.throw('invalid arguments type. TOS not a list'));
1577
+ }
1578
+
1579
+ string_to_number_func() {
1580
+ if (env.is_string(env.TOS())) {
1581
+ try {
1582
+ let value = env.s.pop()._datum;
1583
+ value = Number(value);
1584
+ if (isNaN(value)) {
1585
+ throw 'NaN';
1586
+ }
1587
+ env.s.push({ _type: env.guess_type(value), _datum: value });
1588
+ return;
1589
+ } catch (e) {
1590
+ env.s.push(err.throw(e));
1591
+ return;
1592
+ }
1593
+ }
1594
+ env.s.push(err.throw('invalid arguments type. TOS not a string'));
1595
+ }
1596
+
1597
+ populate_repl() {
1598
+ env.set('handle', { _type: 'TC_NATIVE_FUNC', _datum: this.handle_repl_func }, 'TC_WORD');
1599
+ }
1600
+
1601
+ populate() {
1602
+ env.set('pippo', { _type: 'TC_NATIVE_FUNC', _datum: this.test_func }, 'TC_WORD');
1603
+ env.set('bye', { _type: 'TC_NATIVE_FUNC', _datum: this.bye_func }, 'TC_WORD');
1604
+ env.set('noop', { _type: 'TC_NATIVE_FUNC', _datum: this.noop_func }, 'TC_WORD');
1605
+ env.set('.s', { _type: 'TC_NATIVE_FUNC', _datum: this.print_stack_func }, 'TC_WORD');
1606
+ env.set('.e', { _type: 'TC_NATIVE_FUNC', _datum: this.print_env_func }, 'TC_WORD');
1607
+ env.set('words', { _type: 'TC_NATIVE_FUNC', _datum: this.print_words_func }, 'TC_WORD');
1608
+ env.set('emit', { _type: 'TC_NATIVE_FUNC', _datum: this.emit_func }, 'TC_WORD');
1609
+ env.set('.', { _type: 'TC_NATIVE_FUNC', _datum: this.print_tos_func }, 'TC_WORD');
1610
+ env.set('.?', { _type: 'TC_NATIVE_FUNC', _datum: this.print_debug_tos_func }, 'TC_WORD');
1611
+ env.set('!', { _type: 'TC_NATIVE_FUNC', _datum: this.assign_var_func }, 'TC_WORD');
1612
+ env.set('@', { _type: 'TC_NATIVE_FUNC', _datum: this.read_var_func }, 'TC_WORD');
1613
+ env.set('not', { _type: 'TC_NATIVE_FUNC', _datum: this.not_func }, 'TC_WORD');
1614
+ env.set('and', { _type: 'TC_NATIVE_FUNC', _datum: this.and_func }, 'TC_WORD');
1615
+ env.set('or', { _type: 'TC_NATIVE_FUNC', _datum: this.or_func }, 'TC_WORD');
1616
+ env.set('is_num', { _type: 'TC_NATIVE_FUNC', _datum: this.is_num_func }, 'TC_WORD');
1617
+ env.set('is_string', { _type: 'TC_NATIVE_FUNC', _datum: this.is_string_func }, 'TC_WORD');
1618
+ env.set('is_list', { _type: 'TC_NATIVE_FUNC', _datum: this.is_list_func }, 'TC_WORD');
1619
+ env.set('is_falsy', { _type: 'TC_NATIVE_FUNC', _datum: this.is_falsy_func }, 'TC_WORD');
1620
+ env.set('dup', { _type: 'TC_NATIVE_FUNC', _datum: this.dup_func }, 'TC_WORD');
1621
+ env.set('swap', { _type: 'TC_NATIVE_FUNC', _datum: this.swap_func }, 'TC_WORD');
1622
+ env.set('drop', { _type: 'TC_NATIVE_FUNC', _datum: this.drop_func }, 'TC_WORD');
1623
+ env.set('ndrop', { _type: 'TC_NATIVE_FUNC', _datum: this.ndrop_func }, 'TC_WORD');
1624
+ env.set('nbye', { _type: 'TC_NATIVE_FUNC', _datum: this.nbye_func }, 'TC_WORD');
1625
+ env.set('over', { _type: 'TC_NATIVE_FUNC', _datum: this.over_func }, 'TC_WORD');
1626
+ env.set('n:+', { _type: 'TC_NATIVE_FUNC', _datum: this.num_plus_func }, 'TC_WORD');
1627
+ env.set('n:-', { _type: 'TC_NATIVE_FUNC', _datum: this.num_minus_func }, 'TC_WORD');
1628
+ env.set('n:*', { _type: 'TC_NATIVE_FUNC', _datum: this.num_times_func }, 'TC_WORD');
1629
+ env.set('n:/', { _type: 'TC_NATIVE_FUNC', _datum: this.num_div_func }, 'TC_WORD');
1630
+ env.set('+', { _type: 'TC_NATIVE_FUNC', _datum: this.plus_func }, 'TC_WORD');
1631
+ env.set('-', { _type: 'TC_NATIVE_FUNC', _datum: this.minus_func }, 'TC_WORD');
1632
+ env.set('*', { _type: 'TC_NATIVE_FUNC', _datum: this.times_func }, 'TC_WORD');
1633
+ env.set('/', { _type: 'TC_NATIVE_FUNC', _datum: this.division_func }, 'TC_WORD');
1634
+ env.set('%', { _type: 'TC_NATIVE_FUNC', _datum: this.module_func }, 'TC_WORD');
1635
+ env.set('handle', { _type: 'TC_NATIVE_FUNC', _datum: this.handle_standard_func }, 'TC_WORD');
1636
+ env.set('throw', { _type: 'TC_NATIVE_FUNC', _datum: this.throw_func }, 'TC_WORD');
1637
+ env.set('s:+', { _type: 'TC_NATIVE_FUNC', _datum: this.string_plus_func }, 'TC_WORD');
1638
+ env.set('a:+', { _type: 'TC_NATIVE_FUNC', _datum: this.array_plus_func }, 'TC_WORD');
1639
+ env.set('included', { _type: 'TC_NATIVE_FUNC', _datum: this.included_func }, 'TC_WORD');
1640
+ env.set('a:@', { _type: 'TC_NATIVE_FUNC', _datum: this.array_at_func }, 'TC_WORD');
1641
+ env.set('a:!', { _type: 'TC_NATIVE_FUNC', _datum: this.array_set_at_func }, 'TC_WORD');
1642
+ env.set('m:@', { _type: 'TC_NATIVE_FUNC', _datum: this.object_at_func }, 'TC_WORD');
1643
+ env.set('m:!', { _type: 'TC_NATIVE_FUNC', _datum: this.object_set_at_func }, 'TC_WORD');
1644
+ env.set('a:len', { _type: 'TC_NATIVE_FUNC', _datum: this.array_length_func }, 'TC_WORD');
1645
+ env.set('a:push', { _type: 'TC_NATIVE_FUNC', _datum: this.array_push_func }, 'TC_WORD');
1646
+ env.set('a:pop', { _type: 'TC_NATIVE_FUNC', _datum: this.array_pop_func }, 'TC_WORD');
1647
+ env.set('m:keys', { _type: 'TC_NATIVE_FUNC', _datum: this.object_keys_func }, 'TC_WORD');
1648
+ env.set('m:values', { _type: 'TC_NATIVE_FUNC', _datum: this.object_values_func }, 'TC_WORD');
1649
+ env.set('s:split', { _type: 'TC_NATIVE_FUNC', _datum: this.string_split_func }, 'TC_WORD');
1650
+ env.set('s:join', { _type: 'TC_NATIVE_FUNC', _datum: this.string_join_func }, 'TC_WORD');
1651
+ env.set('j:stringify', { _type: 'TC_NATIVE_FUNC', _datum: this.json_stringify_func }, 'TC_WORD');
1652
+ env.set('j:parse', { _type: 'TC_NATIVE_FUNC', _datum: this.json_parse_func }, 'TC_WORD');
1653
+ env.set('s:@', { _type: 'TC_NATIVE_FUNC', _datum: this.string_at_func }, 'TC_WORD');
1654
+ env.set('s:!', { _type: 'TC_NATIVE_FUNC', _datum: this.string_set_at_func }, 'TC_WORD');
1655
+ env.set('=', { _type: 'TC_NATIVE_FUNC', _datum: this.equal_func }, 'TC_WORD');
1656
+ env.set('===', { _type: 'TC_NATIVE_FUNC', _datum: this.equal_func }, 'TC_WORD');
1657
+ env.set('==', { _type: 'TC_NATIVE_FUNC', _datum: this.eq_func }, 'TC_WORD');
1658
+ env.set('<', { _type: 'TC_NATIVE_FUNC', _datum: this.num_minor_func }, 'TC_WORD');
1659
+ env.set('>', { _type: 'TC_NATIVE_FUNC', _datum: this.num_major_func }, 'TC_WORD');
1660
+ env.set('<=', { _type: 'TC_NATIVE_FUNC', _datum: this.num_min_eq_func }, 'TC_WORD');
1661
+ env.set('>=', { _type: 'TC_NATIVE_FUNC', _datum: this.num_maj_eq_func }, 'TC_WORD');
1662
+ env.set('f:slurp', { _type: 'TC_NATIVE_FUNC', _datum: this.file_slurp_func }, 'TC_WORD');
1663
+ env.set('f:write', { _type: 'TC_NATIVE_FUNC', _datum: this.file_write_func }, 'TC_WORD');
1664
+ env.set('f:append', { _type: 'TC_NATIVE_FUNC', _datum: this.file_append_func }, 'TC_WORD');
1665
+ env.set('f:exists', { _type: 'TC_NATIVE_FUNC', _datum: this.file_exists_func }, 'TC_WORD');
1666
+ env.set('net:request', { _type: 'TC_NATIVE_FUNC', _datum: this.net_request_func }, 'TC_WORD');
1667
+ env.set('j:require-js', { _type: 'TC_NATIVE_FUNC', _datum: this.require_js_func }, 'TC_WORD');
1668
+ env.set('!!', { _type: 'TC_NATIVE_FUNC', _datum: this.funcjs_exec_func }, 'TC_WORD');
1669
+ env.set('G:delete', { _type: 'TC_NATIVE_FUNC', _datum: this.delete_dict_func }, 'TC_WORD');
1670
+ env.set('rx:test', { _type: 'TC_NATIVE_FUNC', _datum: this.regex_test_func }, 'TC_WORD');
1671
+ env.set('rx:exec', { _type: 'TC_NATIVE_FUNC', _datum: this.regex_exec_func }, 'TC_WORD');
1672
+ env.set('rx:match', { _type: 'TC_NATIVE_FUNC', _datum: this.regex_match_func }, 'TC_WORD');
1673
+ env.set('rx:search', { _type: 'TC_NATIVE_FUNC', _datum: this.regex_search_func }, 'TC_WORD');
1674
+ env.set('rx:replace', { _type: 'TC_NATIVE_FUNC', _datum: this.regex_replace_func }, 'TC_WORD');
1675
+ env.set('a:shift', { _type: 'TC_NATIVE_FUNC', _datum: this.array_shift_func }, 'TC_WORD');
1676
+ env.set('a:unshift', { _type: 'TC_NATIVE_FUNC', _datum: this.array_unshift_func }, 'TC_WORD');
1677
+ env.set('a:each', { _type: 'TC_NATIVE_FUNC', _datum: this.array_each_func }, 'TC_WORD');
1678
+ env.set('os:parse-args', { _type: 'TC_NATIVE_FUNC', _datum: this.parse_args_func }, 'TC_WORD');
1679
+ env.set('await', { _type: 'TC_NATIVE_FUNC', _datum: this.await_func }, 'TC_WORD');
1680
+ env.set('s:format', { _type: 'TC_NATIVE_FUNC', _datum: this.string_format_func }, 'TC_WORD');
1681
+ env.set('s:len', { _type: 'TC_NATIVE_FUNC', _datum: this.string_length_func }, 'TC_WORD');
1682
+ env.set('s:to_upper', { _type: 'TC_NATIVE_FUNC', _datum: this.string_to_upper_func }, 'TC_WORD');
1683
+ env.set('s:to_lower', { _type: 'TC_NATIVE_FUNC', _datum: this.string_to_lower_func }, 'TC_WORD');
1684
+ env.set('xml:loadDOM', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_load_func }, 'TC_WORD');
1685
+ env.set('xml:loadXML', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_loadXML_func }, 'TC_WORD');
1686
+ env.set('xml:$', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_select_func }, 'TC_WORD');
1687
+ env.set('xml:get_text', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_get_text_func }, 'TC_WORD');
1688
+ env.set('xml:get_html', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_get_html_func }, 'TC_WORD');
1689
+ env.set('xml:get_attr', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_get_attr_func }, 'TC_WORD');
1690
+ env.set('xml:has_class', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_has_class_func }, 'TC_WORD');
1691
+ env.set('xml:get_val', { _type: 'TC_NATIVE_FUNC', _datum: this.xml_get_val_func }, 'TC_WORD');
1692
+ env.set('os:exec', { _type: 'TC_NATIVE_FUNC', _datum: this.os_exec }, 'TC_WORD');
1693
+ env.set('os:exec-i', { _type: 'TC_NATIVE_FUNC', _datum: this.os_exec_i }, 'TC_WORD');
1694
+ env.set('>r', { _type: 'TC_NATIVE_FUNC', _datum: this.put_r_stack }, 'TC_WORD');
1695
+ env.set('r>', { _type: 'TC_NATIVE_FUNC', _datum: this.get_r_stack }, 'TC_WORD');
1696
+ env.set('a:head', { _type: 'TC_NATIVE_FUNC', _datum: this.list_head }, 'TC_WORD');
1697
+ env.set('a:tail', { _type: 'TC_NATIVE_FUNC', _datum: this.list_tail }, 'TC_WORD');
1698
+ env.set('s:to_num', { _type: 'TC_NATIVE_FUNC', _datum: this.string_to_number_func }, 'TC_WORD');
1699
+ }
1700
+ }
1701
+
1702
+ export default new NativeLib();