forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsrepl_rps.cc
1314 lines (1265 loc) · 54.9 KB
/
parsrepl_rps.cc
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
/****************************************************************
* file parsrepl_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the parser support for the Read Eval Print Loop
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2022 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "refpersys.hh"
/// hopefully these TokenSource::parse routines could be used a lot...
//// routine to parse a binary operation like Aleft BINOP Aright. Is
//// given BINOP as an operation (semantics) and a delimiter
//// (syntax). Is given the parsing C++ closure for both Aleft and
//// Aright.
Rps_Value
Rps_TokenSource::parse_symmetrical_binaryop(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq,
Rps_ObjectRef binoper, Rps_ObjectRef bindelim,
std::function<Rps_Value(Rps_CallFrame*,Rps_TokenSource*,std::deque<Rps_Value>&,bool*)> parser_binop,
bool*pokparse, const char*opername)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(/*descr:*/nullptr,callframe,
Rps_Value resexprsymv;
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef binoperob;
Rps_ObjectRef bindelimob;
);
if (!opername)
opername="???";
bool leftok = false;
bool rightok = false;
std::string startpos = position_str();
/// ensure the GC knows them
_f.binoperob = binoper;
_f.bindelimob = bindelim;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername <<" START position:" << startpos << " calldepth="
<< rps_call_frame_depth(&_));
if (is_looking_ahead(pokparse))
{
// TODO: review, ... only lookahead for left part....
_f.leftv = parser_binop(&_,this,token_deq, RPS_DO_LOOKAHEAD);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername << " lookahead " << (_f.leftv?"success":"failure"));
return _f.leftv;
}
else
_f.leftv = parser_binop(&_,this,token_deq,&leftok);
if (!leftok)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername << " LEFT FAILURE startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_));
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.lextokv = lookahead_token(&_, token_deq, 0);
if (!_f.lextokv)
{
if (pokparse)
*pokparse = true;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse " << opername << " LEFTONLY startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_)
<< " GIVES " << _f.leftv);
return _f.leftv;
}
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object() == bindelim)
{
(void) get_token(&_); // consume the operator
_f.rightv = parser_binop(&_,this,token_deq,&rightok);
if (!rightok)
{
RPS_WARNOUT("failed to parse right operand for "<< opername << " at " << position_str()
<< " starting " << startpos);
if (pokparse)
*pokparse = false;
return nullptr;
}
}
else
{
if (pokparse)
*pokparse = true;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse " << opername << " LEFTONLY startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_)
<< " GIVES " << _f.leftv);
return _f.leftv;
}
_f.resexprsymv = Rps_InstanceValue(_f.binoperob, {_f.leftv, _f.rightv});
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername <<" END position:" << position_str()
<< " startpos:" << startpos
<< " calldepth="
<< rps_call_frame_depth(&_));
return _f.resexprsymv;
} // end Rps_TokenSource::parse_symmetrical_binaryop
//// Routine to parse a binary operation like A BINOP B. Is given
//// BINOP as an operation (semantics) and a delimiter (syntax). Is
//// given the parsing C++ closure for parsing A and the other parsing
//// C++ closure for parsing B.
////
Rps_Value
Rps_TokenSource::parse_asymmetrical_binaryop(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq,
Rps_ObjectRef binoper, Rps_ObjectRef bindelim,
std::function<Rps_Value(Rps_CallFrame*,Rps_TokenSource*,std::deque<Rps_Value>&,bool*)>
parser_leftop,
std::function<Rps_Value(Rps_CallFrame*,Rps_TokenSource*,std::deque<Rps_Value>&,bool*)>
parser_rightop,
bool*pokparse, const char*opername)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(/*descr:*/nullptr,callframe,
Rps_Value resexprsymv;
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef binoperob;
Rps_ObjectRef bindelimob;
);
if (!opername)
opername="???";
bool leftok = false;
bool rightok = false;
std::string startpos = position_str();
/// ensure the GC knows them
_f.binoperob = binoper;
_f.bindelimob = bindelim;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse asymmetrical binop " << opername <<" START position:" << startpos << " calldepth="
<< rps_call_frame_depth(&_));
if (is_looking_ahead(pokparse))
{
/// for lookahead just run the left parsing...
_f.leftv = parser_leftop(&_,this,token_deq,RPS_DO_LOOKAHEAD);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername <<" LOOKAHEAD "
<< (_f.leftv?"success":"failure")
<< " startpos:" << startpos
<< " calldepth="
<< rps_call_frame_depth(&_));
}
else
_f.leftv = parser_leftop(&_,this,token_deq,&leftok);
if (!leftok)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse asymmetrical binop " << opername << " LEFT FAILURE startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_));
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.lextokv = lookahead_token(&_, token_deq, 0);
if (!_f.lextokv)
{
if (pokparse)
*pokparse = true;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse asymmetrical " << opername << " LEFTONLY startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_)
<< " GIVES " << _f.leftv);
return _f.leftv;
}
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object() == bindelim)
{
(void) get_token(&_); // consume the operator
_f.rightv = parser_rightop(&_,this,token_deq,&rightok);
if (!rightok)
{
RPS_WARNOUT("failed to parse asymmetrical right operand for "<< opername << " at " << position_str()
<< " starting " << startpos);
if (pokparse)
*pokparse = false;
return nullptr;
}
}
else
{
if (pokparse)
*pokparse = true;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse asymmetrical " << opername << " LEFTONLY startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_)
<< " GIVES " << _f.leftv);
return _f.leftv;
}
_f.resexprsymv = Rps_InstanceValue(_f.binoperob, {_f.leftv, _f.rightv});
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse asymmetrical binop " << opername <<" END position:" << position_str()
<< " startpos:" << startpos
<< " calldepth="
<< rps_call_frame_depth(&_));
return _f.resexprsymv;
} // end Rps_TokenSource::parse_asymmetrical_binaryop
///// Routine to parse a sequence of n operands X1, X2, ... each
///// separated by the same operation OP so X1 OP X2 OP X3 ... OP Xn
Rps_Value
Rps_TokenSource::parse_polyop(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, Rps_ObjectRef polyoper, Rps_ObjectRef polydelim,
std::function<Rps_Value(Rps_CallFrame*,Rps_TokenSource*,std::deque<Rps_Value>&,bool*)> parser_suboperand,
bool*pokparse, const char*opername)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(/*descr:*/nullptr,callframe,
Rps_Value resexprv;
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value curargv;
Rps_ObjectRef operob;
Rps_ObjectRef delimob;
);
std::vector<Rps_Value> argvect;
bool leftok = false;
/// GC needs this:
_f.operob = polyoper;
_f.delimob = polydelim;
_.set_additional_gc_marker([&](Rps_GarbageCollector* gc)
{
for (auto tokenv : token_deq)
gc->mark_value(tokenv);
for (auto curargv : argvect)
gc->mark_value(curargv);
});
std::string startpos = position_str();
if (!opername)
opername="???";
if (is_looking_ahead(pokparse))
{
// TODO: review, ... only lookahead for first suboperand....
_f.leftv = parser_suboperand(&_,this,token_deq, RPS_DO_LOOKAHEAD);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse symmetrical binop " << opername << " lookahead " << (_f.leftv?"success":"failure"));
return _f.leftv;
}
_f.leftv = parser_suboperand(&_,this,token_deq,&leftok);
if (!leftok)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse polyop " << opername << " LEFT FAILURE startpos:" << startpos
<< " curpos" << position_str() << " calldepth="
<< rps_call_frame_depth(&_));
if (pokparse)
*pokparse = false;
return nullptr;
}
argvect.push_back(_f.leftv);
while (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object() == polydelim)
{
bool okarg = false;
(void) get_token(&_); // consume the operator delimiter
_f.curargv = parser_suboperand(&_,this,token_deq,&okarg);
if (!okarg)
{
RPS_WARNOUT("parse_polyop failed to parse operand#" << argvect.size() <<" for "<< opername << " at " << position_str()
<< " starting " << startpos);
break;
}
argvect.push_back(_f.curargv);
}
_f.resexprv = Rps_InstanceValue(_f.operob, argvect);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse poly oper " << opername <<" END position:" << position_str()
<< " startpos:" << startpos
<< " resexpr:" << _f.resexprv
<< " calldepth="
<< rps_call_frame_depth(&_));
if (pokparse)
*pokparse = true;
return _f.resexprv;
} // end Rps_TokenSource::parse_polyop
/// This member function returns some expression which could later be
/// evaluated to a value; the *pokparse flag, when given, is set to
/// true if and only if parsing was successful.
Rps_Value
Rps_TokenSource::parse_expression(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
// a REPL expression is a sequence of disjuncts separated by ||
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(/*descr:*/nullptr,
/*callerframe:*/callframe,
Rps_Value resexprv;
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef lexkindob;
Rps_ObjectRef ordelimob;
Rps_ObjectRef oroperob;
Rps_Value lexvalv;
);
std::vector<Rps_Value> disjvect;
_.set_additional_gc_marker([&](Rps_GarbageCollector*gc)
{
// maybe token_deq is already GC-marked by caller....
for (auto tokenv : token_deq)
gc->mark_value(tokenv);
// but the disjvect needs to be GC-marked
for (auto disjv : disjvect)
gc->mark_value(disjv);
});
bool ok = false;
std::string startpos = position_str();
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression start position:" << startpos << " calldepth="
<< rps_call_frame_depth(&_));
_f.lextokv = lookahead_token(&_, token_deq, 0);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression lextokv="
<< _f.lextokv << " position:" << position_str()<< " startpos:" << startpos);
if (!_f.lextokv)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.leftv = parse_disjunction(&_, token_deq, &ok);
if (!ok)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
disjvect.push_back(_f.leftv);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression leftv=" << _f.leftv << " position:" << position_str()<< " startpos:" << startpos);
bool again = false;
static Rps_Id idordelim;
if (!idordelim)
idordelim = Rps_Id("_1HsUfOkNw0W033EIW1"); // id of "or!binop"∈repl_delimiter
static Rps_Id idoroper;
if (!idoroper)
idoroper = Rps_Id("_1ghZV0g1dtR02xPgqk"); // id of "or!binop"∈repl_binary_operator
do
{
again = false;
_f.lextokv = lookahead_token(&_, token_deq, 0);
if (!_f.lextokv)
{
again = false;
break;
};
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression testing or lextokv=" << _f.lextokv << " position:" << position_str()<< " startpos:" << startpos);
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object()->oid() == idordelim)
{
(void) get_token(&_); // consume the or operator
again = true;
if (!_f.oroperob)
_f.oroperob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,idoroper);
}
else
again = false;
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression oroperob="
<< _f.oroperob
<< " position:" << position_str()<< " startpos:" << startpos);
if (again)
{
bool okright=false;
_f.rightv = parse_disjunction(&_, token_deq, &okright);
if (okright)
disjvect.push_back(_f.rightv);
else
{
RPS_WARNOUT("failed to parse disjunct at " << position_str());
return nullptr;
}
}
}
while (again);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression oroperob=" << _f.oroperob
<< "nbdisj:" << disjvect.size()
<< " position:" << position_str()<< " startpos:" << startpos);
if (disjvect.size() > 1)
{
/// we make an instance:
_f.resexprv = Rps_InstanceValue(_f.oroperob, disjvect);
}
else
{
_f.resexprv = disjvect[0];
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_expression result: "
<< _f.resexprv << " position:" << position_str()<< " startpos:" << startpos
<< " calldepth=" << rps_call_frame_depth(&_)
<< " token_deq=" << token_deq);
return _f.resexprv;
} // end Rps_TokenSource::parse_expression
////////////////
/// This member function returns some disjunct which could later be
/// evaluated to a value; the *pokparse flag, when given, is set to
/// true if and only if parsing was successful.
Rps_Value
Rps_TokenSource::parse_disjunction(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
/// a disjunction is a sequence of one or more conjunct separated by
/// && - the and operator
RPS_LOCALFRAME(/*descr:*/nullptr,
/*callerframe:*/callframe,
Rps_Value resdisjv;
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef lexkindob;
Rps_ObjectRef ordelimob;
Rps_ObjectRef oroperob;
Rps_Value lexvalv;
);
std::vector<Rps_Value> conjvect;
_.set_additional_gc_marker([&](Rps_GarbageCollector*gc)
{
// maybe token_deq is already GC-marked by caller....
for (auto tokenv : token_deq)
gc->mark_value(tokenv);
// but the conjvect needs to be GC-marked
for (auto conjv : conjvect)
gc->mark_value(conjv);
});
bool ok = false;
std::string startpos = position_str();
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction startpos:"
<< startpos << " calldepth#" << rps_call_frame_depth(&_));
_f.lextokv = lookahead_token(&_, token_deq, 0);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction lextokv="
<< _f.lextokv
<< " position: " << position_str() << " startpos=" << startpos);
if (!_f.lextokv)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.leftv = parse_conjunction(&_, token_deq, &ok);
if (!ok)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
conjvect.push_back(_f.leftv);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction leftv=" << _f.leftv
<< " position: " << position_str() << " startpos:" << startpos);
bool again = false;
static Rps_Id idordelim;
if (!idordelim)
idordelim = Rps_Id("_1HsUfOkNw0W033EIW1"); // id of "or!delim"∈repl_delimiter
static Rps_Id idoroper;
if (!idoroper)
idoroper = Rps_Id("_1ghZV0g1dtR02xPgqk"); // id of "or!binop"∈repl_binary_operator
do
{
again = false;
_f.lextokv = lookahead_token(&_, token_deq, 0);
if (!_f.lextokv)
{
again = false;
break;
};
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction testing and lextokv=" << _f.lextokv << " position:" << position_str() << " startpos:" << startpos);
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind()
== RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object()->oid() == idordelim)
{
(void) get_token(&_); // consume the or operator
again = true;
if (!_f.oroperob)
_f.oroperob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,idoroper);
}
else
again = false;
if (again)
{
bool okright=false;
_f.rightv = parse_conjunction(&_, token_deq, &okright);
if (okright)
conjvect.push_back(_f.rightv);
else
{
RPS_WARNOUT("Rps_TokenSource::parse_disjunction failed to parse conjunct at " << position_str() << " starting " << startpos);
return nullptr;
}
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction position:" << position_str() << " startpos:" << startpos << "endloop "
<< (again?"again":"STOP")
<< " calldepth#" << rps_call_frame_depth(&_));
}
while (again);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction oroperob=" << _f.oroperob
<< "nbconj:" << conjvect.size() << " position:" << position_str() << " startpos:" << startpos
<< " calldepth#" << rps_call_frame_depth(&_));
if (conjvect.size() > 1)
{
/// we make an instance:
_f.resdisjv = Rps_InstanceValue(_f.oroperob, conjvect);
}
else
{
_f.resdisjv = conjvect[0];
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction result "
<< _f.resdisjv << " position:" << position_str()
<< " startpos:" << startpos
<< " calldepth#" << rps_call_frame_depth(&_));
return _f.resdisjv;
} // end Rps_TokenSource::parse_disjunction
////////////////
Rps_Value
Rps_TokenSource::parse_conjunction(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
RPS_LOCALFRAME(nullptr, callframe,
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_Value disjv;
Rps_Value conjv;
Rps_ObjectRef lexkindob;
Rps_ObjectRef anddelimob;
Rps_ObjectRef andbinopob;
Rps_Value lexvalv;);
std::vector<Rps_Value> conjvect;
_.set_additional_gc_marker([&](Rps_GarbageCollector* gc)
{
for (auto tokenv : token_deq)
gc->mark_value(tokenv);
for (auto disjv : conjvect)
gc->mark_value(disjv);
});
static Rps_Id id_and_delim;
if (!id_and_delim)
id_and_delim = Rps_Id("_2YVmrhVcwW00120rTK");
static Rps_Id id_and_binop;
if (!id_and_binop)
id_and_binop = Rps_Id("_8xk4sKglyzG01dloqq");
_f.anddelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_and_delim);
_f.andbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_and_binop);
//
bool ok = false;
_f.lextokv = lookahead_token(&_, token_deq, 0);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_conjunction lextokv="
<< _f.lextokv
<< " position: "
<< position_str()
<< " anddelim:" << _f.anddelimob
<< " andbinop:" << _f.andbinopob);
if (!_f.lextokv)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
std::string startpos = position_str();
_f.leftv = parse_comparison(&_, token_deq, &ok);
if (!ok)
{
if (pokparse)
*pokparse = false;
return nullptr;
}
conjvect.push_back(_f.leftv);
bool again = false;
do
{
again = false;
_f.lextokv = lookahead_token(&_, token_deq, 0);
if (!_f.lextokv)
{
again = false;
break;
};
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_conjunction testing or lextokv=" << _f.lextokv << " position:" << position_str());
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object()
&& _f.lextokv.to_lextoken()->lxval().to_object()->oid() == id_and_delim)
{
(void) get_token(&_); // consume the and operator
again = true;
if (!_f.andbinopob)
_f.andbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_and_binop);
}
else
again = false;
if (again)
{
bool okright=false;
_f.rightv = parse_comparison(&_, token_deq, &okright);
if (okright)
conjvect.push_back(_f.rightv);
else
{
RPS_WARNOUT("failed to parse conjunct at " << position_str());
return nullptr;
}
}
}
while (again);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_disjunction andbinopob=" << _f.andbinopob
<< "nbconj:" << conjvect.size() << " at " << startpos);
if (conjvect.size() > 1)
{
/// we make an instance:
_f.conjv = Rps_InstanceValue(_f.andbinopob, conjvect);
}
else
{
_f.conjv = conjvect[0];
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_conjunction gives "
<< _f.conjv << " at " << startpos);
return _f.conjv;
} // end Rps_TokenSource::parse_conjunction
Rps_Value
Rps_TokenSource::parse_comparison(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(nullptr, callframe,
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_Value compv;
Rps_ObjectRef cmpdelimob;
Rps_ObjectRef cmpbinopob;
);
std::string startpos = position_str();
/// lessequal is <=
static Rps_Id id_lessequal_delim;
if (!id_lessequal_delim)
id_lessequal_delim = Rps_Id("_1mfq8qfixB401umCL9");
static Rps_Id id_lessequal_binop;
if (!id_lessequal_binop)
id_lessequal_binop = Rps_Id("_7kiAonuM6tZ018OcyU");
// less is <
static Rps_Id id_less_delim;
if (!id_less_delim)
id_less_delim = Rps_Id("_0yxQCphDiO102S0BnY");
static Rps_Id id_less_binop;
if (!id_less_binop)
id_less_binop = Rps_Id("_7E9GRiz630X04AEDlB");
/// greater is >
static Rps_Id id_greater_delim;
if (!id_greater_delim)
id_greater_delim = Rps_Id("_04IcidRP5Jh00zW0qs");
static Rps_Id id_greater_binop;
if (!id_greater_binop)
id_greater_binop = Rps_Id("_40mWEiSX65P02fzIdD");
/// greaterequal is >=
static Rps_Id id_greaterequal_delim;
if (!id_greaterequal_delim)
id_greaterequal_delim = Rps_Id("_312DlYVXeEs01aTJpg");
static Rps_Id id_greaterequal_binop;
if (!id_greaterequal_binop)
id_greaterequal_binop = Rps_Id("_8p431uwpLJI00r5FQD");
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparison startpos:" << startpos);
bool okleft = false;
_f.leftv = parse_comparand(&_, token_deq, &okleft);
if (okleft)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparison leftv=" << _f.leftv << " startpos:" << startpos);
}
else
{
RPS_WARNOUT("parse_comparison failed to parse left comparand at " << startpos
<< " current line:" << Rps_QuotedC_String(current_line())
<< " curpos " << position_str()
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "Rps_TokenSource::parse_comparison fail"));
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.lextokv = lookahead_token(&_, token_deq, 0);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparison after left lextok=" << _f.lextokv << " pos:" << position_str());
if (!_f.lextokv)
{
if (pokparse)
*pokparse = true;
return _f.leftv;
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparison testing lextokv=" << _f.lextokv << " position:" << position_str());
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object())
{
Rps_Id delimid = _f.lextokv.to_lextoken()->lxval().to_object()->oid();
RPS_WARNOUT("Rps_TokenSource::parse_comparison incomplete delimid=" << delimid << " position:" << position_str()
<< " startpos:" << startpos
<< std::endl << " leftv=" << _f.leftv << " lextokv=" << _f.lextokv);
}
else
{
RPS_WARNOUT("Rps_TokenSource::parse_comparison unexpected lextokv="
<< _f.lextokv << " position:" << position_str()
<< " startpos:" << startpos
<< std::endl << " leftv=" << _f.leftv);
}
#warning unimplemented Rps_TokenSource::parse_comparison
RPS_FATALOUT("missing code in Rps_TokenSource::parse_comparison from " << Rps_ShowCallFrame(callframe)
<< " with token_deq=" << token_deq << " at " << position_str()
<< " startpos:" << startpos
<< std::endl
<< "... leftv=" << _f.leftv << " lextokv=" << _f.lextokv);
} // end Rps_TokenSource::parse_comparison
// a comparand - something on left or right side of compare operators is a sequence of terms with additive operators
Rps_Value
Rps_TokenSource::parse_comparand(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(nullptr, callframe,
Rps_Value lextokv;
Rps_Value lexopertokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_Value compv;
Rps_ObjectRef cmpdelimob;
Rps_ObjectRef cmpbinopob;
);
std::string startpos = position_str();
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparand start from " << Rps_ShowCallFrame(&_)
<< " with token_deq=" << token_deq << " at " << startpos);
bool okleft = false;
_f.leftv = parse_term(&_, token_deq, &okleft);
if (okleft)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparand leftv=" << _f.leftv << " startpos:" << startpos
<< " currentpos:" << position_str());
}
else
{
RPS_WARNOUT("parse_comparand failed to parse left comparand at " << startpos
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "Rps_TokenSource::parse_comparand fail"));
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.lextokv = lookahead_token(&_, token_deq, 0);
_f.lexopertokv = lookahead_token(&_, token_deq, 1);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_comparand leftv=" << _f.leftv
<< " lextokv:" << _f.lextokv << " lexopertokv:" << _f.lexopertokv
<< " startpos:" << startpos
<< " currentpos:" << position_str()
<< " curcptr " << Rps_QuotedC_String(curcptr()) << "@" << (void*)curcptr());
#warning unimplemented Rps_TokenSource::parse_comparand
/***
* we probably should loop and collect all terms if they are separated by the same additive operator
***/
RPS_FATALOUT("missing code in Rps_TokenSource::parse_comparand from " << Rps_ShowCallFrame(callframe)
<< " with token_deq=" << token_deq << " at startpos: " << startpos
<< " currentpos:" << position_str()
<< " curcptr " << Rps_QuotedC_String(curcptr()) << "@" << (void*)curcptr()
<< std::endl
<< "... lextokv=" << _f.lextokv << " lexopertokv=" << _f.lexopertokv << " leftv=" << _f.leftv
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_TokenSource::parse_comparand incomplete"));
} // end Rps_TokenSource::parse_comparand
Rps_Value
Rps_TokenSource::parse_factor(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(nullptr, callframe,
Rps_Value lextokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef binoperob;
Rps_ObjectRef plusdelimob;
Rps_ObjectRef plusbinopob;
Rps_ObjectRef minusdelimob;
Rps_ObjectRef minusbinopob;
);
std::string startpos = position_str();
/// + delimiter and binary operator
static Rps_Id id_plus_delim;
if (!id_plus_delim)
id_plus_delim = Rps_Id("_4ShDsOWk7al02eDRTM");
static Rps_Id id_plus_binop;
if (!id_plus_binop)
id_plus_binop = Rps_Id("_51jvc2mFhql03qwRg6");
_f.plusdelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_plus_delim); // "plus!delim"∈repl_delimiter
_f.plusbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_plus_binop); // "plus!binop"∈repl_binary_operator
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor plusdelimob=" << _f.plusdelimob << " plusbinopob=" << _f.plusbinopob);
/// - delimiter and binary operator
static Rps_Id id_minus_delim;
if (!id_minus_delim)
id_minus_delim = Rps_Id("_3isfNUOsxXY02Bww6l");
static Rps_Id id_minus_binop;
if (!id_minus_binop)
id_minus_binop = Rps_Id("_13ffyyJhGHI01ySLhK");
_f.minusdelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_minus_delim); // "minus!delim"∈repl_delimiter
_f.minusbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_minus_binop); // "minus!binop"∈repl_binary_operator
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor minusdelimob=" << _f.minusdelimob << " minusbinopob=" << _f.minusbinopob);
///
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor start from " << Rps_ShowCallFrame(&_)
<< " with token_deq=" << token_deq << " at " << startpos);
bool okleft = false;
#warning Rps_TokenSource::parse_factor should probably use parse_primary here
_f.leftv = parse_term(&_, token_deq, &okleft);
if (okleft)
{
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor leftv=" << _f.leftv << " startpos:" << startpos);
}
else
{
RPS_WARNOUT("parse_factor failed to parse left term at " << startpos);
if (pokparse)
*pokparse = false;
return nullptr;
}
_f.lextokv = lookahead_token(&_, token_deq, 0);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor lextokv=" << _f.lextokv
<< " with token_deq=" << token_deq << " at " << startpos);
if (_f.lextokv.is_lextoken()
&& _f.lextokv.to_lextoken()->lxkind() == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK) //repl_delimiter∈class
&& _f.lextokv.to_lextoken()->lxval().is_object())
{
if (_f.lextokv.to_lextoken()->lxval().to_object() == _f.plusdelimob)
_f.binoperob = _f.plusbinopob;
else if (_f.lextokv.to_lextoken()->lxval().to_object() == _f.minusdelimob)
_f.binoperob = _f.minusbinopob;
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor lextokv=" << _f.lextokv
<< " with token_deq=" << token_deq << " at " << startpos << " binoperob=" << _f.binoperob);
if (_f.binoperob)
{
bool okright = false;
/// consume the + or - delimiter...
_f.lexgotokv = get_token(&_);
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor lexgotokv=" << _f.lexgotokv
<< " with token_deq=" << token_deq << " at "
<< startpos << " binoperob=" << _f.binoperob);
RPS_ASSERT(_f.lexgotokv == _f.lextokv);
_f.rightv = parse_term(&_, token_deq, &okright);
if (!okright)
{
RPS_WARNOUT("parse_factor failed to parse right term starting " << startpos << " at " << position_str());
if (pokparse)
*pokparse = false;
return nullptr;
}
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_factor leftv=" << _f.leftv
<< " rightv=" << _f.rightv
<< " with token_deq=" << token_deq << " at "
<< startpos << " binoperob=" << _f.binoperob);
}
else
{
}
#warning unimplemented Rps_TokenSource::parse_factor
RPS_FATALOUT("missing code in Rps_TokenSource::parse_factor from " << Rps_ShowCallFrame(callframe)
<< " with token_deq=" << token_deq << " at " << startpos);
} // end Rps_TokenSource::parse_factor
/// a term is a sequence of factors with multiplicative operators
/// between them.... All the operators should be the same. Otherwise we build intermediate subexpressions
Rps_Value
Rps_TokenSource::parse_term(Rps_CallFrame*callframe, std::deque<Rps_Value>& token_deq, bool*pokparse)
{
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(nullptr, callframe,
Rps_Value restermv;
Rps_Value lextokv;
Rps_Value lexopertokv;
Rps_Value lexgotokv;
Rps_Value leftv;
Rps_Value rightv;
Rps_ObjectRef binoperob;
Rps_ObjectRef curoperob;
Rps_ObjectRef bindelimob;
Rps_ObjectRef multdelimob;
Rps_ObjectRef multbinopob;
Rps_ObjectRef divdelimob;
Rps_ObjectRef divbinopob;
Rps_ObjectRef moddelimob;
Rps_ObjectRef modbinopob;
Rps_ObjectRef lexoperdelimob;
);
std::vector<Rps_Value> operandvect;
_.set_additional_gc_marker([&](Rps_GarbageCollector*gc)
{
// maybe token_deq is already GC-marked by caller....
for (auto tokenv : token_deq)
gc->mark_value(tokenv);
// but the operandvect needs to be GC-marked
for (auto operv : operandvect)
gc->mark_value(operv);
});
std::string startpos = position_str();
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_term starting startpos:" << startpos);
/// multiplication operator and * delim
static Rps_Id id_mult_delim;
if (!id_mult_delim)
id_mult_delim = Rps_Id("_2uw3Se5dPOU00yhxpA"); // id of "mult!delim"∈repl_delimiter
static Rps_Id id_mult_oper;
if (!id_mult_oper)
id_mult_oper = Rps_Id("_4QX7Cg3gDkd005b9bn"); // id of "mult!binop"∈repl_binary_operator
_f.multdelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_mult_delim); // "mult!delim"∈repl_delimiter
_f.multbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_mult_oper); // "mult!binop"∈repl_binary_operator
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_term startpos:" << startpos << " multdelimob:" << _f.multdelimob
<< " multbinopob: " << _f.multbinopob);
/// division operator and / delim
static Rps_Id id_div_delim;
if (!id_div_delim)
id_div_delim = Rps_Id("_3ak80l3pr9700M90pz"); // id of "div!delim"∈repl_delimiter
static Rps_Id id_div_oper;
if (!id_div_oper)
id_div_oper = Rps_Id("_0GTVGelTnCP01I0od2"); // id of "div!binop"∈repl_binary_operator
_f.divdelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_div_delim); // "div!delim"∈repl_delimiter
_f.divbinopob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_div_oper); // "div!binop"∈repl_binary_operator
RPS_DEBUG_LOG(REPL, "Rps_TokenSource::parse_term startpos:" << startpos << " divdelimob:" << _f.divdelimob
<< " divbinopob: " << _f.divbinopob);
/// modulus operator and % delim
static Rps_Id id_mod_delim;
if (!id_mod_delim)
id_mod_delim = Rps_Id("_5yI4nqeRQdR02PcUIi"); // id of mod!delim"∈repl_delimiter
static Rps_Id id_mod_oper;
if (!id_mod_oper)
id_mod_oper = Rps_Id("_07mKKY7ByIq03w7k4J"); // id of "mod!binop"∈repl_binary_operator
_f.moddelimob = Rps_ObjectRef::find_object_or_fail_by_oid(&_,id_mod_delim); // "mod!delim"∈repl_delimiter