-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathread.js
1221 lines (1177 loc) · 39.8 KB
/
read.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
/* Term reading */
// See http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
// Parsers return either:
// 1) An string, in case of an atom
// 2) An integer, in case of an integer
// 3) An object with .list and .tail if a list (because apparently it is not easy to determine if the type of something is a list at runtime!?)
// If it is a proper list, .tail == NIL
// 4) An object with .variable_name, if a variable
// 5) An object with .functor (a string) and .args (an array) defined if a term
function parse_infix(s, lhs, precedence)
{
var token = {};
if (!read_token(s, token))
return false;
token = token.value;
var rhs = {};
if (!read_expression(s, precedence, false, false, rhs))
return false;
return {functor: token,
args: [lhs, rhs.value]};
}
function parse_postfix(s, lhs)
{
var token = {};
if (!read_token(s, token))
return false;
return {functor: token.value,
args: [lhs]};
}
// A reminder: yfx means an infix operator f, with precedence p, where the lhs has a precendece <= p and the rhs has a precedence < p.
var prefix_operators = {":-": {precedence: 1200, fixity: "fx"},
"?-": {precedence: 1200, fixity: "fx"},
"dynamic": {precedence: 1150, fixity: "fx"},
"discontiguous": {precedence: 1150, fixity: "fx"},
"initialization": {precedence: 1150, fixity: "fx"},
"meta_predicate": {precedence: 1150, fixity: "fx"},
"module_transparent": {precedence: 1150, fixity: "fx"},
"multifile": {precedence: 1150, fixity: "fx"},
"thread_local": {precedence: 1150, fixity: "fx"},
"volatile": {precedence: 1150, fixity: "fx"},
"\+": {precedence: 900, fixity: "fy"},
"~": {precedence: 900, fixity: "fx"},
"?": {precedence: 500, fixity: "fx"},
"+": {precedence: 200, fixity: "fy"},
"-": {precedence: 200, fixity: "fy"},
"\\": {precedence: 200, fixity: "fy"}};
var infix_operators = {":-": {precedence: 1200, fixity: "xfx"},
"-->": {precedence: 1200, fixity: "xfx"},
";": {precedence: 1100, fixity: "xfy"},
"|": {precedence: 1100, fixity: "xfy"},
"->": {precedence: 1050, fixity: "xfy"},
"*->": {precedence: 1050, fixity: "xfy"},
",": {precedence: 1000, fixity: "xfy"},
":=": {precedence: 990, fixity: "xfx"},
"<": {precedence: 700, fixity: "xfx"},
"=": {precedence: 700, fixity: "xfx"},
"=..": {precedence: 700, fixity: "xfx"},
"=@=": {precedence: 700, fixity: "xfx"},
"=:=": {precedence: 700, fixity: "xfx"},
"=<": {precedence: 700, fixity: "xfx"},
"==": {precedence: 700, fixity: "xfx"},
"=\=": {precedence: 700, fixity: "xfx"},
">": {precedence: 700, fixity: "xfx"},
">=": {precedence: 700, fixity: "xfx"},
"@<": {precedence: 700, fixity: "xfx"},
"@=<": {precedence: 700, fixity: "xfx"},
"@>": {precedence: 700, fixity: "xfx"},
"@>=": {precedence: 700, fixity: "xfx"},
"\=": {precedence: 700, fixity: "xfx"},
"\==": {precedence: 700, fixity: "xfx"},
"is": {precedence: 700, fixity: "xfx"},
">:<": {precedence: 700, fixity: "xfx"},
":<": {precedence: 700, fixity: "xfx"},
":": {precedence: 600, fixity: "xfy"},
"+": {precedence: 500, fixity: "yfx"},
"-": {precedence: 500, fixity: "yfx"},
"/\\": {precedence: 500, fixity: "yfx"},
"\\/": {precedence: 500, fixity: "yfx"},
"xor": {precedence: 500, fixity: "yfx"},
"*": {precedence: 400, fixity: "yfx"},
"/": {precedence: 400, fixity: "yfx"},
"//": {precedence: 400, fixity: "yfx"},
"rdiv": {precedence: 400, fixity: "yfx"},
"<<": {precedence: 400, fixity: "yfx"},
">>": {precedence: 400, fixity: "yfx"},
"mod": {precedence: 400, fixity: "yfx"},
"rem": {precedence: 400, fixity: "yfx"},
"**": {precedence: 200, fixity: "xfx"},
"^": {precedence: 200, fixity: "xfy"}};
// This returns a javascript object representation of the term. It takes the two extra args because of some oddities with Prolog:
// 1) If we are reading foo(a, b) and we are at the a, we would accept the , as part of the LHS. ie, we think (a,b) is the sole argument. Instead, we should make , have
// very high precedence if we are reading an arg. Of course, () can reduce this again, so that foo((a,b)) does indeed read ,(a,b) as the single argument
// 2) | behaves slightly differently in lists, in a similar sort of way
function read_expression(s, precedence, isarg, islist, expression)
{
var token = {};
if (!read_token(s, token))
return false;
token = token.value;
if (token == null)
{
expression.value = {end_of_file:true};
return true;
}
var lhs;
// Either the token is an operator, or it must be an atom (or the start of a list or curly-list)
var op = prefix_operators[token];
if (op === undefined)
{
if (token == "\"")
{
// We have to just read chars until we get a close " (taking care with \" in the middle)
var args = [];
var t = 0;
var mode = 0;
if (prolog_flag_values['double_quotes'] == "chars")
mode = 0;
else if (prolog_flag_values['double_quotes'] == "codes")
mode = 1;
else if (prolog_flag_values['double_quotes'] == "atom")
mode = 2;
while (true)
{
t = get_raw_char_with_conversion(s.stream);
if (t == '"')
break;
if (t == "\\")
{
if (peek_raw_char_with_conversion(s.stream) == '"')
{
get_raw_char_with_conversion(s.stream);
if (mode == 1)
args.push('"'.charCodeAt(0));
else
args.push('"');
continue;
}
}
if (mode == 1)
args.push(t.charCodeAt(0));
else
args.push(t);
}
if (mode == 2)
lhs = args.join('');
else
lhs = {list: args, tail: "[]"};
}
else if (token == "[" || token == "{")
{
// The principle for both of these is very similar
var args = [];
var next = {};
while(true)
{
var t = {};
if (!read_expression(s, Infinity, true, true, t))
return false;
t = t.value;
if (t == "]")
{
lhs = "[]";
break;
// Special case for the empty list, since the first argument is ']'
}
args.push(t);
next = {};
if (!read_token(s, next))
return false;
next = next.value;
if (next == ',')
continue;
else if (next == "]" && token == "[")
{
lhs = {list: args, tail: "[]"};
break;
}
else if (next == "}" && token == "{")
{
lhs = {functor: "{}", args:args};
break;
}
else if (next == "|" && token == "[")
{
var tail = {};
if (!read_expression(s, Infinity, true, true, tail))
return false;
lhs = {list: args, tail: tail.value},
next = {};
if (!read_token(s, next))
return false;
next = next.value;
if (next == "]")
break;
else
return syntax_error("missing ]");
}
else
{
return syntax_error("mismatched " + token + " at " + next);
}
}
}
else if (token == "(")
{
// Is this right? () just increases the precedence to infinity and reads another term?
var lhs = {};
if (!read_expression(s, Infinity, false, false, lhs))
return false;
lhs = lhs.value;
next = {};
if (!read_token(s, next))
return false;
next = next.value;
if (next != ")")
return syntax_error("mismatched ( at " + next);
}
else if (token == "]")
{
expression.value = token;
return true;
}
else
{
// It is an atom
lhs = token;
}
}
else if (op.fixity == "fx")
{
var arg = {};
if (!read_expression(s, op.precedence, isarg, islist, arg))
return false;
lhs = {functor: token, args:[arg.value]};
}
else if (op.fixity == "fy")
{
var arg = {};
if (!read_expression(s, op.precedence+0.5, isarg, islist, arg))
return false;
lhs = {functor: token, args:[arg.value]};
}
else
return false; // Parse error
while (true)
{
var infix_operator = {};
if (!peek_token(s, infix_operator))
return false;
infix_operator = infix_operator.value;
if (typeof(infix_operator) == "number" && infix_operator <= 0)
{
// Yuck. This is when we read something like X is A-1. Really the - is -/2 in this case
read_token(s, {});
unread_token(s, Math.abs(infix_operator));
unread_token(s, "-");
infix_operator = "-";
}
if (infix_operator == '(')
{
// We are reading a term. Keep reading expressions: After each one we should
// either get , or )
// First though, consume the (
read_token(s, {});
var args = [];
var next = {};
while (true)
{
var arg = {};
if (!read_expression(s, Infinity, true, false, arg))
return false;
args.push(arg.value);
next = {};
if (!read_token(s, next))
return false;
next = next.value;
if (next == ')')
break;
else if (next == ',')
continue;
else
{
if (next == null)
return syntax_error("end_of_file");
else
return syntax_error(next);
}
}
// ./2 is a list
if (lhs == "." && args.length == 2)
{
lhs = {list: args[0],
tail: args[1]};
}
else
{
lhs = {functor: lhs,
args:args};
}
// Now, where were we?
infix_operator = {};
if (!peek_token(s, infix_operator))
return false;
infix_operator = infix_operator.value;
}
// Pretend that . is an operator with infinite precedence
if (infix_operator == ".")
{
expression.value = lhs;
return true;
}
if (infix_operator == "," && isarg)
{
expression.value = lhs;
return true;
}
if (infix_operator == "|" && islist)
{
expression.value = lhs;
return true;
}
if (infix_operator == null)
{
expression.value = lhs;
return true;
}
op = infix_operators[infix_operator];
if (op !== undefined)
{
if (op.fixity == "xfx" && precedence > op.precedence)
{
lhs = parse_infix(s, lhs, op.precedence);
if (lhs == false)
return false;
}
else if (op.fixity == "xfy" && precedence > op.precedence)
{
// Is this 0.5 thing right? Will it eventually drive up precedence to the wrong place? We never want to reach the next integer...
lhs = parse_infix(s, lhs, op.precedence+0.5);
if (lhs == false)
return false;
}
else if (op.fixity == "yfx" && precedence >= op.precedence)
{
lhs = parse_infix(s, lhs, op.precedence);
if (lhs == false)
return false;
}
else if (op.fixity == "xf" && precedence > op.precedence)
{
lhs = parse_postfix(s, lhs, op.precedence);
if (lhs == false)
return false;
}
else if (op.fixity == "yf" && precedence >= op.precedence)
{
lhs = parse_postfix(s, lhs, op.precedence);
if (lhs == false)
return false;
}
else
{
expression.value = lhs;
return true;
}
}
else
{
expression.value = lhs;
return true;
}
}
}
function parse_term_options(options)
{
var result = {};
var yes = lookup_atom("true");
while (options != NIL)
{
if (TAG(options) != TAG_LST)
return type_error("list", options);
var head = memory[VAL(options)];
if (TAG(head) != TAG_STR)
return type_error("option", head);
var ftor = memory[VAL(head)];
if (ftor == lookup_functor("quoted",1))
{
result.quoted = (memory[VAL(head)+1] == yes)
}
else if (ftor == lookup_functor("ignore_ops",1))
{
result.ignore_ops = (memory[VAL(head)+1] == yes)
}
else if (ftor == lookup_functor("numbervars",1))
{
result.numbervars = (memory[VAL(head)+1] == yes)
}
else if (ftor == lookup_functor("variables",1))
{
result.variables = memory[VAL(head)+1];
}
else if (ftor == lookup_functor("variable_names",1))
{
result.variable_names = memory[VAL(head)+1];
}
else if (ftor == lookup_functor("singletons",1))
{
result.singletons = memory[VAL(head)+1];
}
else
{
return type_error(option, head);
}
options = memory[VAL(options)+1];
}
return result;
}
function read_term(stream, term, options)
{
if (!(options = parse_term_options(options)))
return false;
var streamindex = VAL(get_arg(stream, 1));
var s = streams[streamindex];
var context = {stream:s, peeked_token: undefined};
var expression = {};
if (!read_expression(context, Infinity, false, false, expression))
return false;
expression = expression.value;
// Depending on the situation, we may expect a . now on the stream.
// There will not be one if we are going to return end_of_file because it is actually the eof
// (Of course, if the file contains end_of_file. then we will return end_of_file AND read the .
// Luckily we can distinguish the two cases
// There will also not be one if we are in atom_to_term mode, which is not yet implemented
if (expression.end_of_file === undefined)
{
var period = {};
if (!read_token(context, period))
return false;
if (period.value != ".") // Missing period === eof
return syntax_error("end_of_file");
}
else
expression = "end_of_file";
debug_msg("Read expression: " + expression_to_string(expression));
var varmap = {};
var singletons = {};
t1 = expression_to_term(expression, varmap, singletons);
var rc = 1;
if (options.variables !== undefined || options.singletons !== undefined)
{
var equals2 = lookup_functor("=", 2);
var keys = Object.keys(varmap);
for (var i = 0; i < keys.length; i++)
{
var varname = keys[i];
if (options.variables !== undefined)
{
if (!unify(state.H ^ (TAG_LST << WORD_BITS), options.variables))
return false;
memory[state.H] = varmap[varname];
memory[state.H+1] = (state.H+1) ^ (TAG_REF << WORD_BITS);
options.variables = memory[state.H+1];
state.H+=2;
}
if (options.variable_names !== undefined)
{
if (!unify(state.H ^ (TAG_LST << WORD_BITS), options.variable_names))
{
debug("not unifiable: " + term_to_string(options.variable_names));
return false;
}
memory[state.H] = (state.H+2) ^ (TAG_STR << WORD_BITS);
memory[state.H+1] = (state.H+1) ^ (TAG_REF << WORD_BITS);
options.variable_names = memory[state.H+1];
memory[state.H+2] = equals2;
memory[state.H+3] = lookup_atom(varname);
memory[state.H+4] = varmap[varname];
state.H+=5;
}
}
if (options.variables !== undefined)
if (!unify(options.variables, NIL))
return false;
if (options.variable_names !== undefined)
if (!unify(options.variable_names, NIL))
return false;
}
if (options.singletons !== undefined)
{
var keys = Object.keys(singletons);
for (var i = 0; i < keys.length; i++)
{
var varname = keys[i];
if (singletons[varname] == 1)
{
if (!unify(state.H ^ (TAG_LST << WORD_BITS), options.singletons))
return false;
memory[state.H] = (state.H+2) ^ (TAG_STR << WORD_BITS);
memory[state.H+1] = (state.H+1) ^ (TAG_REF << WORD_BITS);
options.singletons = memory[state.H+1];
memory[state.H+2] = equals2;
memory[state.H+3] = lookup_atom(varname);
memory[state.H+4] = varmap[varname];
state.H+=5;
}
}
if (!unify(options.singletons, NIL))
return false;
}
debug_msg("A term has been created ( " + VAL(t1) + " ). Reading it back from the heap gives: " + term_to_string(t1));
return unify(term, t1);
}
function predicate_write_term(stream, term, options)
{
if (!(options = parse_term_options(options)))
return false;
var value = format_term(term, options);
var s = {};
if (!get_stream(stream, s))
return false;
s = s.value;
if (s.write == null)
return permission_error("output", "stream", stream);
var bytes = toByteArray(value);
return (s.write(s, 1, bytes.length, bytes) >= 0)
}
function escape_atom(a)
{
chars = a.split('');
var result = "";
for (var i = 0; i < chars.length; i++)
{
if (chars[i] == "'")
result += "\\'";
else
result += chars[i];
}
return result;
}
function quote_atom(a)
{
if (a.charAt(0) >= "A" && a.charAt(0) <= "Z")
return "'" + escape_atom(a) + "'";
chars = a.split('');
if (is_punctuation(chars[0]))
{
for (var i = 0; i < chars.length; i++)
{
if (!is_punctuation(chars[i]))
return "'" + escape_atom(a) + "'";
}
}
else
{
for (var i = 0; i < chars.length; i++)
{
if (is_punctuation(chars[i]) || chars[i] == ' ')
return "'" + escape_atom(a) + "'";
}
}
return a;
}
function is_operator(ftor)
{
ftor = VAL(ftor);
if (ftable[ftor][1] == 2 && infix_operators[atable[ftable[ftor][0]]] != undefined)
return true;
if (ftable[ftor][1] == 1 && prefix_operators[atable[ftable[ftor][0]]] != undefined)
return true;
return false;
}
function format_term(value, options)
{
if (value == undefined)
abort("Illegal memory access in format_term: " + hex(value) + ". Dumping...");
value = deref(value);
switch(TAG(value))
{
case TAG_REF:
if (VAL(value) > HEAP_SIZE)
{
if (state.E > state.B)
lTop = state.E + state.CP.code[state.CP.offset - 1] + 2;
else
lTop = state.B + memory[state.B] + 8;
return "_L" + (lTop - VAL(value));
}
else
return "_G" + VAL(value);
case TAG_ATM:
atom = atable[VAL(value)];
if (atom == undefined)
abort("No such atom: " + VAL(value));
if (options.quoted === true)
return quote_atom(atom);
return atom;
case TAG_INT:
if ((VAL(value) & (1 << (WORD_BITS-1))) == (1 << (WORD_BITS-1)))
return (VAL(value) - (1 << WORD_BITS)) + "";
else
return VAL(value) + "";
// fall-through
case TAG_FLT:
return floats[VAL(value)] + "";
case TAG_STR:
var ftor = VAL(memory[VAL(value)]);
if (options.numbervars === true && ftor == lookup_functor('$VAR', 1) && TAG(memory[VAL(value)+1]) == TAG_INT)
{
var index = VAL(memory[VAL(value)+1]);
var result = String.fromCharCode(65 + (index % 26));
if (index >= 26)
result = result + Math.floor(index / 26);
return result;
}
if (!is_operator(ftor) || options.ignore_ops === true)
{
// Print in canonical form functor(arg1, arg2, ...)
var result = format_term(ftable[ftor][0] ^ (TAG_ATM << WORD_BITS), options) + "(";
for (var i = 0; i < ftable[ftor][1]; i++)
{
result += format_term(memory[VAL(value)+1+i], options);
if (i+1 < ftable[ftor][1])
result += ",";
}
return result + ")";
}
else
{
// Print as an operator
var fname = atable[ftable[ftor][0]];
if (ftable[ftor][1] == 2 && infix_operators[fname] != undefined)
{
// Infix operator
var lhs = format_term(memory[VAL(value)+1], options);
if (is_punctuation(lhs.charAt(lhs.length-1)) && !is_punctuation(fname.charAt(0)))
result = lhs + fname;
else if (!is_punctuation(lhs.charAt(lhs.length-1)) && is_punctuation(fname.charAt(0)))
result = lhs + fname;
else
{
result = lhs + " " + fname;
}
var rhs = format_term(memory[VAL(value)+2], options);
if (is_punctuation(rhs.charAt(0)) && !is_punctuation(fname.charAt(fname.length-1)))
return result + rhs;
else if (!is_punctuation(rhs.charAt(0)) && is_punctuation(fname.charAt(fname.length-1)))
return result + rhs;
else
return result + " " + rhs;
}
else if (ftable[ftor][1] == 1 && prefix_operators[fname] != undefined)
{
// Prefix operator
var rhs = format_term(memory[VAL(value)+1], options);
if (is_punctuation(rhs.charAt(0)) && !is_punctuation(fname.charAt(fname.length-1)))
return fname + rhs;
else if (!is_punctuation(rhs.charAt(0)) && is_punctuation(fname.charAt(fname.length-1)))
return fname + rhs;
else
return fname + " " + rhs;
}
}
case TAG_LST:
if (options.ignore_ops)
return "'.'(" + format_term(memory[VAL(value)], options) + "," + format_term(memory[VAL(value)+1], options) + ")";
// Otherwise we need to print the list in list-form
var result = "[";
var head = memory[VAL(value)];
var tail = memory[VAL(value)+1];
while (true)
{
result += format_term(head, options);
if (tail == NIL)
return result + "]";
else if (TAG(tail) == TAG_LST)
{
head = memory[VAL(tail)];
tail = memory[VAL(tail)+1];
result += ",";
}
else
return result + "|" + format_term(tail, options) + "]";
}
}
}
function expression_to_term(s, varmap, singletons)
{
if (typeof(s) == "string")
return lookup_atom(s);
else if (typeof(s) == "number")
{
if (s == ~~s)
{
return (s & ((1 << WORD_BITS)-1)) ^ (TAG_INT << WORD_BITS);
}
else
{
return lookup_float(s);
}
}
else if (s.variable_name !== undefined)
{
if (varmap[s.variable_name] !== undefined)
{
result = state.H;
memory[state.H] = varmap[s.variable_name];
state.H++;
}
else
{
result = alloc_var();
varmap[s.variable_name] = result;
}
if (singletons[s.variable_name] === undefined)
singletons[s.variable_name] = 1;
else
singletons[s.variable_name]++;
return result;
}
else if (s.list !== undefined)
{
// Special case for [], as usual, since we do not actually allocate any lists!
if (s.list.length == 0)
return NIL;
var result = alloc_var();
var tail = result;
var head;
for (var i = 0; i < s.list.length; i++)
{
unify(tail, state.H ^ (TAG_LST << WORD_BITS));
head = alloc_var();
tail = alloc_var();
unify(head, expression_to_term(s.list[i], varmap, singletons));
}
unify(tail, expression_to_term(s.tail, varmap, singletons));
return result;
}
else if (s.functor !== undefined)
{
var t = (state.H ^ TAG_STR << WORD_BITS);
memory[state.H++] = lookup_functor(s.functor, s.args.length);
// Reserve space for the args
var var_args = [];
for (var i = 0; i < s.args.length; i++)
var_args[i] = alloc_var();
for (var i = 0; i < s.args.length; i++)
{
z = expression_to_term(s.args[i], varmap, singletons);
unify(z, var_args[i]);
}
return t;
}
else
abort("Invalid expression: " + JSON.stringify(s));
}
function peek_token(s, t)
{
if (s.peek_tokens === undefined || s.peeked_tokens.length == 0 )
{
var tt = {};
if (!read_token(s, tt))
return false;
s.peeked_tokens = [tt.value];
}
t.value = s.peeked_tokens[0];
return true;
}
function unread_token(s, t)
{
if (s.peeked_tokens === undefined)
s.peeked_tokens = [t];
else
s.peeked_tokens.push(t);
}
function read_token(s, t)
{
if (s.peeked_tokens !== undefined && s.peeked_tokens.length != 0)
{
t.value = s.peeked_tokens.pop();
return true;
}
if (!lex(s.stream, t))
return false;
return true;
}
function is_char(c)
{
return ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '_');
}
var punctuation_array = ['`', '~', '@', '#', '$', '^', '&', '*', '-', '+', '=', '<', '>', '/', '?', ':', ',', '\\', '.'];
function is_punctuation(c)
{
return punctuation_array.indexOf(c) != -1;
}
// lex(stream, t) returns a single token in t.value and fails if an exception is raised
function lex(s, t)
{
var token;
while(true)
{
var c = get_raw_char_with_conversion(s);
if (c == -1)
{
t.value = null;
return true;
}
// Consume any whitespace
if (c == ' ' || c == '\n' || c == '\t')
continue;
else if (c == '%')
{
do
{
d = get_raw_char_with_conversion(s);
if (d == -1)
{
t.value = null;
return true;
}
} while(d != '\n');
continue;
}
else if (c == '/')
{
d = peek_raw_char_with_conversion(s);
if (d == '*')
{
// Block comment
get_raw_char_with_conversion(s);
while(true)
{
d = get_raw_char_with_conversion(s);
if (d == -1)
return syntax_error("end of file in block comment");
if (d == '*' && get_raw_char_with_conversion(s) == '/')
break;
}
continue;
}
else
{
// My mistake, the term actually begins with /. c is still set to the right thing
break;
}
}
break;
}
if ((c >= 'A' && c <= 'Z') || c == '_')
{
token = {variable_name: "" + c};
// Variable. May contain a-zA-Z0-9_
while (true)
{
c = peek_raw_char_with_conversion(s);
if (is_char(c))
{
token.variable_name += get_raw_char_with_conversion(s);
}
else
{
t.value = token;
return true;
}
}
}
else if ((c >= '0' && c <= '9') || (c == '-' && peek_raw_char_with_conversion(s) >= '0' && peek_raw_char_with_conversion(s) <= '9'))
{
// Integer. May contain 0-9 only. Floats complicate this a bit
var negate = false;
if (c == '-')
{
token = '';
negate = true;
}
else
token = c - '0';
var decimal_places = 0;
var seen_decimal = false;
while (true)
{
c = peek_raw_char_with_conversion(s);
if (seen_decimal)
decimal_places++;
if ((c >= '0' && c <= '9'))
{
token = token * 10 + (get_raw_char_with_conversion(s) - '0');
}
else if (c == '.' && !seen_decimal)
{
// Fixme: Also must check that the next char is actually a number. Otherwise 'X = 3.' will confuse things somewhat.
seen_decimal = true;
get_raw_char_with_conversion(s);
continue;
}
else if (is_char(c))
return syntax_error("illegal number" + token + ": " + c);
else
{
if (seen_decimal)
{
for (var i = 1; i < decimal_places; i++)
token = token / 10;
}
t.value = negate?(-token):token;
return true;
}
}
}
else
{
// Either:
// 1) a term
// 2) an atom (which is a term with no arguments)
// 3) An operator
// In all cases, first we have to read an atom
var buffer = "";
var state = 0;
if (c == '\'')
{
// Easy. The atom is quoted!
while(true)
{
c = get_raw_char_with_conversion(s);
if (c == '\\')
state = (state + 1) % 2;
if (c == -1)
return syntax_error("end of file in atom");
if (c == '\'' && state == 0)
break;
buffer += c;
}
}
else // Not so simple. Have to read an atom using rules, which are actually available only for a fee from ISO...
{
buffer += c;
// An unquoted atom may contain either all punctuation or all A-Za-z0-9_. There are probably more complicated rules, but this will do
char_atom = is_char(c);
punctuation_atom = is_punctuation(c);
while (true)
{
c = peek_raw_char_with_conversion(s);
if (c == -1)
break;
if (char_atom && is_char(c))
buffer += get_raw_char_with_conversion(s);
else if (punctuation_atom && is_punctuation(c))
buffer += get_raw_char_with_conversion(s);
else
break;
}
}
t.value=buffer;
return true;
}
}
// This is one of the more ridiculous things in the ISO standard
var char_conversion_override = {};
function predicate_char_conversion(a, b)
{
if (TAG(a) != TAG_ATM)
return type_error("atom", a);
if (TAG(b) != TAG_ATM)
return type_error("atom", b);
char_conversion_override[atable[VAL(a)]] = atable[VAL(b)];
return true;
}