/
chaiscript_eval.hpp
1494 lines (1273 loc) · 64.7 KB
/
chaiscript_eval.hpp
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
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#ifndef CHAISCRIPT_EVAL_HPP_
#define CHAISCRIPT_EVAL_HPP_
#include <exception>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "../chaiscript_defines.hpp"
#include "../dispatchkit/boxed_cast.hpp"
#include "../dispatchkit/boxed_number.hpp"
#include "../dispatchkit/boxed_value.hpp"
#include "../dispatchkit/dispatchkit.hpp"
#include "../dispatchkit/dynamic_object_detail.hpp"
#include "../dispatchkit/proxy_functions.hpp"
#include "../dispatchkit/proxy_functions_detail.hpp"
#include "../dispatchkit/register_function.hpp"
#include "../dispatchkit/type_info.hpp"
#include "chaiscript_algebraic.hpp"
#include "chaiscript_common.hpp"
namespace chaiscript::exception {
class bad_boxed_cast;
} // namespace chaiscript::exception
namespace chaiscript {
/// \brief Classes and functions that are part of the runtime eval system
namespace eval {
template<typename T>
struct AST_Node_Impl;
template<typename T>
using AST_Node_Impl_Ptr = typename std::unique_ptr<AST_Node_Impl<T>>;
namespace detail {
/// Helper function that will set up the scope around a function call, including handling the named function parameters
template<typename T>
Boxed_Value eval_function(chaiscript::detail::Dispatch_Engine &t_ss,
const AST_Node_Impl<T> &t_node,
const std::vector<std::string> &t_param_names,
const Function_Params &t_vals,
const std::map<std::string, Boxed_Value> *t_locals = nullptr,
bool has_this_capture = false) {
chaiscript::detail::Dispatch_State state(t_ss);
const Boxed_Value *thisobj = [&]() -> const Boxed_Value * {
if (auto &stack = t_ss.get_stack_data(state.stack_holder()).back(); !stack.empty() && stack.back().first == "__this") {
return &stack.back().second;
} else if (!t_vals.empty()) {
return &t_vals[0];
} else {
return nullptr;
}
}();
chaiscript::eval::detail::Stack_Push_Pop tpp(state);
if (thisobj && !has_this_capture) {
state.add_object("this", *thisobj);
}
if (t_locals) {
for (const auto &[name, value] : *t_locals) {
state.add_object(name, value);
}
}
for (size_t i = 0; i < t_param_names.size(); ++i) {
if (t_param_names[i] != "this") {
state.add_object(t_param_names[i], t_vals[i]);
}
}
try {
return t_node.eval(state);
} catch (detail::Return_Value &rv) {
return std::move(rv.retval);
}
}
inline Boxed_Value clone_if_necessary(Boxed_Value incoming, std::atomic_uint_fast32_t &t_loc, const chaiscript::detail::Dispatch_State &t_ss) {
if (!incoming.is_return_value()) {
if (incoming.get_type_info().is_arithmetic()) {
return Boxed_Number::clone(incoming);
} else if (incoming.get_type_info().bare_equal_type_info(typeid(bool))) {
return Boxed_Value(*static_cast<const bool *>(incoming.get_const_ptr()));
} else if (incoming.get_type_info().bare_equal_type_info(typeid(std::string))) {
return Boxed_Value(*static_cast<const std::string *>(incoming.get_const_ptr()));
} else {
std::array<Boxed_Value, 1> params{std::move(incoming)};
return t_ss->call_function("clone", t_loc, Function_Params{params}, t_ss.conversions());
}
} else {
incoming.reset_return_value();
return incoming;
}
}
} // namespace detail
template<typename T>
struct AST_Node_Impl : AST_Node {
AST_Node_Impl(std::string t_ast_node_text,
AST_Node_Type t_id,
Parse_Location t_loc,
std::vector<AST_Node_Impl_Ptr<T>> t_children = std::vector<AST_Node_Impl_Ptr<T>>())
: AST_Node(std::move(t_ast_node_text), t_id, std::move(t_loc))
, children(std::move(t_children)) {
}
static bool get_scoped_bool_condition(const AST_Node_Impl<T> &node, const chaiscript::detail::Dispatch_State &t_ss) {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
return get_bool_condition(node.eval(t_ss), t_ss);
}
std::vector<std::reference_wrapper<AST_Node>> get_children() const final {
std::vector<std::reference_wrapper<AST_Node>> retval;
retval.reserve(children.size());
for (auto &child : children) {
retval.emplace_back(*child);
}
return retval;
}
Boxed_Value eval(const chaiscript::detail::Dispatch_State &t_e) const final {
try {
T::trace(t_e, this);
return eval_internal(t_e);
} catch (exception::eval_error &ee) {
ee.call_stack.push_back(*this);
throw;
}
}
std::vector<AST_Node_Impl_Ptr<T>> children;
protected:
virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const {
throw std::runtime_error("Undispatched ast_node (internal error)");
}
};
template<typename T>
struct Compiled_AST_Node : AST_Node_Impl<T> {
Compiled_AST_Node(AST_Node_Impl_Ptr<T> t_original_node,
std::vector<AST_Node_Impl_Ptr<T>> t_children,
std::function<Boxed_Value(const std::vector<AST_Node_Impl_Ptr<T>> &, const chaiscript::detail::Dispatch_State &t_ss)> t_func)
: AST_Node_Impl<T>(t_original_node->text, AST_Node_Type::Compiled, t_original_node->location, std::move(t_children))
, m_func(std::move(t_func))
, m_original_node(std::move(t_original_node)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { return m_func(this->children, t_ss); }
std::function<Boxed_Value(const std::vector<AST_Node_Impl_Ptr<T>> &, const chaiscript::detail::Dispatch_State &t_ss)> m_func;
AST_Node_Impl_Ptr<T> m_original_node;
};
template<typename T>
struct Fold_Right_Binary_Operator_AST_Node : AST_Node_Impl<T> {
Fold_Right_Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children, Boxed_Value t_rhs)
: AST_Node_Impl<T>(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children))
, m_oper(Operators::to_operator(t_oper))
, m_rhs(std::move(t_rhs)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
return do_oper(t_ss, this->text, this->children[0]->eval(t_ss));
}
protected:
Boxed_Value do_oper(const chaiscript::detail::Dispatch_State &t_ss, const std::string &t_oper_string, const Boxed_Value &t_lhs) const {
try {
if (t_lhs.get_type_info().is_arithmetic()) {
// If it's an arithmetic operation we want to short circuit dispatch
try {
return Boxed_Number::do_oper(m_oper, t_lhs, m_rhs);
} catch (const chaiscript::exception::arithmetic_error &) {
throw;
} catch (...) {
throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
}
} else {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
std::array<Boxed_Value, 2> params{t_lhs, m_rhs};
fpp.save_params(Function_Params{params});
return t_ss->call_function(t_oper_string, m_loc, Function_Params{params}, t_ss.conversions());
}
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, *t_ss);
}
}
private:
Operators::Opers m_oper;
Boxed_Value m_rhs;
mutable std::atomic_uint_fast32_t m_loc = {0};
};
template<typename T>
struct Binary_Operator_AST_Node : AST_Node_Impl<T> {
Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children))
, m_oper(Operators::to_operator(t_oper)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
auto lhs = this->children[0]->eval(t_ss);
auto rhs = this->children[1]->eval(t_ss);
return do_oper(t_ss, m_oper, this->text, lhs, rhs);
}
protected:
Boxed_Value do_oper(const chaiscript::detail::Dispatch_State &t_ss,
Operators::Opers t_oper,
const std::string &t_oper_string,
const Boxed_Value &t_lhs,
const Boxed_Value &t_rhs) const {
try {
if (t_oper != Operators::Opers::invalid && t_lhs.get_type_info().is_arithmetic() && t_rhs.get_type_info().is_arithmetic()) {
// If it's an arithmetic operation we want to short circuit dispatch
try {
return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs);
} catch (const chaiscript::exception::arithmetic_error &) {
throw;
} catch (...) {
throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
}
} else {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
std::array<Boxed_Value, 2> params{t_lhs, t_rhs};
fpp.save_params(Function_Params(params));
return t_ss->call_function(t_oper_string, m_loc, Function_Params(params), t_ss.conversions());
}
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, *t_ss);
}
}
private:
Operators::Opers m_oper;
mutable std::atomic_uint_fast32_t m_loc = {0};
};
template<typename T>
struct Constant_AST_Node final : AST_Node_Impl<T> {
Constant_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, Boxed_Value t_value)
: AST_Node_Impl<T>(t_ast_node_text, AST_Node_Type::Constant, std::move(t_loc))
, m_value(std::move(t_value)) {
}
explicit Constant_AST_Node(Boxed_Value t_value)
: AST_Node_Impl<T>("", AST_Node_Type::Constant, Parse_Location())
, m_value(std::move(t_value)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const override { return m_value; }
Boxed_Value m_value;
};
template<typename T>
struct Id_AST_Node final : AST_Node_Impl<T> {
Id_AST_Node(const std::string &t_ast_node_text, Parse_Location t_loc)
: AST_Node_Impl<T>(t_ast_node_text, AST_Node_Type::Id, std::move(t_loc)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
try {
return t_ss.get_object(this->text, m_loc);
} catch (std::exception &) {
throw exception::eval_error("Can not find object: " + this->text);
}
}
private:
mutable std::atomic_uint_fast32_t m_loc = {0};
};
template<typename T>
struct Fun_Call_AST_Node : AST_Node_Impl<T> {
Fun_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Fun_Call, std::move(t_loc), std::move(t_children)) {
assert(!this->children.empty());
}
template<bool Save_Params>
Boxed_Value do_eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
std::vector<Boxed_Value> params;
params.reserve(this->children[1]->children.size());
for (const auto &child : this->children[1]->children) {
params.push_back(child->eval(t_ss));
}
if (Save_Params) {
fpp.save_params(Function_Params{params});
}
Boxed_Value fn(this->children[0]->eval(t_ss));
try {
return (*t_ss->boxed_cast<const dispatch::Proxy_Function_Base *>(fn))(Function_Params{params}, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'",
e.parameters,
e.functions,
false,
*t_ss);
} catch (const exception::bad_boxed_cast &) {
try {
using ConstFunctionTypeRef = const Const_Proxy_Function &;
Const_Proxy_Function f = t_ss->boxed_cast<ConstFunctionTypeRef>(fn);
// handle the case where there is only 1 function to try to call and dispatch fails on it
throw exception::eval_error("Error calling function '" + this->children[0]->text + "'", params, make_vector(f), false, *t_ss);
} catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("'" + this->children[0]->pretty_print() + "' does not evaluate to a function.");
}
} catch (const exception::arity_error &e) {
throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'");
} catch (const exception::guard_error &e) {
throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'");
} catch (detail::Return_Value &rv) {
return rv.retval;
}
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { return do_eval_internal<true>(t_ss); }
};
template<typename T>
struct Unused_Return_Fun_Call_AST_Node final : Fun_Call_AST_Node<T> {
Unused_Return_Fun_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: Fun_Call_AST_Node<T>(std::move(t_ast_node_text), std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
return this->template do_eval_internal<false>(t_ss);
}
};
template<typename T>
struct Arg_AST_Node final : AST_Node_Impl<T> {
Arg_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Arg_List, std::move(t_loc), std::move(t_children)) {
}
};
template<typename T>
struct Arg_List_AST_Node final : AST_Node_Impl<T> {
Arg_List_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Arg_List, std::move(t_loc), std::move(t_children)) {
}
static std::string get_arg_name(const AST_Node_Impl<T> &t_node) {
if (t_node.children.empty()) {
return t_node.text;
} else if (t_node.children.size() == 1) {
return t_node.children[0]->text;
} else {
return t_node.children[1]->text;
}
}
static std::vector<std::string> get_arg_names(const AST_Node_Impl<T> &t_node) {
std::vector<std::string> retval;
for (const auto &node : t_node.children) {
retval.push_back(get_arg_name(*node));
}
return retval;
}
static std::pair<std::string, Type_Info> get_arg_type(const AST_Node_Impl<T> &t_node, const chaiscript::detail::Dispatch_State &t_ss) {
if (t_node.children.size() < 2) {
return {};
} else {
return {t_node.children[0]->text, t_ss->get_type(t_node.children[0]->text, false)};
}
}
static dispatch::Param_Types get_arg_types(const AST_Node_Impl<T> &t_node, const chaiscript::detail::Dispatch_State &t_ss) {
std::vector<std::pair<std::string, Type_Info>> retval;
for (const auto &child : t_node.children) {
retval.push_back(get_arg_type(*child, t_ss));
}
return dispatch::Param_Types(std::move(retval));
}
};
template<typename T>
struct Equation_AST_Node final : AST_Node_Impl<T> {
Equation_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Equation, std::move(t_loc), std::move(t_children))
, m_oper(Operators::to_operator(this->text)) {
assert(this->children.size() == 2);
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
auto params = [&]() {
// The RHS *must* be evaluated before the LHS
// consider `var range = range(x)`
// if we declare the variable in scope first, then the name lookup fails
// for the RHS
auto rhs = this->children[1]->eval(t_ss);
auto lhs = this->children[0]->eval(t_ss);
std::array<Boxed_Value, 2> p{std::move(lhs), std::move(rhs)};
return p;
}();
if (params[0].is_return_value()) {
throw exception::eval_error("Error, cannot assign to temporary value.");
} else if (params[0].is_const()) {
throw exception::eval_error("Error, cannot assign to constant value.");
}
if (m_oper != Operators::Opers::invalid && params[0].get_type_info().is_arithmetic() && params[1].get_type_info().is_arithmetic()) {
try {
return Boxed_Number::do_oper(m_oper, params[0], params[1]);
} catch (const std::exception &) {
throw exception::eval_error("Error with unsupported arithmetic assignment operation.");
}
} else if (m_oper == Operators::Opers::assign) {
try {
if (params[0].is_undef()) {
if (!this->children.empty()
&& ((this->children[0]->identifier == AST_Node_Type::Reference)
|| (!this->children[0]->children.empty() && this->children[0]->children[0]->identifier == AST_Node_Type::Reference)))
{
/// \todo This does not handle the case of an unassigned reference variable
/// being assigned outside of its declaration
params[0].assign(params[1]);
params[0].reset_return_value();
return params[1];
} else {
params[1] = detail::clone_if_necessary(std::move(params[1]), m_clone_loc, t_ss);
}
}
try {
return t_ss->call_function(this->text, m_loc, Function_Params{params}, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss);
}
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Missing clone or copy constructor for right hand side of equation",
e.parameters,
e.functions,
false,
*t_ss);
}
} else if (this->text == ":=") {
if (params[0].is_undef() || Boxed_Value::type_match(params[0], params[1])) {
params[0].assign(params[1]);
params[0].reset_return_value();
} else {
throw exception::eval_error("Mismatched types in equation");
}
} else {
try {
return t_ss->call_function(this->text, m_loc, Function_Params{params}, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Unable to find appropriate'" + this->text + "' operator.", e.parameters, e.functions, false, *t_ss);
}
}
return params[1];
}
private:
Operators::Opers m_oper;
mutable std::atomic_uint_fast32_t m_loc = {0};
mutable std::atomic_uint_fast32_t m_clone_loc = {0};
};
template<typename T>
struct Global_Decl_AST_Node final : AST_Node_Impl<T> {
Global_Decl_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Global_Decl, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const std::string &idname = [&]() -> const std::string & {
if (this->children[0]->identifier == AST_Node_Type::Reference) {
return this->children[0]->children[0]->text;
} else {
return this->children[0]->text;
}
}();
return t_ss->add_global_no_throw(Boxed_Value(), idname);
}
};
template<typename T>
struct Var_Decl_AST_Node final : AST_Node_Impl<T> {
Var_Decl_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Var_Decl, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const std::string &idname = this->children[0]->text;
try {
Boxed_Value bv;
t_ss.add_object(idname, bv);
return bv;
} catch (const exception::name_conflict_error &e) {
throw exception::eval_error("Variable redefined '" + e.name() + "'");
}
}
};
template<typename T>
struct Assign_Decl_AST_Node final : AST_Node_Impl<T> {
Assign_Decl_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Assign_Decl, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const std::string &idname = this->children[0]->text;
try {
Boxed_Value bv(detail::clone_if_necessary(this->children[1]->eval(t_ss), m_loc, t_ss));
bv.reset_return_value();
t_ss.add_object(idname, bv);
return bv;
} catch (const exception::name_conflict_error &e) {
throw exception::eval_error("Variable redefined '" + e.name() + "'");
}
}
private:
mutable std::atomic_uint_fast32_t m_loc = {0};
};
template<typename T>
struct Array_Call_AST_Node final : AST_Node_Impl<T> {
Array_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Array_Call, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
std::array<Boxed_Value, 2> params{this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)};
try {
fpp.save_params(Function_Params{params});
return t_ss->call_function("[]", m_loc, Function_Params{params}, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, e.functions, false, *t_ss);
}
}
private:
mutable std::atomic_uint_fast32_t m_loc = {0};
};
template<typename T>
struct Dot_Access_AST_Node final : AST_Node_Impl<T> {
Dot_Access_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Dot_Access, std::move(t_loc), std::move(t_children))
, m_fun_name(((this->children[1]->identifier == AST_Node_Type::Fun_Call) || (this->children[1]->identifier == AST_Node_Type::Array_Call))
? this->children[1]->children[0]->text
: this->children[1]->text) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
Boxed_Value retval = this->children[0]->eval(t_ss);
auto params = make_vector(retval);
bool has_function_params = false;
if (this->children[1]->children.size() > 1) {
has_function_params = true;
for (const auto &child : this->children[1]->children[1]->children) {
params.push_back(child->eval(t_ss));
}
}
fpp.save_params(Function_Params{params});
try {
retval = t_ss->call_member(m_fun_name, m_loc, Function_Params{params}, has_function_params, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
if (e.functions.empty()) {
throw exception::eval_error("'" + m_fun_name + "' is not a function.");
} else {
throw exception::eval_error(std::string(e.what()) + " for function '" + m_fun_name + "'", e.parameters, e.functions, true, *t_ss);
}
} catch (detail::Return_Value &rv) {
retval = std::move(rv.retval);
}
if (this->children[1]->identifier == AST_Node_Type::Array_Call) {
try {
std::array<Boxed_Value, 2> p{retval, this->children[1]->children[1]->eval(t_ss)};
retval = t_ss->call_function("[]", m_array_loc, Function_Params{p}, t_ss.conversions());
} catch (const exception::dispatch_error &e) {
throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, e.functions, true, *t_ss);
}
}
return retval;
}
private:
mutable std::atomic_uint_fast32_t m_loc = {0};
mutable std::atomic_uint_fast32_t m_array_loc = {0};
const std::string m_fun_name;
};
template<typename T>
struct Lambda_AST_Node final : AST_Node_Impl<T> {
Lambda_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(t_ast_node_text,
AST_Node_Type::Lambda,
std::move(t_loc),
std::vector<AST_Node_Impl_Ptr<T>>(std::make_move_iterator(t_children.begin()),
std::make_move_iterator(std::prev(t_children.end()))))
, m_param_names(Arg_List_AST_Node<T>::get_arg_names(*this->children[1]))
, m_this_capture(has_this_capture(this->children[0]->children))
, m_lambda_node(std::move(t_children.back())) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const auto captures = [&]() -> std::map<std::string, Boxed_Value> {
std::map<std::string, Boxed_Value> named_captures;
for (const auto &capture : this->children[0]->children) {
named_captures.insert(std::make_pair(capture->children[0]->text, capture->children[0]->eval(t_ss)));
}
return named_captures;
}();
const auto numparams = this->children[1]->children.size();
const auto param_types = Arg_List_AST_Node<T>::get_arg_types(*this->children[1], t_ss);
std::reference_wrapper<chaiscript::detail::Dispatch_Engine> engine(*t_ss);
return Boxed_Value(dispatch::make_dynamic_proxy_function(
[engine, lambda_node = this->m_lambda_node, param_names = this->m_param_names, captures, this_capture = this->m_this_capture](
const Function_Params &t_params) {
return detail::eval_function(engine, *lambda_node, param_names, t_params, &captures, this_capture);
},
static_cast<int>(numparams),
m_lambda_node,
param_types));
}
static bool has_this_capture(const std::vector<AST_Node_Impl_Ptr<T>> &t_children) noexcept {
return std::any_of(std::begin(t_children), std::end(t_children), [](const auto &child) { return child->children[0]->text == "this"; });
}
private:
const std::vector<std::string> m_param_names;
const bool m_this_capture = false;
const std::shared_ptr<AST_Node_Impl<T>> m_lambda_node;
};
template<typename T>
struct Scopeless_Block_AST_Node final : AST_Node_Impl<T> {
Scopeless_Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Scopeless_Block, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const auto num_children = this->children.size();
for (size_t i = 0; i < num_children - 1; ++i) {
this->children[i]->eval(t_ss);
}
return this->children.back()->eval(t_ss);
}
};
template<typename T>
struct Block_AST_Node final : AST_Node_Impl<T> {
Block_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Block, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
const auto num_children = this->children.size();
for (size_t i = 0; i < num_children - 1; ++i) {
this->children[i]->eval(t_ss);
}
return this->children.back()->eval(t_ss);
}
};
template<typename T>
struct Def_AST_Node final : AST_Node_Impl<T> {
std::shared_ptr<AST_Node_Impl<T>> m_body_node;
std::shared_ptr<AST_Node_Impl<T>> m_guard_node;
Def_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text),
AST_Node_Type::Def,
std::move(t_loc),
std::vector<AST_Node_Impl_Ptr<T>>(std::make_move_iterator(t_children.begin()),
std::make_move_iterator(
std::prev(t_children.end(), has_guard(t_children, 1) ? 2 : 1))))
,
// This apparent use after move is safe because we are only moving out the specific elements we need
// on each operation.
m_body_node(get_body_node(std::move(t_children)))
, m_guard_node(get_guard_node(std::move(t_children), t_children.size() - this->children.size() == 2))
{
}
static std::shared_ptr<AST_Node_Impl<T>> get_guard_node(std::vector<AST_Node_Impl_Ptr<T>> &&vec, bool has_guard) {
if (has_guard) {
return std::move(*std::prev(vec.end(), 2));
} else {
return {};
}
}
static std::shared_ptr<AST_Node_Impl<T>> get_body_node(std::vector<AST_Node_Impl_Ptr<T>> &&vec) { return std::move(vec.back()); }
static bool has_guard(const std::vector<AST_Node_Impl_Ptr<T>> &t_children, const std::size_t offset) noexcept {
if ((t_children.size() > 2 + offset) && (t_children[1 + offset]->identifier == AST_Node_Type::Arg_List)) {
if (t_children.size() > 3 + offset) {
return true;
}
} else {
if (t_children.size() > 2 + offset) {
return true;
}
}
return false;
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
std::vector<std::string> t_param_names;
size_t numparams = 0;
dispatch::Param_Types param_types;
if ((this->children.size() > 1) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) {
numparams = this->children[1]->children.size();
t_param_names = Arg_List_AST_Node<T>::get_arg_names(*this->children[1]);
param_types = Arg_List_AST_Node<T>::get_arg_types(*this->children[1], t_ss);
}
std::reference_wrapper<chaiscript::detail::Dispatch_Engine> engine(*t_ss);
std::shared_ptr<dispatch::Proxy_Function_Base> guard;
if (m_guard_node) {
guard = dispatch::make_dynamic_proxy_function(
[engine, guardnode = m_guard_node, t_param_names](const Function_Params &t_params) {
return detail::eval_function(engine, *guardnode, t_param_names, t_params);
},
static_cast<int>(numparams),
m_guard_node);
}
try {
const std::string &l_function_name = this->children[0]->text;
t_ss->add(dispatch::make_dynamic_proxy_function(
[engine, func_node = m_body_node, t_param_names](const Function_Params &t_params) {
return detail::eval_function(engine, *func_node, t_param_names, t_params);
},
static_cast<int>(numparams),
m_body_node,
param_types,
guard),
l_function_name);
} catch (const exception::name_conflict_error &e) {
throw exception::eval_error("Function redefined '" + e.name() + "'");
}
return void_var();
}
};
template<typename T>
struct While_AST_Node final : AST_Node_Impl<T> {
While_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::While, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
try {
while (this->get_scoped_bool_condition(*this->children[0], t_ss)) {
try {
this->children[1]->eval(t_ss);
} catch (detail::Continue_Loop &) {
// we got a continue exception, which means all of the remaining
// loop implementation is skipped and we just need to continue to
// the next condition test
}
}
} catch (detail::Break_Loop &) {
// loop was broken intentionally
}
return void_var();
}
};
template<typename T>
struct Class_AST_Node final : AST_Node_Impl<T> {
Class_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Class, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
/// \todo do this better
// put class name in current scope so it can be looked up by the attrs and methods
t_ss.add_object("_current_class_name", const_var(this->children[0]->text));
this->children[1]->eval(t_ss);
return void_var();
}
};
template<typename T>
struct If_AST_Node final : AST_Node_Impl<T> {
If_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::If, std::move(t_loc), std::move(t_children)) {
assert(this->children.size() == 3);
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
if (this->get_bool_condition(this->children[0]->eval(t_ss), t_ss)) {
return this->children[1]->eval(t_ss);
} else {
return this->children[2]->eval(t_ss);
}
}
};
template<typename T>
struct Ranged_For_AST_Node final : AST_Node_Impl<T> {
Ranged_For_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Ranged_For, std::move(t_loc), std::move(t_children)) {
assert(this->children.size() == 3);
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
const auto get_function = [&t_ss](const std::string &t_name, auto &t_hint) {
uint_fast32_t hint = t_hint;
auto [funs_loc, funs] = t_ss->get_function(t_name, hint);
if (funs_loc != hint) {
t_hint = uint_fast32_t(funs_loc);
}
return std::move(funs);
};
const auto call_function = [&t_ss](const auto &t_funcs, const Boxed_Value &t_param) {
return dispatch::dispatch(*t_funcs, Function_Params{t_param}, t_ss.conversions());
};
const std::string &loop_var_name = this->children[0]->text;
Boxed_Value range_expression_result = this->children[1]->eval(t_ss);
const auto do_loop = [&loop_var_name, &t_ss, this](const auto &ranged_thing) {
try {
for (auto &&loop_var : ranged_thing) {
// This scope push and pop might not be the best thing for perf
// but we know it's 100% correct
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
/// to-do make this if-constexpr with C++17 branch
if (!std::is_same<std::decay_t<decltype(loop_var)>, Boxed_Value>::value) {
t_ss.add_get_object(loop_var_name, Boxed_Value(std::ref(loop_var)));
} else {
t_ss.add_get_object(loop_var_name, Boxed_Value(loop_var));
}
try {
this->children[2]->eval(t_ss);
} catch (detail::Continue_Loop &) {
}
}
} catch (detail::Break_Loop &) {
// loop broken
}
return void_var();
};
if (range_expression_result.get_type_info().bare_equal_type_info(typeid(std::vector<Boxed_Value>))) {
return do_loop(boxed_cast<const std::vector<Boxed_Value> &>(range_expression_result));
} else if (range_expression_result.get_type_info().bare_equal_type_info(typeid(std::map<std::string, Boxed_Value>))) {
return do_loop(boxed_cast<const std::map<std::string, Boxed_Value> &>(range_expression_result));
} else {
const auto range_funcs = get_function("range", m_range_loc);
const auto empty_funcs = get_function("empty", m_empty_loc);
const auto front_funcs = get_function("front", m_front_loc);
const auto pop_front_funcs = get_function("pop_front", m_pop_front_loc);
try {
const auto range_obj = call_function(range_funcs, range_expression_result);
while (!boxed_cast<bool>(call_function(empty_funcs, range_obj))) {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
t_ss.add_get_object(loop_var_name, call_function(front_funcs, range_obj));
try {
this->children[2]->eval(t_ss);
} catch (detail::Continue_Loop &) {
// continue statement hit
}
call_function(pop_front_funcs, range_obj);
}
} catch (detail::Break_Loop &) {
// loop broken
}
return void_var();
}
}
private:
mutable std::atomic_uint_fast32_t m_range_loc = {0};
mutable std::atomic_uint_fast32_t m_empty_loc = {0};
mutable std::atomic_uint_fast32_t m_front_loc = {0};
mutable std::atomic_uint_fast32_t m_pop_front_loc = {0};
};
template<typename T>
struct For_AST_Node final : AST_Node_Impl<T> {
For_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::For, std::move(t_loc), std::move(t_children)) {
assert(this->children.size() == 4);
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
try {
for (this->children[0]->eval(t_ss); this->get_scoped_bool_condition(*this->children[1], t_ss); this->children[2]->eval(t_ss)) {
try {
// Body of Loop
this->children[3]->eval(t_ss);
} catch (detail::Continue_Loop &) {
// we got a continue exception, which means all of the remaining
// loop implementation is skipped and we just need to continue to
// the next iteration step
}
}
} catch (detail::Break_Loop &) {
// loop broken
}
return void_var();
}
};
template<typename T>
struct Switch_AST_Node final : AST_Node_Impl<T> {
Switch_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_Node_Impl_Ptr<T>> t_children)
: AST_Node_Impl<T>(std::move(t_ast_node_text), AST_Node_Type::Switch, std::move(t_loc), std::move(t_children)) {
}
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
bool breaking = false;
size_t currentCase = 1;
bool hasMatched = false;
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
Boxed_Value match_value(this->children[0]->eval(t_ss));
while (!breaking && (currentCase < this->children.size())) {
try {
if (this->children[currentCase]->identifier == AST_Node_Type::Case) {
// This is a little odd, but because want to see both the switch and the case simultaneously, I do a downcast here.
try {
std::array<Boxed_Value, 2> p{match_value, this->children[currentCase]->children[0]->eval(t_ss)};
if (hasMatched || boxed_cast<bool>(t_ss->call_function("==", m_loc, Function_Params{p}, t_ss.conversions()))) {
this->children[currentCase]->eval(t_ss);
hasMatched = true;
}
} catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("Internal error: case guard evaluation not boolean");