-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.js
1504 lines (1379 loc) · 46 KB
/
repl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QuickJS Read Eval Print Loop
*
* Copyright (c) 2017-2019 Fabrice Bellard
* Copyright (c) 2017-2019 Charlie Gordon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
"use strip";
import * as std from "std";
import * as os from "os";
(function(g) {
/* add a 'global' binding */
g.global = std.global;
/* close global objects */
var Object = g.Object;
var String = g.String;
var Array = g.Array;
var Date = g.Date;
var Math = g.Math;
var isFinite = g.isFinite;
var parseFloat = g.parseFloat;
/* XXX: use preprocessor ? */
var config_numcalc = (typeof os.open === "undefined");
var has_jscalc = (typeof Fraction === "function");
var has_bignum = (typeof BigFloat === "function");
var colors = {
none: "\x1b[0m",
black: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m",
gray: "\x1b[30;1m",
grey: "\x1b[30;1m",
bright_red: "\x1b[31;1m",
bright_green: "\x1b[32;1m",
bright_yellow: "\x1b[33;1m",
bright_blue: "\x1b[34;1m",
bright_magenta: "\x1b[35;1m",
bright_cyan: "\x1b[36;1m",
bright_white: "\x1b[37;1m",
};
var styles;
if (config_numcalc) {
styles = {
'default': 'black',
'comment': 'white',
'string': 'green',
'regex': 'cyan',
'number': 'green',
'keyword': 'blue',
'function': 'gray',
'type': 'bright_magenta',
'identifier': 'yellow',
'error': 'bright_red',
'result': 'black',
'error_msg': 'bright_red',
};
} else {
styles = {
'default': 'bright_green',
'comment': 'white',
'string': 'bright_cyan',
'regex': 'cyan',
'number': 'green',
'keyword': 'bright_white',
'function': 'bright_yellow',
'type': 'bright_magenta',
'identifier': 'bright_green',
'error': 'red',
'result': 'bright_white',
'error_msg': 'bright_red',
};
}
var history = [];
var clip_board = "";
var prec;
var expBits;
var log2_10;
var pstate = "";
var prompt = "";
var plen = 0;
var ps1;
if (config_numcalc)
ps1 = "> ";
else
ps1 = "qjs > ";
var ps2 = " ... ";
var utf8 = true;
var show_time = false;
var show_colors = true;
var eval_time = 0;
var mexpr = "";
var level = 0;
var cmd = "";
var cursor_pos = 0;
var last_cmd = "";
var last_cursor_pos = 0;
var history_index;
var this_fun, last_fun;
var quote_flag = false;
var utf8_state = 0;
var utf8_val = 0;
var term_fd;
var term_read_buf;
var term_width;
/* current X position of the cursor in the terminal */
var term_cursor_x = 0;
function termInit() {
var tab;
term_fd = std.in.fileno();
/* get the terminal size */
term_width = 80;
if (os.isatty(term_fd)) {
if (os.ttyGetWinSize) {
tab = os.ttyGetWinSize(term_fd);
if (tab)
term_width = tab[0];
}
if (os.ttySetRaw) {
/* set the TTY to raw mode */
os.ttySetRaw(term_fd);
}
}
/* install a Ctrl-C signal handler */
os.signal(os.SIGINT, sigint_handler);
/* install a handler to read stdin */
term_read_buf = new Uint8Array(64);
os.setReadHandler(term_fd, term_read_handler);
}
function sigint_handler() {
/* send Ctrl-C to readline */
handle_byte(3);
}
function term_read_handler() {
var l, i;
l = os.read(term_fd, term_read_buf.buffer, 0, term_read_buf.length);
for(i = 0; i < l; i++)
handle_byte(term_read_buf[i]);
}
function handle_byte(c) {
if (!utf8) {
handle_char(c);
} else if (utf8_state !== 0 && (c >= 0x80 && c < 0xc0)) {
utf8_val = (utf8_val << 6) | (c & 0x3F);
utf8_state--;
if (utf8_state === 0) {
handle_char(utf8_val);
}
} else if (c >= 0xc0 && c < 0xf8) {
utf8_state = 1 + (c >= 0xe0) + (c >= 0xf0);
utf8_val = c & ((1 << (6 - utf8_state)) - 1);
} else {
utf8_state = 0;
handle_char(c);
}
}
function is_alpha(c) {
return typeof c === "string" &&
((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
function is_digit(c) {
return typeof c === "string" && (c >= '0' && c <= '9');
}
function is_word(c) {
return typeof c === "string" &&
(is_alpha(c) || is_digit(c) || c == '_' || c == '$');
}
function is_balanced(a, b) {
switch (a + b) {
case "()":
case "[]":
case "{}":
return true;
}
return false;
}
function print_color_text(str, start, style_names) {
var i, j;
for (j = start; j < str.length;) {
var style = style_names[i = j];
while (++j < str.length && style_names[j] == style)
continue;
std.puts(colors[styles[style] || 'default']);
std.puts(str.substring(i, j));
std.puts(colors['none']);
}
}
function print_csi(n, code) {
std.puts("\x1b[" + ((n != 1) ? n : "") + code);
}
function move_cursor(delta) {
var i, l;
if (delta > 0) {
while (delta != 0) {
if (term_cursor_x == (term_width - 1)) {
std.puts("\n"); /* translated to CRLF */
term_cursor_x = 0;
delta--;
} else {
l = Math.min(term_width - 1 - term_cursor_x, delta);
print_csi(l, "C"); /* right */
delta -= l;
term_cursor_x += l;
}
}
} else {
delta = -delta;
while (delta != 0) {
if (term_cursor_x == 0) {
print_csi(1, "A"); /* up */
print_csi(term_width - 1, "C"); /* right */
delta--;
term_cursor_x = term_width - 1;
} else {
l = Math.min(delta, term_cursor_x);
print_csi(l, "D"); /* left */
delta -= l;
term_cursor_x -= l;
}
}
}
}
function update() {
var i;
if (cmd != last_cmd) {
if (!show_colors && last_cmd.substring(0, last_cursor_pos) == cmd.substring(0, last_cursor_pos)) {
/* optimize common case */
std.puts(cmd.substring(last_cursor_pos));
} else {
/* goto the start of the line */
move_cursor(-last_cursor_pos);
if (show_colors) {
var str = mexpr ? mexpr + '\n' + cmd : cmd;
var start = str.length - cmd.length;
var colorstate = colorize_js(str);
print_color_text(str, start, colorstate[2]);
} else {
std.puts(cmd);
}
}
/* Note: assuming no surrogate pairs */
term_cursor_x = (term_cursor_x + cmd.length) % term_width;
if (term_cursor_x == 0) {
/* show the cursor on the next line */
std.puts(" \x08");
}
/* remove the trailing characters */
std.puts("\x1b[J");
last_cmd = cmd;
last_cursor_pos = cmd.length;
}
move_cursor(cursor_pos - last_cursor_pos);
last_cursor_pos = cursor_pos;
std.out.flush();
}
/* editing commands */
function insert(str) {
if (str) {
cmd = cmd.substring(0, cursor_pos) + str + cmd.substring(cursor_pos);
cursor_pos += str.length;
}
}
function quoted_insert() {
quote_flag = true;
}
function abort() {
cmd = "";
cursor_pos = 0;
return -2;
}
function alert() {
}
function beginning_of_line() {
cursor_pos = 0;
}
function end_of_line() {
cursor_pos = cmd.length;
}
function forward_char() {
if (cursor_pos < cmd.length)
cursor_pos++;
}
function backward_char() {
if (cursor_pos > 0)
cursor_pos--;
}
function skip_word_forward(pos) {
while (pos < cmd.length && !is_word(cmd.charAt(pos)))
pos++;
while (pos < cmd.length && is_word(cmd.charAt(pos)))
pos++;
return pos;
}
function skip_word_backward(pos) {
while (pos > 0 && !is_word(cmd.charAt(pos - 1)))
pos--;
while (pos > 0 && is_word(cmd.charAt(pos - 1)))
pos--;
return pos;
}
function forward_word() {
cursor_pos = skip_word_forward(cursor_pos);
}
function backward_word() {
cursor_pos = skip_word_backward(cursor_pos);
}
function accept_line() {
std.puts("\n");
history_add(cmd);
return -1;
}
function history_add(str) {
if (str) {
history.push(str);
}
history_index = history.length;
}
function previous_history() {
if (history_index > 0) {
if (history_index == history.length) {
history.push(cmd);
}
history_index--;
cmd = history[history_index];
cursor_pos = cmd.length;
}
}
function next_history() {
if (history_index < history.length - 1) {
history_index++;
cmd = history[history_index];
cursor_pos = cmd.length;
}
}
function history_search(dir) {
var pos = cursor_pos;
for (var i = 1; i <= history.length; i++) {
var index = (history.length + i * dir + history_index) % history.length;
if (history[index].substring(0, pos) == cmd.substring(0, pos)) {
history_index = index;
cmd = history[index];
return;
}
}
}
function history_search_backward() {
return history_search(-1);
}
function history_search_forward() {
return history_search(1);
}
function delete_char_dir(dir) {
var start = cursor_pos - (dir < 0);
var end = start + 1;
if (start >= 0 && start < cmd.length) {
if (last_fun === kill_region) {
kill_region(start, end, dir);
} else {
cmd = cmd.substring(0, start) + cmd.substring(end);
cursor_pos = start;
}
}
}
function delete_char() {
delete_char_dir(1);
}
function control_d() {
if (cmd.length == 0) {
std.puts("\n");
return -3; /* exit read eval print loop */
} else {
delete_char_dir(1);
}
}
function backward_delete_char() {
delete_char_dir(-1);
}
function transpose_chars() {
var pos = cursor_pos;
if (cmd.length > 1 && pos > 0) {
if (pos == cmd.length)
pos--;
cmd = cmd.substring(0, pos - 1) + cmd.substring(pos, pos + 1) +
cmd.substring(pos - 1, pos) + cmd.substring(pos + 1);
cursor_pos = pos + 1;
}
}
function transpose_words() {
var p1 = skip_word_backward(cursor_pos);
var p2 = skip_word_forward(p1);
var p4 = skip_word_forward(cursor_pos);
var p3 = skip_word_backward(p4);
if (p1 < p2 && p2 <= cursor_pos && cursor_pos <= p3 && p3 < p4) {
cmd = cmd.substring(0, p1) + cmd.substring(p3, p4) +
cmd.substring(p2, p3) + cmd.substring(p1, p2);
cursor_pos = p4;
}
}
function upcase_word() {
var end = skip_word_forward(cursor_pos);
cmd = cmd.substring(0, cursor_pos) +
cmd.substring(cursor_pos, end).toUpperCase() +
cmd.substring(end);
}
function downcase_word() {
var end = skip_word_forward(cursor_pos);
cmd = cmd.substring(0, cursor_pos) +
cmd.substring(cursor_pos, end).toLowerCase() +
cmd.substring(end);
}
function kill_region(start, end, dir) {
var s = cmd.substring(start, end);
if (last_fun !== kill_region)
clip_board = s;
else if (dir < 0)
clip_board = s + clip_board;
else
clip_board = clip_board + s;
cmd = cmd.substring(0, start) + cmd.substring(end);
if (cursor_pos > end)
cursor_pos -= end - start;
else if (cursor_pos > start)
cursor_pos = start;
this_fun = kill_region;
}
function kill_line() {
kill_region(cursor_pos, cmd.length, 1);
}
function backward_kill_line() {
kill_region(0, cursor_pos, -1);
}
function kill_word() {
kill_region(cursor_pos, skip_word_forward(cursor_pos), 1);
}
function backward_kill_word() {
kill_region(skip_word_backward(cursor_pos), cursor_pos, -1);
}
function yank() {
insert(clip_board);
}
function control_c() {
if (last_fun === control_c) {
std.puts("\n");
std.exit(0);
} else {
std.puts("\n(Press Ctrl-C again to quit)\n");
readline_print_prompt();
}
}
function reset() {
cmd = "";
cursor_pos = 0;
}
function get_context_word(line, pos) {
var s = "";
while (pos > 0 && is_word(line[pos - 1])) {
pos--;
s = line[pos] + s;
}
return s;
}
function get_context_object(line, pos) {
var obj, base, c;
if (pos <= 0 || " ~!%^&*(-+={[|:;,<>?/".indexOf(line[pos - 1]) >= 0)
return g;
if (pos >= 2 && line[pos - 1] === ".") {
pos--;
obj = {};
switch (c = line[pos - 1]) {
case '\'':
case '\"':
return "a";
case ']':
return [];
case '}':
return {};
case '/':
return / /;
default:
if (is_word(c)) {
base = get_context_word(line, pos);
if (["true", "false", "null", "this"].includes(base) || !isNaN(+base))
return eval(base);
obj = get_context_object(line, pos - base.length);
if (obj === null || obj === void 0)
return obj;
else
return obj[base];
}
return {};
}
}
return void 0;
}
function get_completions(line, pos) {
var s, obj, ctx_obj, r, i, j, paren;
s = get_context_word(line, pos);
ctx_obj = get_context_object(line, pos - s.length);
r = [];
/* enumerate properties from object and its prototype chain,
add non-numeric regular properties with s as e prefix
*/
for (i = 0, obj = ctx_obj; i < 10 && obj !== null && obj !== void 0; i++) {
var props = Object.getOwnPropertyNames(obj);
/* add non-numeric regular properties */
for (j = 0; j < props.length; j++) {
var prop = props[j];
if (typeof prop == "string" && ""+(+prop) != prop && prop.startsWith(s))
r.push(prop);
}
obj = Object.getPrototypeOf(obj);
}
if (r.length > 1) {
/* sort list with internal names last and remove duplicates */
function symcmp(a, b) {
if (a[0] != b[0]) {
if (a[0] == '_')
return 1;
if (b[0] == '_')
return -1;
}
if (a < b)
return -1;
if (a > b)
return +1;
return 0;
}
r.sort(symcmp);
for(i = j = 1; i < r.length; i++) {
if (r[i] != r[i - 1])
r[j++] = r[i];
}
r.length = j;
}
/* 'tab' = list of completions, 'pos' = cursor position inside
the completions */
return { tab: r, pos: s.length, ctx: ctx_obj };
}
function completion() {
var tab, res, s, i, j, len, t, max_width, col, n_cols, row, n_rows;
res = get_completions(cmd, cursor_pos);
tab = res.tab;
if (tab.length === 0)
return;
s = tab[0];
len = s.length;
/* add the chars which are identical in all the completions */
for(i = 1; i < tab.length; i++) {
t = tab[i];
for(j = 0; j < len; j++) {
if (t[j] !== s[j]) {
len = j;
break;
}
}
}
for(i = res.pos; i < len; i++) {
insert(s[i]);
}
if (last_fun === completion && tab.length == 1) {
/* append parentheses to function names */
var m = res.ctx[tab[0]];
if (typeof m == "function") {
insert('(');
if (m.length == 0)
insert(')');
} else if (typeof m == "object") {
insert('.');
}
}
/* show the possible completions */
if (last_fun === completion && tab.length >= 2) {
max_width = 0;
for(i = 0; i < tab.length; i++)
max_width = Math.max(max_width, tab[i].length);
max_width += 2;
n_cols = Math.max(1, Math.floor((term_width + 1) / max_width));
n_rows = Math.ceil(tab.length / n_cols);
std.puts("\n");
/* display the sorted list column-wise */
for (row = 0; row < n_rows; row++) {
for (col = 0; col < n_cols; col++) {
i = col * n_rows + row;
if (i >= tab.length)
break;
s = tab[i];
if (col != n_cols - 1)
s = s.padEnd(max_width);
std.puts(s);
}
std.puts("\n");
}
/* show a new prompt */
readline_print_prompt();
}
}
var commands = { /* command table */
"\x01": beginning_of_line, /* ^A - bol */
"\x02": backward_char, /* ^B - backward-char */
"\x03": control_c, /* ^C - abort */
"\x04": control_d, /* ^D - delete-char or exit */
"\x05": end_of_line, /* ^E - eol */
"\x06": forward_char, /* ^F - forward-char */
"\x07": abort, /* ^G - bell */
"\x08": backward_delete_char, /* ^H - backspace */
"\x09": completion, /* ^I - history-search-backward */
"\x0a": accept_line, /* ^J - newline */
"\x0b": kill_line, /* ^K - delete to end of line */
"\x0d": accept_line, /* ^M - enter */
"\x0e": next_history, /* ^N - down */
"\x10": previous_history, /* ^P - up */
"\x11": quoted_insert, /* ^Q - quoted-insert */
"\x12": alert, /* ^R - reverse-search */
"\x13": alert, /* ^S - search */
"\x14": transpose_chars, /* ^T - transpose */
"\x18": reset, /* ^X - cancel */
"\x19": yank, /* ^Y - yank */
"\x1bOA": previous_history, /* ^[OA - up */
"\x1bOB": next_history, /* ^[OB - down */
"\x1bOC": forward_char, /* ^[OC - right */
"\x1bOD": backward_char, /* ^[OD - left */
"\x1bOF": forward_word, /* ^[OF - ctrl-right */
"\x1bOH": backward_word, /* ^[OH - ctrl-left */
"\x1b[1;5C": forward_word, /* ^[[1;5C - ctrl-right */
"\x1b[1;5D": backward_word, /* ^[[1;5D - ctrl-left */
"\x1b[1~": beginning_of_line, /* ^[[1~ - bol */
"\x1b[3~": delete_char, /* ^[[3~ - delete */
"\x1b[4~": end_of_line, /* ^[[4~ - eol */
"\x1b[5~": history_search_backward,/* ^[[5~ - page up */
"\x1b[6~": history_search_forward, /* ^[[5~ - page down */
"\x1b[A": previous_history, /* ^[[A - up */
"\x1b[B": next_history, /* ^[[B - down */
"\x1b[C": forward_char, /* ^[[C - right */
"\x1b[D": backward_char, /* ^[[D - left */
"\x1b[F": end_of_line, /* ^[[F - end */
"\x1b[H": beginning_of_line, /* ^[[H - home */
"\x1b\x7f": backward_kill_word, /* M-C-? - backward_kill_word */
"\x1bb": backward_word, /* M-b - backward_word */
"\x1bd": kill_word, /* M-d - kill_word */
"\x1bf": forward_word, /* M-f - backward_word */
"\x1bk": backward_kill_line, /* M-k - backward_kill_line */
"\x1bl": downcase_word, /* M-l - downcase_word */
"\x1bt": transpose_words, /* M-t - transpose_words */
"\x1bu": upcase_word, /* M-u - upcase_word */
"\x7f": backward_delete_char, /* ^? - delete */
};
function dupstr(str, count) {
var res = "";
while (count-- > 0)
res += str;
return res;
}
var readline_keys;
var readline_state;
var readline_cb;
function readline_print_prompt()
{
std.puts(prompt);
term_cursor_x = prompt.length % term_width;
last_cmd = "";
last_cursor_pos = 0;
}
function readline_start(defstr, cb) {
cmd = defstr || "";
cursor_pos = cmd.length;
history_index = history.length;
readline_cb = cb;
prompt = pstate;
if (mexpr) {
prompt += dupstr(" ", plen - prompt.length);
prompt += ps2;
} else {
if (show_time) {
var t = Math.round(eval_time) + " ";
eval_time = 0;
t = dupstr("0", 5 - t.length) + t;
prompt += t.substring(0, t.length - 4) + "." + t.substring(t.length - 4);
}
plen = prompt.length;
prompt += ps1;
}
readline_print_prompt();
update();
readline_state = 0;
}
function handle_char(c1) {
var c;
c = String.fromCharCode(c1);
switch(readline_state) {
case 0:
if (c == '\x1b') { /* '^[' - ESC */
readline_keys = c;
readline_state = 1;
} else {
handle_key(c);
}
break;
case 1: /* '^[ */
readline_keys += c;
if (c == '[') {
readline_state = 2;
} else if (c == 'O') {
readline_state = 3;
} else {
handle_key(readline_keys);
readline_state = 0;
}
break;
case 2: /* '^[[' - CSI */
readline_keys += c;
if (!(c == ';' || (c >= '0' && c <= '9'))) {
handle_key(readline_keys);
readline_state = 0;
}
break;
case 3: /* '^[O' - ESC2 */
readline_keys += c;
handle_key(readline_keys);
readline_state = 0;
break;
}
}
function handle_key(keys) {
var fun;
if (quote_flag) {
if (keys.length === 1)
insert(keys);
quote_flag = false;
} else if (fun = commands[keys]) {
this_fun = fun;
switch (fun(keys)) {
case -1:
readline_cb(cmd);
return;
case -2:
readline_cb(null);
return;
case -3:
/* uninstall a Ctrl-C signal handler */
os.signal(os.SIGINT, null);
/* uninstall the stdin read handler */
os.setReadHandler(term_fd, null);
return;
}
last_fun = this_fun;
} else if (keys.length === 1 && keys >= ' ') {
insert(keys);
last_fun = insert;
} else {
alert(); /* beep! */
}
cursor_pos = (cursor_pos < 0) ? 0 :
(cursor_pos > cmd.length) ? cmd.length : cursor_pos;
update();
}
var hex_mode = false;
var eval_mode = "std";
function bignum_typeof(a) {
"use bigint";
return typeof a;
}
function eval_mode_typeof(a) {
if (eval_mode === "std")
return typeof a;
else
return bignum_typeof(a);
}
function number_to_string(a, radix) {
var s;
if (!isFinite(a)) {
/* NaN, Infinite */
if (typeof a === "bigfloat" && eval_mode !== "math") {
return "BigFloat(" + a.toString() + ")";
} else {
return a.toString();
}
} else {
if (a == 0) {
if (1 / a < 0)
s = "-0";
else
s = "0";
} else {
if (radix == 16) {
var s;
if (a < 0) {
a = -a;
s = "-";
} else {
s = "";
}
s += "0x" + a.toString(16);
} else {
s = a.toString();
}
}
if (typeof a === "bigfloat" && eval_mode !== "math") {
s += "l";
} else if (eval_mode !== "std" && s.indexOf(".") < 0 &&
((radix == 16 && s.indexOf("p") < 0) ||
(radix == 10 && s.indexOf("e") < 0))) {
/* add a decimal point so that the floating point type
is visible */
s += ".0";
}
return s;
}
}
function bigint_to_string(a, radix) {
var s;
if (radix == 16) {
var s;
if (a < 0) {
a = -a;
s = "-";
} else {
s = "";
}
s += "0x" + a.toString(16);
} else {
s = a.toString();
}
if (eval_mode === "std")
s += "n";
return s;
}
function print(a) {
var stack = [];
function print_rec(a) {
var n, i, keys, key, type, s;
type = eval_mode_typeof(a);
if (type === "object") {
if (a === null) {
std.puts(a);
} else if (stack.indexOf(a) >= 0) {
std.puts("[circular]");
} else if (has_jscalc && (a instanceof Fraction ||
a instanceof Complex ||
a instanceof Mod ||
a instanceof Polynomial ||
a instanceof PolyMod ||
a instanceof RationalFunction ||
a instanceof Series)) {
std.puts(a.toString());
} else {
stack.push(a);
if (Array.isArray(a)) {
n = a.length;
std.puts("[ ");
for(i = 0; i < n; i++) {
if (i !== 0)
std.puts(", ");
if (i in a) {
print_rec(a[i]);
} else {
std.puts("<empty>");
}
if (i > 20) {
std.puts("...");
break;
}
}
std.puts(" ]");
} else if (Object.__getClass(a) === "RegExp") {
std.puts(a.toString());
} else {
keys = Object.keys(a);
n = keys.length;
std.puts("{ ");
for(i = 0; i < n; i++) {
if (i !== 0)
std.puts(", ");
key = keys[i];
std.puts(key, ": ");
print_rec(a[key]);
}
std.puts(" }");
}
stack.pop(a);
}
} else if (type === "string") {
s = a.__quote();
if (s.length > 79)
s = s.substring(0, 75) + "...\"";
std.puts(s);
} else if (type === "number" || type === "bigfloat") {
std.puts(number_to_string(a, hex_mode ? 16 : 10));
} else if (type === "bigint") {
std.puts(bigint_to_string(a, hex_mode ? 16 : 10));
} else if (type === "symbol") {
std.puts(String(a));
} else if (type === "function") {