forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdrepl_rps.cc
More file actions
2017 lines (1922 loc) · 82 KB
/
Copy pathcmdrepl_rps.cc
File metadata and controls
2017 lines (1922 loc) · 82 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 cmdrepl_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It implements the Read-Eval-Print-Loop commands in relation to
* repl_rps.cc file.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2021 - 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"
extern "C" const char rps_cmdrepl_gitid[];
const char rps_repl_gitid[]= RPS_GITID;
extern "C" const char rps_cmdrepl_date[];
const char rps_cmdrepl_date[]= __DATE__;
extern "C" const char rps_cmdrepl_shortgitid[];
const char rps_repl_shortgitid[]= RPS_SHORTGITID;
// internal code to evaluate composite expressions like arithmetic, conditionals, etc...
static Rps_TwoValues
rps_full_evaluate_repl_composite_object(Rps_CallFrame*callframe, unsigned long count, Rps_ObjectRef exprobarg, Rps_ObjectRef envobarg, unsigned depth=0);
/// FIXME: declare rps_full_evaluate_repl_instance
#warning should declare rps_full_evaluate_repl_instance
//// TODO: define some cmdrepl_rps.cc local conventions for local frames so a future
//// RPS_REPLEVAL_LOCALFRAME macro which defines and initialize exprv and envob
#warning we could need some RPS_REPLEVAL_LOCALFRAME macro local to this source file.
/// Evaluate for the REPL machinery in given callframe the expression
/// `expr` in the environment given by `envob`; should give two values
/// which should not be both null. This routine might be called in a
/// non-REPL thread by agenda....
Rps_TwoValues
rps_full_evaluate_repl_expr(Rps_CallFrame*callframe, Rps_Value exprarg, Rps_ObjectRef envobarg)
{
#define TEMPORARY_CODE 1
RPS_ASSERT_CALLFRAME (callframe);
RPS_ASSERT(envobarg);
constexpr int maxloop=256;
int framdepth = callframe->call_frame_depth();
unsigned startdbgflags = rps_debug_flags.load();
static std::atomic<unsigned long> eval_repl_counter_;
const unsigned long eval_number = 1+eval_repl_counter_.fetch_add(1);
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
callframe,
Rps_Value closv;
Rps_Value exprv;
Rps_ObjectRef evalob;
Rps_ObjectRef envob;
Rps_ObjectRef nextenvob;
Rps_ObjectRef firstenvob;
Rps_ObjectRef classob;
Rps_Value mainresv;
Rps_Value extraresv;
);
_f.closv = _.call_frame_closure();
_f.exprv = exprarg;
_f.envob = envobarg;
_f.nextenvob = nullptr;
_f.firstenvob = envobarg;
/// macros to ease debugging
#define RPS_REPLEVAL_GIVES_BOTH_AT(V1,V2,LIN) do { \
_f.mainresv = (V1); \
_f.extraresv = (V2); \
RPS_DEBUG_LOG_AT(__FILE__,LIN,REPL, \
__FUNCTION__ << "#" \
<< eval_number << " of expr:" << _f.exprv \
<< " in envob:" << _f.envob \
<< " gives main:" << _f.mainresv \
<< ", extra:" << _f.extraresv); \
return Rps_TwoValues(_f.mainresv,_f.extraresv); \
} while(0)
#define RPS_REPLEVAL_GIVES_BOTH(V1,V2) RPS_REPLEVAL_GIVES_BOTH_AT((V1),(V2),__LINE__)
///
#define RPS_REPLEVAL_GIVES_PLAIN_AT(V1,LIN) do { \
_f.mainresv = (V1); \
_f.extraresv = nullptr; \
RPS_DEBUG_LOG_AT(__FILE__,LIN,REPL, \
__FUNCTION__ << "#" \
<< eval_number << " of expr:" \
<< _f.exprv \
<< " in envob:" << _f.envob \
<< " gives main:" << _f.mainresv \
<< ", extra:" << _f.extraresv); \
return Rps_TwoValues(_f.mainresv,_f.extraresv); \
} while(0)
///
#define RPS_REPLEVAL_GIVES_PLAIN(V) RPS_REPLEVAL_GIVES_PLAIN_AT((V),__LINE__)
///
#define RPS_REPLEVAL_FAIL_AT(MSG,LOG,LIN) do { \
RPS_DEBUG_LOG_AT(__FILE__,LIN,REPL, \
__FUNCTION__ << "#" \
<< eval_number << " FAILS for expr:" \
<< _f.exprv \
<< " in envob:" << _f.envob \
<< "::" << (MSG) \
<< "; " << LOG); \
throw std::runtime_error("rps_full_evaluate_repl_expr " \
" fail " #MSG "@" #LIN); } \
while(0)
///
///
#define RPS_REPLEVAL_FAIL(MSG,LOG) RPS_REPLEVAL_FAIL_AT(MSG,LOG,__LINE__)
///
///
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#"
<< eval_number << " *STARTEVAL*"
<< " expr:" << _f.exprv
<< " in env:" << _f.envob << " framdepth=" << framdepth);
/// to check the above failure macro:
if (!_f.envob || _f.envob->stored_type() != Rps_Type::Object)
{
// This don't happen in practice, but tests that
// RPS_REPLEVAL_FAIL macro is good enough...
RPS_REPLEVAL_FAIL("*check-fail*","never happens no envob"
<< _f.envob);
};
std::lock_guard gu(*_f.envob->objmtxptr());
if (!_f.envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
{
RPS_REPLEVAL_FAIL("bad environment","The envob " << _f.envob << " of class "
<< _f.envob->get_class() << " is not a valid environment");
};
auto envpayl = _f.envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!envpayl)
{
RPS_REPLEVAL_FAIL("bad environment payload","The envob " << _f.envob << " of class "
<< _f.envob->get_class() << " without environment payload");
};
/* environments should have bindings, probably with Rps_PayloadEnvironment */
#warning rps_full_evaluate_repl_expr should check that envob is an environment, with bindings and optional parent env....
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#"
<< eval_number << " *STARTEVAL*"
<< " expr:" << _f.exprv
<< " in env:" << _f.envob);
/* we try to put common cases first... */
RPS_POSSIBLE_BREAKPOINT();
if (_f.exprv.is_int())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_double())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_string())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_tuple())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_set())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_closure())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_empty())
/// return a secondary value to avoid "failure"
RPS_REPLEVAL_GIVES_BOTH(nullptr,
RPS_ROOT_OB(_2i66FFjmS7n03HNNBx)); //space∈class
else if (_f.exprv.is_json())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_lextoken())
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
else if (_f.exprv.is_instance())
{
_f.classob = _f.exprv.compute_class(&_);
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " instance expr:" << _f.exprv
<< " of class:" << _f.classob
<< " in env:" << _f.envob);
#warning TODO: should probably define and call a rps_full_evaluate_repl_instance
RPS_FATALOUT("rps_full_evaluate_repl_expr#" << eval_number
<< " UNIMPLEMENTED instance expr:" << _f.exprv
<< " of class:" << _f.classob
<< " in env:" << _f.envob);
}
else if (_f.exprv.is_object())
{
_f.evalob = _f.exprv.as_object();
std::lock_guard<std::recursive_mutex> gu(*_f.evalob->objmtxptr());
_f.classob = _f.exprv.compute_class(&_);
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object expr:" << _f.exprv
<< " of class:" << _f.classob << " physicalclass:" << _f.evalob->get_class()
<< " in env:" << _f.envob);
};
///
RPS_ASSERT(_f.classob && _f.classob->is_class());
RPS_POSSIBLE_BREAKPOINT();
/****
* Evaluation of variables - perhaps anonymous ones
****/
if (_f.classob == RPS_ROOT_OB(_4HJvNCh35Lu00n5z3R) //variable∈class
|| _f.classob->is_subclass_of(RPS_ROOT_OB(_4HJvNCh35Lu00n5z3R) //variable∈class
))
{
int count=0;
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object expr:" << _f.exprv
<< " is variable envob:" <<_f.envob);
while (count++ < maxloop && _f.envob)
{
_f.nextenvob = nullptr;
std::lock_guard gu(*_f.envob->objmtxptr());
RPS_POSSIBLE_BREAKPOINT();
auto paylenv = _f.envob->get_dynamic_payload<Rps_PayloadEnvironment>();
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object expr:" << _f.exprv << std::endl
<< " evalob=" << _f.evalob
<< ", loopcount:" << count
<< " envob=" << _f.envob << ' '
<< ((paylenv != nullptr)?"*env*":"*NOTENV*")
<< ", firstenvob=" << _f.firstenvob);
if (paylenv)
{
bool missing = false;
RPS_POSSIBLE_BREAKPOINT();
_f.mainresv = paylenv->get_obmap(_f.evalob,/*defaultval:*/nullptr,&missing);
if (!missing)
{
RPS_REPLEVAL_GIVES_PLAIN(_f.mainresv);
}
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object expr:" << _f.exprv << " missing in envob=" << _f.envob
<< ", firstenvob=" << _f.firstenvob
<< " loopcount:" << count);
_f.nextenvob = paylenv->get_parent_environment();
}
else // envob without Rps_PayloadEnvironment
RPS_REPLEVAL_FAIL("bad environment","The envob " << _f.envob << " of class "
<< _f.envob->get_class() << " has no payload environment;"
<< " first env was " <<_f.firstenvob
<< " evaluating variable " << _f.exprv);
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object variable:" << _f.evalob << " ending loop count#" << count
<< " is variable envob:" <<_f.envob << " firstenvob:" << _f.firstenvob
<< " envob=" << _f.envob << " nextenvob=" << _f.nextenvob);
_f.envob = _f.nextenvob;
RPS_POSSIBLE_BREAKPOINT();
} // end while count... loop for variable
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object VARIABLE expr:" << _f.evalob << " exprv:" << _f.exprv
<< " unbound in envob=" << _f.envob << " firstenvob=" << _f.firstenvob << " count#" << count << std::endl
<< RPS_FULL_BACKTRACE_HERE(1,"rps_full_evaluate_repl_expr unboundvar"));
RPS_POSSIBLE_BREAKPOINT();
RPS_REPLEVAL_FAIL("unbound variable","Variable " << _f.evalob << " unbound with envob " << _f.envob << " of class "
<< _f.envob->get_class()
<< " first env was " <<_f.firstenvob);
}
/****
* Evaluation of symbolic variables - named ones
****/
else if (_f.classob == RPS_ROOT_OB(_4Si5RBkg1Qm0285SD0) //symbolic_variable∈class
|| _f.classob->is_subclass_of(RPS_ROOT_OB(_4Si5RBkg1Qm0285SD0) //symbolic_variable∈class
))
{
int count=0;
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object expr:" << _f.exprv
<< " is symbolic_variable envob:" <<_f.envob);
RPS_POSSIBLE_BREAKPOINT();
while (count++ < maxloop && _f.envob)
{
_f.nextenvob = nullptr;
std::lock_guard gu(*_f.envob->objmtxptr());
auto paylenv = _f.envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (paylenv)
{
bool missing = false;
_f.mainresv = paylenv->get_obmap(_f.evalob,nullptr,&missing);
if (!missing)
{
RPS_REPLEVAL_GIVES_PLAIN(_f.mainresv);
}
_f.nextenvob = paylenv->get_parent_environment();
}
else // envob without Rps_PayloadEnvironment
RPS_REPLEVAL_FAIL("bad environment","The envob " << _f.envob << " of class "
<< _f.envob->get_class() << " has no payload environment;"
<< " first env was " <<_f.firstenvob
<< " evaluating symbolic variable " << _f.exprv);
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object variable:" << _f.evalob << " ending loop count#" << count
<< " is symbolic_variable envob:" <<_f.envob << " firstenvob:" << _f.firstenvob
<< " nextenvob:" << _f.nextenvob);
RPS_POSSIBLE_BREAKPOINT();
_f.envob = _f.nextenvob;
}; // end while count<... symbvar
RPS_REPLEVAL_FAIL("unbound symbolic variable","Symbolic variable " << _f.evalob
<< " unbound with envob " << _f.envob << " of class "
<< _f.envob->get_class()
<< " first env was " <<_f.firstenvob);
}
#warning TODO: fixme evaluation of various repl_expression-s e.g. conditional, arithmetic, application
else if (_f.classob == RPS_ROOT_OB(_1jJaY1usnpR02WUvSX) //repl_expression∈class
|| _f.classob->is_subclass_of(RPS_ROOT_OB(_1jJaY1usnpR02WUvSX) //repl_expression∈class
))
{
RPS_POSSIBLE_BREAKPOINT();
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " repl_expression:" << _f.evalob
<< " of class " << _f.classob
<< " in envob:" <<_f.envob << " firstenvob:" << _f.firstenvob);
Rps_TwoValues two = rps_full_evaluate_repl_composite_object(&_, eval_number, _f.evalob, _f.envob, 0);
RPS_REPLEVAL_GIVES_BOTH(two.main_val, two.xtra_val);
}
else
{
// any other object is self evaluating! or NOT?
// TODO: think more.
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_expr#" << eval_number
<< " object:" << _f.evalob << " of class " << _f.classob << " is selfevaluating in envob:" <<_f.envob << " firstenvob:" << _f.firstenvob);
RPS_POSSIBLE_BREAKPOINT();
RPS_REPLEVAL_GIVES_PLAIN(_f.exprv);
}
} // end rps_full_evaluate_repl_expr
Rps_TwoValues
rps_full_evaluate_repl_composite_object(Rps_CallFrame*callframe, unsigned long count, Rps_ObjectRef exprobarg, Rps_ObjectRef envobarg, unsigned depth)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
callframe,
Rps_ObjectRef exprob;
Rps_ObjectRef envob;
Rps_Value mainresv;
Rps_Value otheresv;
);
_f.exprob = exprobarg;
_f.envob = envobarg;
RPS_DEBUG_LOG(REPL, "rps_full_evaluate_repl_composite_object#" << count <<" START exprob:" << _f.exprob << " envob:" << _f.envob
<< " depth:" << depth << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_full_evaluate_repl_composite_object"));
#warning unimplemented rps_full_evaluate_repl_composite_object
RPS_FATALOUT("rps_full_evaluate_repl_composite_object#" << count <<" UNIMPLEMENTED exprob:" << _f.exprob << " envob:" << _f.envob << " depth:" << depth);
} // end rps_full_evaluate_repl_composite_object
/// forget our macros
#undef RPS_REPLEVAL_GIVES_BOTH_AT
#undef RPS_REPLEVAL_GIVES_BOTH
#undef RPS_REPLEVAL_GIVES_PLAIN_AT
#undef RPS_REPLEVAL_GIVES_PLAIN
Rps_Value
rps_simple_evaluate_repl_expr(Rps_CallFrame*callframe, Rps_Value expr, Rps_ObjectRef envob)
{
RPS_ASSERT_CALLFRAME (callframe);
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
callframe,
Rps_Value exprv;
Rps_ObjectRef envob;
Rps_Value mainresv;
Rps_Value otheresv;
);
_f.exprv = expr;
_f.envob = envob;
RPS_DEBUG_LOG(REPL, "rps_simple_evaluate_repl_expr START expr:" << _f.exprv << " envob:" << _f.envob << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_simple_evaluate_repl_expr"));
{
Rps_TwoValues two = rps_full_evaluate_repl_expr(&_,_f.exprv,_f.envob);
_f.mainresv = two.main();
_f.otheresv = two.xtra();
}
return _f.mainresv;
} // end rps_simple_evaluate_repl_expr
////////////////
Rps_PayloadEnvironment::Rps_PayloadEnvironment(Rps_ObjectZone*obown) :
Rps_PayloadObjMap(obown),
env_parent(nullptr)
{
} // end Rps_PayloadEnvironment::Rps_PayloadEnvironment
Rps_ObjectZone*
Rps_PayloadEnvironment::make(Rps_CallFrame*callframe, Rps_ObjectRef classob, Rps_ObjectRef spaceob)
{
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED, callframe,
Rps_ObjectRef obclass;
Rps_ObjectRef obspace;
Rps_ObjectRef obenv;
);
_f.obclass = classob;
_f.obspace = spaceob;
RPS_ASSERT(classob);
RPS_ASSERT(classob == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a) //environment∈class
|| classob->is_subclass_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)));
_f.obenv = Rps_ObjectRef::make_object(&_, _f.obclass, _f.obspace);
auto paylenv = _f.obenv->put_new_plain_payload<Rps_PayloadEnvironment>();
RPS_ASSERT(paylenv);
return _f.obenv;
} // end Rps_PayloadEnvironment::make
Rps_ObjectZone*
Rps_PayloadEnvironment::make_with_parent_environment(Rps_CallFrame*callframe, Rps_ObjectRef parentob, Rps_ObjectRef classob, Rps_ObjectRef spaceob)
{
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED, callframe,
Rps_ObjectRef obparent;
Rps_ObjectRef obclass;
Rps_ObjectRef obspace;
Rps_ObjectRef obenv;
);
_f.obclass = classob;
_f.obparent = parentob;
_f.obspace = spaceob;
RPS_ASSERT(classob);
RPS_ASSERT(classob == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a) //environment∈class
|| classob->is_subclass_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)));
RPS_ASSERT(!parentob || parentob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)));
_f.obenv = Rps_ObjectRef::make_object(&_, _f.obclass, _f.obspace);
auto paylenv = _f.obenv->put_new_plain_payload<Rps_PayloadEnvironment>();
RPS_ASSERT(paylenv);
paylenv->env_parent = parentob;
return _f.obenv;
} // end Rps_PayloadEnvironment::make_with_parent_environment
Rps_Value
rps_environment_get_shallow_bound_value(Rps_ObjectRef envob, Rps_ObjectRef varob,
bool *pmissing)
{
if (envob.is_empty())
{
if (pmissing)
*pmissing = true;
return nullptr;
}
std::lock_guard gu(*envob->objmtxptr());
bool isgoodenv = false;
if (envob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
{
if (pmissing)
*pmissing=true;
return nullptr;
};
auto paylenv = envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
{
if (pmissing)
*pmissing = true;
return nullptr;
};
return paylenv->get_obmap(varob, nullptr, pmissing);
} // end rps_environment_get_shallow_bound_value
constexpr int rps_environment_maxloop = 4096;
int
rps_environment_find_binding_depth(Rps_ObjectRef envob, Rps_ObjectRef varob)
{
int depth=0;
int loopcnt = 0;
Rps_ObjectRef firstenvob = envob;
for(;;)
{
if (loopcnt++ > rps_environment_maxloop)
{
// this should never happen in practice....
RPS_WARNOUT("rps_environment_find_binding_depth looping "
<< loopcnt << " times for initial environment " << envob << " and variable " << varob
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "rps_environment_find_binding_depth"));
return -1;
}
if (envob.is_empty())
{
return -1;
};
std::lock_guard gu(*envob->objmtxptr());
bool isgoodenv = false;
if (envob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
return -1;
auto paylenv = envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
return -1;
if (paylenv->has_key_obmap(varob))
return depth;
depth++;
envob = paylenv->get_parent_environment();
if (!envob)
return -1;
};
} // end rps_environment_find_binding_depth
Rps_Value
rps_environment_find_bound_value(Rps_ObjectRef envob, Rps_ObjectRef varob,
int*pdepth, Rps_ObjectRef*penvob)
{
int depth=0;
int loopcnt = 0;
Rps_ObjectRef firstenvob = envob;
for(;;)
{
if (loopcnt++ > rps_environment_maxloop)
{
// this should never happen in practice....
RPS_WARNOUT("rps_environment_find_bound_value looping "
<< loopcnt << " times for initial environment " << envob << " and variable " << varob
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "rps_environment_find_binding_depth"));
if (pdepth)
*pdepth = -1;
if (penvob)
*penvob = envob;
return nullptr;
}
if (envob.is_empty())
{
if (pdepth)
*pdepth = -1;
if (penvob)
*penvob = nullptr;
return nullptr;
};
std::lock_guard gu(*envob->objmtxptr());
bool isgoodenv = false;
if (envob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
{
if (pdepth)
*pdepth = -1;
if (penvob)
*penvob = nullptr;
return nullptr;
}
auto paylenv = envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
{
if (pdepth)
*pdepth = -1;
if (penvob)
*penvob = nullptr;
return nullptr;
}
if (Rps_Value v = paylenv->get_obmap(varob))
{
if (pdepth)
*pdepth = depth;
if (penvob)
*penvob = envob;
return v;
}
depth++;
envob = paylenv->get_parent_environment();
if (!envob)
{
if (pdepth)
*pdepth = -1;
if (penvob)
*penvob = nullptr;
return nullptr;
}
};
} // end rps_environment_find_bound_value
void
rps_environment_add_shallow_binding(Rps_CallFrame*callframe,
Rps_ObjectRef envob, Rps_ObjectRef varob, Rps_Value val)
{
RPS_ASSERT(callframe && callframe->is_good_call_frame());
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
callframe,
Rps_ObjectRef envob;
Rps_ObjectRef varob;
Rps_Value valv;
);
_f.envob = envob;
_f.varob = varob;
_f.valv = val;
if (!envob || envob.is_empty())
return;
if (!varob || varob.is_empty())
return;
std::lock_guard gu(*envob->objmtxptr());
bool isgoodenv = false;
if (envob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
return;
auto paylenv = envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
return;
paylenv->put_obmap(_f.varob, _f.valv);
} // end rps_environment_add_shallow_binding
/// overwrite a binding in the deep environment containing it, or when
/// not found in the current one. Return affected depth.
int
rps_environment_overwrite_binding(Rps_CallFrame*callframe,
Rps_ObjectRef envob,
Rps_ObjectRef varob, Rps_Value val,
Rps_ObjectRef*penvob)
{
RPS_LOCALFRAME(RPS_CALL_FRAME_UNDESCRIBED,
callframe,
Rps_ObjectRef envob;
Rps_ObjectRef firstenvob;
Rps_ObjectRef varob;
Rps_Value valv;
);
_f.envob = envob;
_f.firstenvob = envob;
_f.varob = varob;
_f.valv = val;
int loopcnt = 0;
int depth = 0;
for(;;)
{
if (loopcnt++ > rps_environment_maxloop)
{
// this should never happen in practice....
RPS_WARNOUT("rps_environment_overwrite_binding looping "
<< loopcnt << " times for initial environment " << _f.firstenvob << " and variable " << _f.varob << " value " << _f.valv
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "rps_environment_find_binding_depth"));
if (penvob)
*penvob = _f.envob;
return -1;
}
if (_f.envob.is_empty())
{
if (penvob)
*penvob = nullptr;
return -1;
};
std::lock_guard gu(*_f.envob->objmtxptr());
bool isgoodenv = false;
if (_f.envob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (_f.envob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
{
if (penvob)
*penvob = nullptr;
return -1;
}
auto paylenv = envob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
{
if (penvob)
*penvob = nullptr;
return -1;
}
if (Rps_Value v = paylenv->get_obmap(_f.varob))
{
if (penvob)
*penvob = _f.envob;
paylenv->put_obmap(_f.varob, _f.valv);
return depth;
}
depth++;
envob = paylenv->get_parent_environment();
if (!envob)
break;
};
std::lock_guard gu(*_f.firstenvob->objmtxptr());
bool isgoodenv = false;
if (_f.firstenvob->get_class() == RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a)) //environment∈class
isgoodenv = true;
else if (_f.firstenvob->is_instance_of(RPS_ROOT_OB(_5LMLyzRp6kq04AMM8a))) //environment∈class
isgoodenv = true;
if (!isgoodenv)
{
if (penvob)
*penvob = nullptr;
return -1;
};
auto paylenv = _f.firstenvob->get_dynamic_payload<Rps_PayloadEnvironment>();
if (!paylenv)
{
if (penvob)
*penvob = nullptr;
return -1;
}
paylenv->put_obmap(_f.varob, _f.valv);
return 0;
} // end rps_environment_overwrite_binding
void
Rps_PayloadEnvironment::gc_mark(Rps_GarbageCollector&gc) const
{
gc_mark_objmap(gc);
if (env_parent)
gc.mark_obj(env_parent);
} // end Rps_PayloadEnvironment::gc_mark
void
Rps_PayloadEnvironment::dump_scan(Rps_Dumper*du) const
{
RPS_ASSERT(du);
dump_scan_objmap_internal(du);
if (rps_is_dumpable_objref(du, env_parent))
rps_dump_scan_object(du, env_parent);
} // end Rps_PayloadEnvironment::dump_scan
void
Rps_PayloadEnvironment::dump_json_content(Rps_Dumper*du, Json::Value&jv) const
{
RPS_ASSERT(du);
jv["payload"] = "environment";
dump_json_objmap_internal_content(du, jv);
if (rps_is_dumpable_objref(du, env_parent))
jv["parent_env"] = rps_dump_json_objectref(du, env_parent);
else
jv["parent_env"] = Json::nullValue;
} // end Rps_PayloadEnvironment::dump_json_content
void
Rps_PayloadEnvironment::output_payload(std::ostream&out, unsigned depth, unsigned maxdepth) const
{
/// most of the code below is "temporarily" duplicated from
/// Rps_PayloadObjMap::output_payload in file morevalues_rps.cc
/// we hope to later (in 2025?) have this C++ code generated at dump time
RPS_ASSERT(depth <= maxdepth);
bool ontty =
(&out == &std::cout)?isatty(STDOUT_FILENO)
:(&out == &std::cerr)?isatty(STDERR_FILENO)
:false;
if (rps_without_terminal_escape)
ontty = false;
const char* BOLD_esc = (ontty?RPS_TERMINAL_BOLD_ESCAPE:"");
const char* NORM_esc = (ontty?RPS_TERMINAL_NORMAL_ESCAPE:"");
std::lock_guard<std::recursive_mutex> gudispob(*owner()->objmtxptr());
int nbobjmap = (int) get_obmap_size();
if (nbobjmap==0)
out << BOLD_esc << "-empty environment-" << NORM_esc;
else
out << BOLD_esc << "-environment of " << nbobjmap
<< ((nbobjmap>1)?" entries":" entry");
Rps_Value dv = get_descr();
if (dv)
out << " described by " << NORM_esc << Rps_OutputValue(dv, depth, maxdepth) << std::endl;
else
out << " plain" << NORM_esc << std::endl;
std::vector<Rps_ObjectRef> attrvect(nbobjmap);
do_each_obmap_entry<std::vector<Rps_ObjectRef>&>(attrvect,
[&](std::vector<Rps_ObjectRef>&atvec,
Rps_ObjectRef atob,
[[unused]]Rps_Value, [[unused]]void*)
{
atvec.push_back(atob);
return false;
});
rps_sort_object_vector_for_display(attrvect);
for (int ix=0; ix<(int)nbobjmap; ix++)
{
const Rps_ObjectRef curattr = attrvect[ix];
const Rps_Value curval = get_obmap(curattr);
out << BOLD_esc << "*"
<< NORM_esc << curattr << ": "
<< Rps_OutputValue(curval, depth, maxdepth)
<< std::endl;
};
Rps_ObjectRef parenvob = get_parent_environment();
if (!parenvob)
out << BOLD_esc << "- no parent env -" << NORM_esc
<< std::endl;
else
out << BOLD_esc << "- parent env: " << NORM_esc;
out << parenvob << BOLD_esc << "-" << NORM_esc << std::endl;
} // end Rps_PayloadEnvironment::output_payload
void
rpsldpy_environment (Rps_ObjectZone*obz, Rps_Loader*ld, const Json::Value& jv, Rps_Id spacid, unsigned lineno)
{
RPS_ASSERT(obz != nullptr);
RPS_ASSERT(ld != nullptr);
RPS_ASSERT(obz->get_payload() == nullptr);
RPS_ASSERT(jv.type() == Json::objectValue);
auto paylenv = obz->put_new_plain_payload<Rps_PayloadEnvironment>();
const Json::Value& jobmap = jv["objmap"];
const Json::Value& jdescr = jv["descr"];
const Json::Value& jparent = jv["parent_env"];
if (jobmap.type () == Json::objectValue)
{
auto membvec = jobmap.getMemberNames(); // vector of strings
for (const std::string& keystr : membvec)
{
Rps_ObjectRef keyob(keystr, ld);
Rps_Value val = Rps_Value(jobmap[keystr], ld);
paylenv->put_obmap(keyob, val);
}
}
paylenv->put_descr(Rps_Value(jdescr, ld));
if (jparent)
paylenv->env_parent = Rps_ObjectRef(jparent,ld);
} // end rpsldpy_environment
////////////////
void
Rps_CallFrame::interpret_repl_statement(Rps_ObjectRef stmtob,Rps_ObjectRef envob)
{
RPS_FATALOUT("unimplemented Rps_CallFrame::interpret_repl_statement stmtob=" << stmtob << " envob=" << envob);
#warning unimplemented Rps_CallFrame::interpret_repl_statement
} // end Rps_CallFrame::interpret_repl_statement
void rps_interpret_repl_statement(Rps_CallFrame*callframe, Rps_ObjectRef stmtob,Rps_ObjectRef envob)
{
RPS_ASSERT(callframe != nullptr && callframe->is_good_call_frame());
RPS_ASSERT(stmtob);
RPS_ASSERT(envob);
callframe->interpret_repl_statement(stmtob, envob);
} // end rps_interpret_repl_statement
////////////////
Rps_TwoValues
Rps_CallFrame::evaluate_repl_expr(Rps_Value expr, Rps_ObjectRef envob)
{
return rps_full_evaluate_repl_expr(this,expr,envob);
} // end Rps_CallFrame::evaluate_repl_expr
Rps_Value
Rps_CallFrame::evaluate_repl_expr1(Rps_Value expr, Rps_ObjectRef envob)
{
return rps_simple_evaluate_repl_expr(this,expr,envob);
} // end Rps_CallFrame::evaluate_repl_expr1
/* C++ closure _61pgHb5KRq600RLnKD for REPL command dump parsing*/
extern "C" rps_applyingfun_t rpsapply_61pgHb5KRq600RLnKD;
Rps_TwoValues
rpsapply_61pgHb5KRq600RLnKD(Rps_CallFrame*callerframe, // REPL dump command
const Rps_Value arg0,
[[maybe_unused]] const Rps_Value arg1,
[[maybe_unused]] const Rps_Value arg2,
[[maybe_unused]] const Rps_Value arg3,
[[maybe_unused]] const std::vector<Rps_Value>* restargs)
{
//RPS_ASSERT(callerframe && callerframe->is_good_call_frame());
RPS_ASSERT_CALLFRAME (callerframe);
static long callcnt;
callcnt++;
static Rps_Id descoid;
if (!descoid) // this happens only once!
descoid=Rps_Id("_61pgHb5KRq600RLnKD");
RPS_DEBUG_LOG(REPL, "REPL command dump callcnt#" << callcnt
<< " descoid=" << descoid
<< " CALLED from:" << std::endl
<< Rps_ShowCallFrame(callerframe) << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "REPL command dump rpsapply_61pgHb5KRq600RLnKD"));
RPS_LOCALFRAME(/*descr:*/Rps_ObjectRef::really_find_object_by_oid(descoid),
callerframe,
Rps_ObjectRef replcmdob;
Rps_ObjectRef lexkindob;
Rps_Value lexval;
Rps_Value closv;
Rps_Value lextokv;
Rps_Value nextokv;
Rps_ObjectRef lexob;
Rps_ObjectRef nextlexob;
Rps_Value nextlexval;
);
_f.closv = _.call_frame_closure();
RPS_DEBUG_LOG(CMD, "REPL command dump start callcnt#" << callcnt << " arg0=" << arg0
<< "∈" << arg0.compute_class(&_)
<< " arg1=" << arg1
<< "∈" << arg1.compute_class(&_) <<std::endl
<< " arg2=" << arg2 << " arg3=" << arg3 << std::endl
<< " callingclos=" << _f.closv
<< " from " << std::endl
<< Rps_ShowCallFrame(&_)
<< "**calldepth=" << _.call_frame_depth()
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "rpsapply_61pgHb5KRq600RLnKD/REPL cmd dump"));
RPS_DEBUG_LOG(REPL, "REPL command dump start callcnt#" << callcnt << " arg0=" << arg0
<< "∈" << arg0.compute_class(&_)
<< " arg1=" << arg1
<< "∈" << arg1.compute_class(&_) <<std::endl
<< " arg2=" << arg2 << " arg3=" << arg3 << std::endl
<< " callingclos=" << _f.closv
<< " from " << std::endl
<< Rps_ShowCallFrame(&_)
<< "**calldepth=" << _.call_frame_depth()
<< std::endl << RPS_FULL_BACKTRACE_HERE(1, "rpsapply_61pgHb5KRq600RLnKD/REPL cmd dump"));
_f.replcmdob = arg0.to_object();
_f.lextokv = arg1;
RPS_ASSERT(_.call_frame_depth() < 7);
RPS_DEBUG_LOG(CMD, "REPL command dump framedepth=" << _.call_frame_depth()
<< " lextokv=" << _f.lextokv
<<" curframe:"
<< std::endl << Rps_ShowCallFrame(&_)
<< RPS_FULL_BACKTRACE_HERE(1, "REPL command dump rpsapply_61pgHb5KRq600RLnKD"));
const Rps_LexTokenZone* ltokz = _f.lextokv.to_lextoken();
RPS_ASSERT (ltokz != nullptr);
{
Rps_TokenSource*tksrc = ltokz->lxsrc();
RPS_ASSERT (tksrc != nullptr);
_f.nextokv = tksrc->get_token(&_);
RPS_DEBUG_LOG(CMD, "REPL command dump callcnt#" << callcnt << " lexval=" << _f.lexval << " nextokv=" << _f.nextokv
<< " framedepth=" << _.call_frame_depth());
const Rps_LexTokenZone* nextokz = _f.nextokv.to_lextoken();
RPS_ASSERT(nextokz);
_f.nextlexob = nextokz->lxkind();
_f.nextlexval = nextokz->lxval();
RPS_DEBUG_LOG(CMD, "REPL command dump callcnt#" << callcnt << " lexval=" << _f.lexval << " nextokv=" << _f.nextokv
<< " nextlexob=" << _f.nextlexob << " nextlexval=" << _f.nextlexval);
}
std::string dumpdir;
bool dumped=false;
RPS_DEBUG_LOG(CMD, "REPL command dump callcnt#" << callcnt << " lexob=" << _f.lexob << " lextokv=" << _f.lextokv
<< " nextlexob=" << _f.nextlexob << " nextlexval=" << _f.nextlexval
<< " framedepth=" << _.call_frame_depth() << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "REPL command dump rpsapply_61pgHb5KRq600RLnKD /nextlex"));
// Attempt to check if there are no more tokens following
RPS_ASSERT (_f.nextlexval);
const Rps_LexTokenZone* lastokzone = _f.nextlexval.to_lextoken();
if (lastokzone != nullptr)
RPS_FATALOUT("invalid REPL syntax for dump command");
RPS_DEBUG_LOG(CMD, "REPL command dump dot callcnt#" << callcnt
<< " lexob=" << _f.lexob
<< " nextlexob=" << _f.nextlexob
<< " nextlexval=" << _f.nextlexval);
///
if (_f.nextlexval.is_object() && _f.nextlexval.to_object()->oid() == Rps_Id("_78wsBiJhJj1025DIs1")) // the dot "."∈repl_delimiter
{
RPS_DEBUG_LOG(CMD, "REPL command dump dot callcnt#" << callcnt
<< " framedepth=" << _.call_frame_depth());
// dump to current directory
rps_dump_into(".", &_);
dumpdir=".";
dumped = true;
RPS_DEBUG_LOG(CMD, "REPL command dumped callcnt#" << callcnt << " into current directory callcnt#" << callcnt);
return {_f.nextlexval, nullptr};
}
else if (_f.nextlexval.is_string()) //string∈class #
{
std::string dirstr = _f.nextlexval.as_cppstring();
RPS_DEBUG_LOG(CMD, "REPL command dumping into '" << Rps_Cjson_String (dirstr) << "' callcnt#" << callcnt
<< " framedepth=" << _.call_frame_depth());
DIR* dirh = opendir(dirstr.c_str());
if (dirh)
{
closedir(dirh);
RPS_DEBUG_LOG(CMD, "REPL command dumping into existing dir '"
<< Rps_Cjson_String (dirstr) << "' callcnt#" << callcnt);
rps_dump_into(dirstr.c_str(), &_);
dumpdir = dirstr;
dumped = true;
}
else if (!mkdir(dirstr.c_str(), 0750))
{
RPS_DEBUG_LOG(CMD, "REPL command dumping into fresh dir '"
<< Rps_Cjson_String (dirstr) << "' callcnt#" << callcnt);
rps_dump_into(dirstr.c_str(), &_);
dumpdir = dirstr;
dumped = true;
}
else