forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl_rps.cc
More file actions
1875 lines (1758 loc) · 72.7 KB
/
repl_rps.cc
File metadata and controls
1875 lines (1758 loc) · 72.7 KB
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 repl_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the Read-Eval-Print-Loop code but don't use GNU readline
* anymore in Feb. 2024 (after commit 5b20c981b9e)
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2019 - 2025 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"
#include "wordexp.h"
extern "C" const char rps_repl_gitid[];
const char rps_repl_gitid[]= RPS_GITID;
extern "C" const char rps_repl_date[];
const char rps_repl_date[]= __DATE__;
extern "C" const char rps_repl_shortgitid[];
const char rps_repl_shortgitid[]= RPS_SHORTGITID;
std::vector<std::string> rps_completion_vect;
/// a C++ closure for getting the REPL lexical token.... with
/// lookahead=0, next token, with lookahead=1 the second-next token
std::function<Rps_LexTokenValue(Rps_CallFrame*,unsigned)> rps_repl_cmd_lexer_fun;
/// these REPL lexical tokens are looked ahead, so we need a function
/// to consume them... Returning true when the leftmost token is
/// forgotten
std::function<bool(Rps_CallFrame*)> rps_repl_consume_cmd_token_fun;
bool rps_repl_stopped;
std::string
rps_repl_version(void)
{
std::string res = "REPL";
{
char gitstart[48];
memset (gitstart, 0, sizeof(gitstart));
strncpy(gitstart, rps_repl_gitid, sizeof(gitstart)-2);
res += " git ";
res += gitstart;
}
return res;
} // end rps_repl_version
extern "C" void
rps_do_builtin_repl_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd, Rps_TokenSource& intoksrc,
const char*title);
extern "C" rps_applyingfun_t rpsapply_repl_not_implemented;
Rps_TwoValues
rpsapply_repl_not_implemented(Rps_CallFrame*callerframe,
const Rps_Value arg0,
const Rps_Value arg1,
const Rps_Value arg2,
const Rps_Value arg3,
const std::vector<Rps_Value>* restargs)
{
if (!restargs)
RPS_WARNOUT("rpsapply_repl_not_implemented arg0:" << arg0
<< " arg1:" << arg1
<< " arg2:" << arg2
<< " arg3:" << arg3
<< std::endl
<< " from caller frame:"
<< Rps_ShowCallFrame(callerframe)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rpsapply_repl_not_implemented"));
else
{
auto nbargs = restargs->size();
std::ostringstream outs;
for (int aix = 0; aix < (int)nbargs; aix++)
{
outs << " rest#" << aix << ":" << restargs->at(aix);
}
outs.flush();
RPS_WARNOUT("rpsapply_repl_not_implemented arg0:" << arg0
<< " arg1:" << arg1
<< " arg2:" << arg2
<< " arg3:" << arg3
<< std::endl
<< outs.str()
<< std::endl
<< " from caller frame:"
<< Rps_ShowCallFrame(callerframe)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rpsapply_repl_not_implemented/many"));
}
return {nullptr,nullptr};
} // end rpsapply_repl_not_implemented
/// Create a new REPL command, and output to stdout some draft C++
/// code to parse it.... To be called from the main thread.
void
rps_repl_create_command(Rps_CallFrame*callframe, const char*commandname)
{
RPS_LOCALFRAME(/*descr:*/RPS_ROOT_OB(_4CZZ2JlnkQT02YJ6sM), //repl_command∈symbol
/*callerframe:*/callframe,
Rps_ObjectRef obsymb;
Rps_ObjectRef obcommand;
Rps_ObjectRef obfun;
Rps_ObjectRef obreplcmdclass;
Rps_Value closv;
Rps_Value strnamev;
);
RPS_ASSERT(rps_is_main_thread());
RPS_ASSERT(callframe && callframe->is_good_call_frame(callframe));
RPS_ASSERT(commandname != nullptr);
_f.obreplcmdclass = RPS_ROOT_OB(_8CncrUdoSL303T5lOK); //repl_command∈class
bool goodname = isalpha(commandname[0]);
for (const char*pc = commandname; *pc && goodname; pc++)
goodname = isalnum(*pc)
|| (pc>commandname && *pc == '_' && pc[-1] != '_');
if (!goodname)
{
RPS_WARNOUT("rps_repl_create_command invalid command name " << commandname << std::endl
<< ".. called from " << Rps_ShowCallFrame(&_));
std::string msg = "invalid REPL command name ";
msg += commandname;
throw std::runtime_error(msg);
};
_f.strnamev = Rps_StringValue(commandname);
_f.obsymb = Rps_ObjectRef::make_new_strong_symbol(&_, std::string(commandname));
RPS_DEBUG_LOG(CMD, "rps_repl_create_command commandname " << commandname
<< " -> obsymb=" << _f.obsymb);
RPS_ASSERT(_f.obsymb);
Rps_PayloadSymbol* paylsymb =
_f.obsymb->get_dynamic_payload<Rps_PayloadSymbol>();
RPS_ASSERT(paylsymb);
/// the command name should be a fresh symbol...
if (paylsymb->symbol_value())
{
RPS_WARNOUT("rps_repl_create_command command name "
<< commandname << " already known as symbol " << _f.obsymb
<< std::endl
<< ".. called from " << Rps_ShowCallFrame(&_));
return;
}
_f.obcommand
= Rps_ObjectRef::make_object(&_,
RPS_ROOT_OB(_8CncrUdoSL303T5lOK), //repl_command∈class
Rps_ObjectRef::root_space());
paylsymb->symbol_put_value(_f.obcommand);
RPS_DEBUG_LOG(CMD, "rps_repl_create_command commandname " << commandname
<< " -> obcommand=" << _f.obcommand);
/* We need to create some object ObFun, of class 9Gz1oNPCnkB00I6VRS
== core_function∈class and make a closure from it; that closure
would be the repl_command_parser == _4I8GwXXfO3P01cdzyd of
ObFun. We also need to output on stdout some C++ skeleton code
for it. */
_f.obfun
= Rps_ObjectRef::make_object(&_,
RPS_ROOT_OB(_9Gz1oNPCnkB00I6VRS), //core_function∈class
Rps_ObjectRef::root_space());
RPS_DEBUG_LOG(CMD, "rps_repl_create_command commandname " << commandname
<< " -> obfun=" << _f.obfun);
_f.strnamev = Rps_StringValue(std::string{commandname} + "°replcfun");
_f.closv = Rps_ClosureValue(_f.obfun, {_f.obcommand,_f.obsymb});
_f.obfun->put_attr(RPS_ROOT_OB(_8CncrUdoSL303T5lOK), //repl_command∈class
_f.obcommand);
_f.obfun->put_attr(Rps_ObjectRef::the_name_object(),
_f.strnamev);
_f.obfun->put_applying_function(rpsapply_repl_not_implemented);
RPS_DEBUG_LOG(CMD, "rps_repl_create_command commandname " << commandname
<< " -> closv=" << _f.closv);
_f.obcommand->put_attr(RPS_ROOT_OB(_4I8GwXXfO3P01cdzyd), /// repl_command_parser∈symbol
_f.closv);
_f.strnamev = Rps_StringValue(commandname);
_f.obcommand->put_attr(Rps_ObjectRef::the_name_object(),
_f.strnamev);
// the symbol should be reachable at dump time and known to the command
_f.obcommand->put_attr(Rps_ObjectRef::the_symbol_class(),
_f.obsymb);
std::cout << std::endl << std::endl << std::endl
<< "/*# C++ function " << _f.obfun << " for REPL command " << commandname << "*/" << std::endl;
std::cout << "extern \"C\" rps_applyingfun_t rpsapply" << _f.obfun->oid() << ";" << std::endl;
std::cout << "Rps_TwoValues" << std::endl << "rpsapply"
<< _f.obfun->oid() << "(Rps_CallFrame*callerframe," << std::endl
<< " const Rps_Value arg0," << std::endl
<< " const Rps_Value arg1," << std::endl
<< " [[maybe_unused]] const Rps_Value arg2," << std::endl
<< " [[maybe_unused]] const Rps_Value arg3," << std::endl
<< " [[maybe_unused]] const std::vector<Rps_Value>* restargs)" << std::endl
<< "{" << std::endl
<< " static Rps_Id descoid;\n"
<< " if (!descoid)\n" " descoid=Rps_Id(\"" << _f.obfun->oid() << "\");" << std::endl
<< " RPS_" "LOCALFRAME(/*descr:*/Rps_ObjectRef::really_find_object_by_oid(descoid)," << std::endl
<< " callerframe," << std::endl
<< " );" << std::endl
<< " RPS_" "DEBUG_LOG(CMD, \"REPL command " << commandname << " start arg0=\" << arg0 << \"∈\" << arg0.compute_class(&_)"
<< std::endl
<< " << \" arg1=\" << arg1 << << \"∈\" << arg1.compute_class(&_) << std::endl" << std::endl
<< " << \" from \" << std::endl" << std::endl
<< " << Rps_ShowCallFrame(&_));" << std::endl
<< "#warning incomplete rpsapply" << _f.obfun->oid() << " for REPL command " << commandname << std::endl
<< " RPS_" "WARNOUT(\"incomplete rpsapply" << _f.obfun->oid() << " for REPL command " << commandname << " from \" << std::endl" << std::endl
<< " << RPS_FULL_BACKTRACE_HERE(1, \"rpsapply" << _f.obfun->oid() << " for REPL command " << commandname << "\"));" << std::endl
<< " return {nullptr,nullptr};" << std::endl
<< "} //end of rpsapply" << _f.obfun->oid() << " for REPL command " << commandname
<< std::endl << std::endl;
_f.obreplcmdclass->append_comp1(Rps_ObjectValue(_f.obcommand));
RPS_DEBUG_LOG(CMD, "rps_repl_create_command commandname " << commandname
<< " added " << _f.obcommand << " to repl_command class " << _f.obreplcmdclass);
/* see also OBSOLETErps_repl_interpret which would apply that closure */
#warning rps_repl_create_command incomplete
RPS_WARNOUT("rps_repl_create_command incomplete for command "
<< commandname << " obfun=" << _f.obfun
<< " obcommand=" << _f.obcommand);
} // end rps_repl_create_command
Rps_Value
rps_lex_chunk_element(Rps_CallFrame*callframe, Rps_ObjectRef obchkarg, Rps_ChunkData_st*chkdata);
/// Inside a code chunk represented by object obchkarg, parse some
/// chunk element...
Rps_Value
rps_lex_chunk_element(Rps_CallFrame *callframe,
[[maybe_unused]] Rps_ObjectRef obchkarg, Rps_ChunkData_st *chkdata)
{
RPS_ASSERT(chkdata != nullptr
&& chkdata->chunkdata_magic == rps_chunkdata_magicnum);
RPS_ASSERT(callframe != nullptr
&& callframe->is_good_call_frame());
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obchunk;
Rps_Value chkelemv;
Rps_ObjectRef namedobv;
);
#warning rps_lex_chunk_element is obsolete code
#if 0 && oldcode
RPS_ASSERT(chkdata->chunkdata_plinebuf != nullptr);
_f.obchunk = obchkarg;
RPS_ASSERT(_f.obchunk);
auto paylvect = _f.obchunk->get_dynamic_payload<Rps_PayloadVectVal>();
RPS_ASSERT(paylvect != nullptr);
const char*linestart = nullptr; //*chkdata->chunkdata_plinebuf;
#warning obsolete code in rps_lex_chunk_element which has no more sense
int linelen = strlen(linestart);
RPS_ASSERT(chkdata->chunkdata_colno >= 0 && chkdata->chunkdata_colno<linelen);
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element start obchunk=" << _f.obchunk
<< ", linestart='" << linestart << "', linelen=" << linelen
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno
<< " endstr='" << chkdata->chunkdata_endstr
<< "' current:"
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_UNDERLINE_ESCAPE:"`")
<< linestart+chkdata->chunkdata_colno
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_NORMAL_ESCAPE:"'")
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_lex_chunk_element-start"));
/// For C name-like things, we return the object naming them or else a string
const char*curstr = linestart+chkdata->chunkdata_colno;
if (isalpha(*curstr))
{
int startnamcol = chkdata->chunkdata_colno;
int endnamcol = startnamcol;
while (endnamcol<linelen && (isalnum(linestart[endnamcol]) || linestart[endnamcol]=='_'))
endnamcol++;
int namlen = endnamcol-startnamcol;
std::string namstr = std::string(linestart+startnamcol, namlen);
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element obchunk=" << _f.obchunk
<< " namstr='" << namstr << "' starting L"
<< chkdata->chunkdata_lineno << ",C" << startnamcol
<< " endnamcol=" << endnamcol);
_f.namedobv = Rps_ObjectRef::find_object_by_string(&_, namstr,
Rps_ObjectRef::Null_When_Missing);
chkdata->chunkdata_colno = endnamcol;
curstr = nullptr;
if (_f.namedobv)
{
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element name obchunk=" << _f.obchunk
<< " -> namedobv=" << _f.namedobv
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno);
return _f.namedobv;
}
else
{
_f.chkelemv = Rps_StringValue(namstr);
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element string obchunk=" << _f.obchunk
<< " -> chkelemv=" << _f.chkelemv
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno);
return _f.chkelemv;
}
}
/// For sequence of spaces, we return an instance of class space and value the number of space characters
else if (isspace(*curstr))
{
int startspacecol = chkdata->chunkdata_colno;
int endspacecol = startspacecol;
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element start space obchunk=" << _f.obchunk
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno);
while (endspacecol<linelen && isspace(linestart[endspacecol]))
endspacecol++;
_f.chkelemv = Rps_InstanceValue(RPS_ROOT_OB(_2i66FFjmS7n03HNNBx), //space∈class
std::initializer_list<Rps_Value>
{
Rps_Value((intptr_t)(endspacecol-startspacecol),
Rps_Value::Rps_IntTag{})
});
chkdata->chunkdata_colno += endspacecol-startspacecol+1;
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element space obchunk=" << _f.obchunk
<< " -> chkelemv=" << _f.chkelemv
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno);
curstr = nullptr;
return _f.chkelemv;
}
/// code chunk meta-variable or meta-notation....
else if (*curstr == '$'
&& chkdata->chunkdata_colno < linelen)
{
// a dollar followed by a name is a meta-variable; that name should be known
const char*metastr = curstr;
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element start meta obchunk=" << _f.obchunk
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno
<< " endstr='" << chkdata->chunkdata_endstr
<< "' metastr:" << metastr);
if (isalpha(metastr[1]))
{
int startnameix=1, endnameix=1;
while (isalnum(metastr[endnameix])||metastr[endnameix]=='_')
endnameix++;
std::string metaname(metastr+1, endnameix-startnameix);
_f.namedobv = Rps_ObjectRef::find_object_by_string(&_, metaname,
Rps_ObjectRef::Null_When_Missing);
if (!_f.namedobv)
{
RPS_WARNOUT("rps_lex_chunk_element: bad metavariable name " << metaname
<< " input " << chkdata->chunkdata_input_name
<< " line " << chkdata->chunkdata_lineno << ", column " << chkdata->chunkdata_colno);
throw std::runtime_error("lexical error - metaname in code chunk");
}
chkdata->chunkdata_colno += endnameix-startnameix+1;
_f.chkelemv = Rps_InstanceValue(RPS_ROOT_OB(_1oPsaaqITVi03OYZb9), //meta_variable∈symbol
std::initializer_list<Rps_Value> {_f.namedobv});
return _f.chkelemv;
}
/// two dollars are parsed as one
else if (metastr[1] == '$')
{
// two dollars should be parsed as a single one, and we make that a string with following letters...
int startnameix=1, endnameix=2;
while (isalnum(metastr[endnameix])||metastr[endnameix]=='_')
endnameix++;
std::string dollname(metastr+1, endnameix-startnameix);
chkdata->chunkdata_colno += endnameix-startnameix+1;
_f.chkelemv = Rps_StringValue(dollname);
return _f.chkelemv;
}
/// a dollar followed by a dot is ignored....
else if (metastr[1] == '.')
{
chkdata->chunkdata_colno += 2;
return nullptr;
}
/// probably other dollar things should be parsed as delimiters....
}
else if (*curstr=='}' && !strncmp(curstr, chkdata->chunkdata_endstr, strlen(chkdata->chunkdata_endstr)))
{
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element ending callframe=" << Rps_ShowCallFrame(callframe)
<< std::endl << "… obchunk=" << _f.obchunk
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno
<< " curstr:"
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_UNDERLINE_ESCAPE:"`")
<< curstr
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_NORMAL_ESCAPE:"'")
<< " endstr='" << chkdata->chunkdata_endstr << "'"
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_lex_chunk_element-ending")
);
chkdata->chunkdata_colno += strlen(chkdata->chunkdata_endstr);
return nullptr;
}
else
{
RPS_DEBUG_LOG(REPL, "rps_lex_chunk_element INCOMPLETE callframe=" << Rps_ShowCallFrame(callframe)
<< std::endl << "… obchunk=" << _f.obchunk
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno
<< " curstr:"
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_UNDERLINE_ESCAPE:"`")
<< curstr
<< ((rps_stdout_istty && !rps_batch)?RPS_TERMINAL_NORMAL_ESCAPE:"'")
<< " endstr='" << chkdata->chunkdata_endstr << "'"
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_lex_chunk_element-incomplete")
);
}
RPS_FATALOUT("unimplemented rps_lex_chunk_element callframe=" << Rps_ShowCallFrame(callframe)
<< " obchkarg=" << obchkarg
<< " chkdata=" << chkdata
<< " @L" << chkdata->chunkdata_lineno << ",C"
<< chkdata->chunkdata_colno
<< " linestart='" << linestart << "'");
#endif /*0 && obsolete*/
#warning we need to document and implement other chunk element conventions in rps_lex_chunk_element, in particular delimiters...
#warning unimplemented rps_lex_chunk_element
return nullptr;
} // end rps_lex_chunk_element
////////////////////////////////////////////////////////////////
//// the Rps_LexTokenZone values are transient, but do need some GC support
Rps_LexTokenZone::Rps_LexTokenZone(Rps_TokenSource* tsrc, Rps_ObjectRef kindob, Rps_Value val, const Rps_String*filestringp, int line, int col)
: Rps_LazyHashedZoneValue(Rps_Type::LexToken),
lex_kind(kindob),
lex_val(val),
lex_file(filestringp),
lex_src(tsrc),
lex_lineno(line),
lex_colno(col),
lex_serial(0)
{
RPS_ASSERT (!kindob || kindob->stored_type() == Rps_Type::Object);
RPS_ASSERT (!filestringp || filestringp->stored_type() == Rps_Type::String);
} // end Rps_LexTokenZone::Rps_LexTokenZone
void
Rps_LexTokenZone::set_serial(unsigned serial)
{
if (serial==0) return;
RPS_ASSERT (lex_serial==0);
lex_serial = serial;
} // end Rps_LexTokenZone::set_serial
Rps_LexTokenZone::~Rps_LexTokenZone()
{
lex_kind = nullptr;
lex_val = nullptr;
lex_src = nullptr;
} // end Rps_LexTokenZone::~Rps_LexTokenZone
Rps_HashInt
Rps_LexTokenZone::compute_hash(void) const
{
auto hkind = lex_kind?(lex_kind->val_hash()):0;
auto hval = lex_val?(lex_val.valhash()):0;
auto hfil = lex_file?(lex_file->val_hash()):0;
// all the constants below are primes
uint64_t h1 = (hkind * 45887) ^ (hval * 75937);
uint64_t h2 = (hfil * 85817) + (lex_lineno * 85931 - lex_colno * 8573);
Rps_HashInt h = (Rps_HashInt)(h1 + h2);
if (!h)
h = (h1&0xffff) + (h2&0xfffff) + 17;
RPS_ASSERT(h != 0);
return h;
} // end Rps_LexTokenZone
Rps_ObjectRef
Rps_LexTokenZone::the_lexical_token_class(void)
{
return RPS_ROOT_OB(_0S6DQvp3Gop015zXhL);
} // end Rps_LexTokenZone::the_lexical_token_class
Rps_ObjectRef
Rps_LexTokenZone::compute_class(Rps_CallFrame*callframe) const
{
// we need to create some lexical_token class object...
RPS_ASSERT(callframe == nullptr || callframe->is_good_call_frame());
return the_lexical_token_class();
} // end Rps_LexTokenZone::compute_class
void
Rps_LexTokenZone::gc_mark(Rps_GarbageCollector&gc, unsigned depth) const
{
if (is_gcmarked(gc))
return;
if (RPS_UNLIKELY(depth > Rps_Value::max_gc_mark_depth))
throw std::runtime_error("too deep Rps_LexTokenZone::gc_mark");
if (lex_kind)
lex_kind->gc_mark(gc,depth+1);
if (lex_val)
lex_val.gc_mark(gc,depth+1);
if (lex_file)
lex_file->gc_mark(gc,depth+1);
} // end Rps_LexTokenZone::gc_mark
void
Rps_LexTokenZone::dump_scan(Rps_Dumper*du, unsigned int) const
{
RPS_ASSERT(du != nullptr);
} // end Rps_LexTokenZone::dump_scan
Json::Value
Rps_LexTokenZone::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
return Json::Value (Json::nullValue);
} // end Rps_LexTokenZone::dump_json
void
Rps_LexTokenZone::val_output(std::ostream&out, unsigned depth, unsigned maxdepth) const
{
if (depth > maxdepth)
{
out << "??";
return;
}
bool showchunk = false;
if (lex_serial>0)
out << "LexToken#" << lex_serial << "{";
else
out << "LexToken{";
if (lex_kind == RPS_ROOT_OB(_36I1BY2NetN03WjrOv)) // symbol∈class
out<<"°symbol";
else if (lex_kind == RPS_ROOT_OB(_2A2mrPpR3Qf03p6o5b)) // int∈class
out<<"°int";
else if (lex_kind == RPS_ROOT_OB(_62LTwxwKpQ802SsmjE)) //string∈class
out<<"°string";
else if (lex_kind == RPS_ROOT_OB(_98sc8kSOXV003i86w5)) // double∈class
out<<"°double";
else if (lex_kind == RPS_ROOT_OB(_3rXxMck40kz03RxRLM)) // code_chunk∈class
{
out<<"°code_chunk";
if (depth==0)
showchunk = true;
}
else if (lex_kind == RPS_ROOT_OB(_5yhJGgxLwLp00X0xEQ)) // object∈class
out<<"°object";
else if (lex_kind == RPS_ROOT_OB(_2wdmxJecnFZ02VGGFK)) //repl_delimiter∈class
out<<"°delim";
else if (!lex_kind)
out<<"°°nul°°";
else
out<<"kind:" << lex_kind;
if (depth > Rps_Value::max_output_depth || depth > maxdepth)
{
out << "…}";
return;
};
out << ", val=";
lex_val.output(out, depth+1,maxdepth);
if (lex_val.is_object() && depth<=1)
{
Rps_ObjectRef obr = lex_val.as_object();
if (obr)
{
std::unique_lock<std::recursive_mutex> guobr (*obr->objmtxptr());
Rps_Value vname = obr->get_physical_attr(RPS_ROOT_OB(_1EBVGSfW2m200z18rx)); //name∈named_attribute
if (auto paylvect = obr->get_dynamic_payload<Rps_PayloadVectVal>())
{
unsigned vsiz = paylvect->size();
out << "⟪";
for (unsigned ix=0; ix<vsiz; ix++)
{
if (ix>0 && ix % 4==0)
{
out << ",";
out << std::endl;
for (unsigned k=depth; k>0; k--) out << " ";
}
else if (ix>0) out << ", ";
out << "["<< ix << "]:";
paylvect->at(ix).output(out, depth+1, maxdepth);
}
out << "⟫";
}
else if (auto paylsymb = obr->get_dynamic_payload<Rps_PayloadSymbol>())
{
out << "symb:" << paylsymb->symbol_name();
}
else if (auto paylclass = obr->get_dynamic_payload<Rps_PayloadClassInfo>())
{
out << "class:" << paylclass->class_name_str();
}
else if (vname.is_string())
{
out << "°named" << Rps_QuotedC_String(vname.to_string()->cppstring());
}
}
}
if (showchunk)
{
/***
* a code chunk is a transient object of class
* RPS_ROOT_OB(_3rXxMck40kz03RxRLM), code_chunk∈class and of
* payload a vector
***/
Rps_ObjectRef obr = lex_val.as_object();
if (obr)
{
std::unique_lock<std::recursive_mutex> guobr (*obr->objmtxptr());
if (auto paylvect = obr->get_dynamic_payload<Rps_PayloadVectVal>())
{
unsigned vsiz = paylvect->size();
out << "#{";
for (unsigned ix=0; ix<vsiz; ix++)
{
if (ix>0 && ix % 4==0)
{
out << ",";
out << std::endl;
for (unsigned k=depth; k>0; k--) out << " ";
}
else if (ix>0) out << ", ";
out << "["<< ix << "]:";
paylvect->at(ix).output(out, depth+1, maxdepth);
}
out << "}#";
}
}
else
RPS_WARNOUT("Rps_LexTokenZone::val_output code chunk without object"
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_LexTokenZone::val_output/codechunk?"));
}
if (lex_file)
{
out << ", @" << lex_file->cppstring()
<< ":" << lex_lineno << ":" << lex_colno;
};
out << "}";
} // end Rps_LexTokenZone::val_output
bool
Rps_LexTokenZone::equal(Rps_ZoneValue const&zv) const
{
if (zv.stored_type() == Rps_Type::LexToken)
{
auto othlt = reinterpret_cast<const Rps_LexTokenZone*>(&zv);
auto lh = lazy_hash();
auto othlh = othlt->lazy_hash();
if (lh != 0 && othlh != 0 && lh != othlh)
return false;
if (lex_file && othlt->lex_file)
{
if (lex_lineno != othlt->lex_lineno)
return false;
if (lex_colno != othlt->lex_colno)
return false;
};
if (lex_kind != othlt->lex_kind)
return false;
if (!lex_file && !othlt->lex_file)
return lex_val == (othlt->lex_val);
return lex_val == othlt->lex_val && lex_file == othlt->lex_file;
}
return false;
} // end Rps_LexTokenZone::equal
bool
Rps_LexTokenZone::less(Rps_ZoneValue const&zv) const
{
if (zv.stored_type() == Rps_Type::LexToken)
{
if (equal(zv))
return false;
auto othlt = reinterpret_cast<const Rps_LexTokenZone*>(&zv);
if (!lex_file && othlt->lex_file)
return true;
if (!othlt->lex_file)
return false;
if (*lex_file < *othlt->lex_file)
return true;
else if (*lex_file > *othlt->lex_file)
return false;
if (lex_lineno < othlt->lex_lineno)
return true;
else if (lex_lineno > othlt->lex_lineno)
return false;
if (lex_colno < othlt->lex_colno)
return true;
else if (lex_colno > othlt->lex_colno)
return false;
if (lex_kind < othlt->lex_kind)
return true;
else if (lex_kind > othlt->lex_kind)
return false;
if (lex_val < othlt->lex_val)
return true;
else if (lex_val > othlt->lex_val)
return false;
RPS_FATALOUT("Rps_LexTokenZone::less corruption: this=" << *this
<< ", other=" << *othlt);
}
else
return Rps_Type::LexToken < zv.stored_type();
} // end Rps_LexTokenZone::less
////////////////////////////////////////////////////////////////
/// TODO: in commit 5a785d82c41da of May 16, 2024 all the builtin
/// commands are similarily implemented, the hope is this handwritten
/// C++ code to be soon and easily generated from new RefPerSys
/// objects....
void
rps_repl_builtin_shell_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
RPS_INFORMOUT(std::endl << "running shell command " << Rps_SingleQuotedC_String(intoksrc.curcptr()));
fflush(nullptr);
int ret = system(intoksrc.curcptr());
if (ret == 0)
RPS_INFORMOUT("successful shell command " << Rps_SingleQuotedC_String(intoksrc.curcptr()));
else
RPS_WARNOUT("failed shell command " << Rps_SingleQuotedC_String(intoksrc.curcptr()) << " exited " << ret);
} // end rps_repl_builtin_shell_command
void
rps_repl_builtin_sh_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
rps_repl_builtin_shell_command(callframe, obenvarg, builtincmd, intoksrc, title);
} // end rps_repl_builtin_sh_command
void
rps_repl_builtin_pwd_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
char cwdbuf[rps_path_byte_size+4];
memset(cwdbuf, 0, sizeof(cwdbuf));
char*cwd = getcwd(cwdbuf, rps_path_byte_size);
if (cwd)
RPS_INFORMOUT(std::endl << "working directory is " << cwdbuf);
else
RPS_WARNOUT("failed getcwd " << strerror(errno));
fflush(nullptr);
} // end rps_repl_builtin_pwd_command
void
rps_repl_builtin_cd_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
char cwdbuf[rps_path_byte_size+4];
memset(cwdbuf, 0, sizeof(cwdbuf));
wordexp_t wx;
memset(&wx, 0, sizeof(wx));
char*cwd = getcwd(cwdbuf, rps_path_byte_size);
if (cwd)
RPS_INFORMOUT(std::endl << "old working directory is " << cwdbuf);
else
RPS_WARNOUT("failed getcwd " << strerror(errno));
const char*cp = intoksrc.curcptr();
while (cp && isspace(*cp))
cp++;
if (!cp)
RPS_WARNOUT("no path given to !cd builtin");
else
{
int failexp = wordexp(cp, &wx, WRDE_SHOWERR|WRDE_UNDEF);
if (failexp)
RPS_WARNOUT("!cd builtin failed to expand " << cp
<< ((failexp==WRDE_BADCHAR)?": bad char"
: (failexp==WRDE_BADVAL)?": bad shell var"
: (failexp==WRDE_NOSPACE)?": out of memory"
: (failexp==WRDE_SYNTAX)?": shell syntax error"
: ": other wordexp failure"));
if (wx.we_wordc == 0)
RPS_WARNOUT("!cd builtin cannot expand " << cp);
else if (wx.we_wordc > 1)
RPS_WARNOUT("!cd builtin ambiguous expand " << cp << " to "
<< wx.we_wordv[0] << " and " << wx.we_wordv[1]
<< ((wx.we_wordc > 2)?" etc.":""));
else
{
if (chdir(wx.we_wordv[0]))
{
const char*err = strerror(errno);
RPS_WARNOUT("!cd " << cp << " failed to chdir to " << wx.we_wordv[0] << " :" << err);
}
else
{
char*cwd = getcwd(cwdbuf, rps_path_byte_size);
if (cwd)
RPS_INFORMOUT(std::endl << "!cd " << cp << " changed to new working directory " << cwdbuf);
else
RPS_WARNOUT("failed getcwd " << strerror(errno));
}
};
wordfree(&wx);
}
} // end rps_repl_builtin_cd_command
void
rps_repl_builtin_pid_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
RPS_INFORMOUT(std::endl << "process id is " << (int)getpid() << " on " << rps_hostname());
fflush(nullptr);
} // end rps_repl_builtin_pid_command
void
rps_repl_builtin_getpid_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
rps_repl_builtin_pid_command(callframe, obenvarg, builtincmd, intoksrc, title);
} // end rps_repl_builtin_getpid_command
void
rps_repl_builtin_git_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
RPS_INFORMOUT(std::endl << "gitid:" << rps_gitid
<< std::endl << "topdir:" << rps_topdirectory);
fflush(nullptr);
} // end rps_repl_builtin_git_command
void
rps_repl_builtin_pmap_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
char pmapbuf[64];
memset (pmapbuf, 0, sizeof(pmapbuf));
fflush(nullptr);
snprintf(pmapbuf, sizeof(pmapbuf)-2, "/usr/bin/pmap %d", (int)getpid());
if (system(pmapbuf))
RPS_WARNOUT("failed running " << pmapbuf << ":" << strerror(errno));
} // end rps_repl_builtin_pmap_command
void
rps_repl_builtin_pfd_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
char pfdbuf[64];
char pathbuf[rps_path_byte_size];
char entbuf[rps_path_byte_size];
std::map<int, std::string> fdmap;
memset (pfdbuf, 0, sizeof(pfdbuf));
memset (entbuf, 0, sizeof(entbuf));
memset (pathbuf, 0, sizeof(pathbuf));
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
snprintf (pfdbuf, sizeof(pfdbuf), "/proc/%d/fd/", (int)getpid());
DIR* pfdir = opendir(pfdbuf);
struct dirent* de= nullptr;
if (!pfdir)
{
RPS_WARNOUT("failed opendir " << pfdbuf << ":" << strerror(errno));
return;
};
RPS_POSSIBLE_BREAKPOINT();
for (de=readdir(pfdir); de!=nullptr; de=readdir(pfdir))
{
if (!isdigit(de->d_name[0]))
continue;
int fdnum = atoi(de->d_name);
if (de->d_type == DT_LNK)
{
memset (pathbuf, 0, sizeof(pathbuf));
snprintf(pathbuf, sizeof(pathbuf), "/proc/%d/fd/%d", (int)getpid(), fdnum);
if (strlen(pathbuf) >= sizeof(pathbuf)-2)
continue;
if (readlink(pathbuf, entbuf, sizeof(entbuf))<0)
continue;
std::string entstr;
entstr.assign(entbuf);
fdmap.insert({fdnum,entstr});
};
}
closedir(pfdir), pfdir = nullptr;
RPS_POSSIBLE_BREAKPOINT();
RPS_INFORMOUT(std::endl << fdmap.size() << " file descriptors:");
for (auto it: fdmap)
{
RPS_POSSIBLE_BREAKPOINT();
std::cout << it.first << ":" << it.second << std::endl;
}
RPS_POSSIBLE_BREAKPOINT();
} // end rps_repl_builtin_pfd_command
void
rps_repl_builtin_time_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
RPS_INFORMOUT(std::endl << "elapsed time (seconds): " << rps_elapsed_real_time()
<< ", cpu time: " << rps_process_cpu_time());
} // end rps_repl_builtin_time_command
void
rps_repl_builtin_gc_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,
Rps_TokenSource& intoksrc,
const char*title)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
/*callerframe:*/callframe,
Rps_ObjectRef obenv;
);
_f.obenv = obenvarg;
std::function<void(Rps_GarbageCollector*)> markall = [&](Rps_GarbageCollector*gc)
{
for (Rps_CallFrame* cf = &_; cf != nullptr; cf = cf->previous_call_frame())
cf->gc_mark_frame(gc);
};
rps_garbage_collect(&markall);
} // end rps_repl_builtin_gc_command
void
rps_repl_builtin_typeinfo_command(Rps_CallFrame*callframe, Rps_ObjectRef obenvarg, const char*builtincmd,