-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAST.h
1174 lines (1033 loc) · 27 KB
/
AST.h
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
#pragma once
//#define NDEBUG
#include <assert.h>
#include "Forward.h"
#include "ImmutableString.h"
#include "FailureOr.h"
#include "SharedEnums.h"
#include "Error.h"
#include "Value.h"
class ASTNode // ASTNodes are abstract symbols which together form a "flow chart" tree of symbols that the parser creates from the text that the interpreter then interprets.
{
protected:
ASTNode() = default;
public:
ASTNode(const ASTNode&) = delete;
virtual ~ASTNode() = default;
int my_line = 0; // Hypothetically, the line that this ASTNode happens on. This is remembered for the sake of improving runtime legibility.
virtual const std::string class_name() const { return "ASTNode"; } // FIXME: Make better use of typeid() calls instead of this shit
// Collapses this symbol into a real dang thing or process that the interpreter can do.
virtual Value resolve(Interpreter&);
// Attempts to collapse this symbol in a constant and scope-less manner. Bool determines if it throws an error when this fails or not.
virtual Value const_resolve(Parser&, bool);
// Returns a reference to whatever Value this ASTNode points to, if any, which you can freely use the operator= on to set it to some new value.
// Mostly to be used by AssignmentStatement and friends.
virtual Value& handle(Interpreter&);
virtual std::string dump(int indent) { return std::string(indent, ' ') + class_name() + "\n"; } // Used for debugging
virtual bool is_expression() { return false; }
};
class Literal : public ASTNode { // A node which denotes a plain ol' literal.
Value heldval;
public:
Literal(Value V)
:heldval(V)
{
}
virtual Value resolve(Interpreter&) override;
virtual Value const_resolve(Parser&, bool) override;
virtual const std::string class_name() const override { return "Literal"; }
virtual std::string dump(int indent) override { return std::string(indent, ' ') + "Literal: " + heldval.to_string() + "\n"; }
};
// Expressions are ASTNodes that make sense to be done on their lonesome as a statement w/o other context.
class Expression : public ASTNode
{
#ifdef JOAO_SAFE
private:
static int expr_count;
protected:
void increment()
{
++expr_count;
if (expr_count > MAX_STATEMENTS)
{
throw error::maximum_expr(std::string("Program reached the limit of ") + std::to_string(MAX_STATEMENTS) + std::string("expressions!"));
}
}
#endif
public:
virtual bool is_expression() override { return true; }
};
class Identifier : public ASTNode
{
ImmutableString t_name;
public:
Identifier(const ImmutableString& s)
:t_name(s)
{
}
const ImmutableString& get_str() const
{
return t_name;
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
virtual const std::string class_name() const override { return "Identifier"; }
virtual std::string dump(int indent) override { return std::string(indent, ' ') + "Identifier: " + t_name.to_string() + "\n"; }
};
class AssignmentStatement : public Expression
{
protected:
ASTNode* id;
ASTNode* rhs;
public:
enum class aOps : uint8_t {
NoOp, // More a Parser abstraction than anything else
Assign, // =
AssignAdd, // +=
AssignSubtract, // -=
AssignMultiply, // *=
AssignDivide // /=
}t_op;
AssignmentStatement(ASTNode* i, ASTNode* r, aOps o = aOps::Assign, int linenum = 0)
:id(i),
rhs(r),
t_op(o)
{
my_line = linenum;
}
virtual ~AssignmentStatement()
{
delete id;
delete rhs;
}
virtual Value resolve(Interpreter&) override;
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "AssignmentStatement, Operation: Assign\n";
str += id->dump(indent + 1);
str += rhs->dump(indent + 1);
return str;
}
};
class LocalAssignmentStatement final : public AssignmentStatement // "Value x = 3;", which has a distinct ASTNode type from "x = 3;"
{
LocalType ty = LocalType::Value;
bool typecheck(Value& ruh) // returns true if it passes the typecheck, false if it fails
{
if (ruh.t_vType == Value::vType::Null)
return true; // Allows for null-initialization of these things
switch (ty)
{
case(LocalType::Value):
break;
case(LocalType::Number):
if (ruh.t_vType == Value::vType::Integer || ruh.t_vType == Value::vType::Double)
break;
return false;
break;
case(LocalType::String):
if (ruh.t_vType == Value::vType::String)
break;
return false;
break;
case(LocalType::Boolean):
if (ruh.t_vType == Value::vType::Bool)
break;
return false;
break;
case(LocalType::Object):
if (ruh.t_vType == Value::vType::Object)
break;
return false;
break;
default:
return false;
}
return true;
}
public:
LocalAssignmentStatement(Identifier* i, ASTNode* r, aOps o, LocalType localtype, int linenum = 0)
:AssignmentStatement(i,r,o)
,ty(localtype)
{
my_line = linenum;
}
virtual Value resolve(Interpreter&) override;
virtual Value const_resolve(Parser&, bool) override;
//A special-snowflake resolver for LocalAssignmentStatement which returns the key-value pair of the property it describes.
std::pair<ImmutableString, Value> resolve_property(Parser&);
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "LocalAssignmentStatement, Type: Value, Operation: Assign\n";
str += id->dump(indent + 1);
str += rhs->dump(indent + 1);
return str;
}
};
class UnaryExpression : public Expression
{
public:
enum class uOps : uint8_t {
NoOp,
Not,
Negate,
BitwiseNot,
Length
}t_op;
ASTNode* t_rhs;
UnaryExpression(uOps Operator, ASTNode* r, int linenum = 0)
:t_op(Operator)
,t_rhs(r)
{
my_line = linenum;
}
virtual ~UnaryExpression()
{
delete t_rhs;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "UnaryExpression"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string op;
switch (t_op)
{
case(uOps::Not):
op = "Not";
break;
case(uOps::BitwiseNot):
op = "BitwiseNot";
break;
case(uOps::Length):
op = "Length";
break;
case(uOps::Negate):
op = "Negate";
break;
default:
op = "???";
break;
}
std::string str = ind + "UnaryExpresion, Operation: " + op + "\n";
return str + t_rhs->dump(indent + 1);
}
};
class BinaryExpression : public Expression
{ // An ASTNode which is an operation between two, smaller Expressions.
public:
enum class bOps : uint8_t {
NoOp, // Used by the parser to store what is basically a null into one of these bOps enums.
//
Add,
Subtract,
Multiply,
Divide,
//
FloorDivide,
Exponent,
Modulo,
//
BitwiseAnd,
BitwiseXor,
BitwiseOr,
//
ShiftRight,
ShiftLeft,
//
Concatenate,
//
LessThan,
LessEquals,
Greater,
GreaterEquals,
Equals,
NotEquals,
//
LogicalAnd,
LogicalOr,
LogicalXor
}t_op;
ASTNode* t_lhs, *t_rhs;
BinaryExpression(bOps Operator, ASTNode* l, ASTNode* r, int linenum = 0)
:t_op(Operator)
,t_lhs(l) // What the fuck is this syntax holy shit
,t_rhs(r)
{
my_line = linenum;
}
virtual ~BinaryExpression()
{
delete t_lhs;
delete t_rhs;
}
virtual Value resolve(Interpreter&) override;
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string sop;
switch (t_op)
{
case(bOps::NoOp):
sop = "NoOp, somehow?";
break;
case(bOps::Add):
sop = "Add";
break;
case(bOps::Subtract):
sop = "Subtract";
break;
case(bOps::Multiply):
sop = "Multiply";
break;
case(bOps::Divide):
sop = "Divide";
break;
//
case(bOps::Exponent):
sop = "Exponent";
break;
//
case(bOps::Concatenate):
sop = "Concatenate";
break;
//
case(bOps::ShiftLeft):
sop = "ShiftLeft";
break;
//
case(bOps::Equals):
sop = "Equals";
break;
case(bOps::NotEquals):
sop = "NotEquals";
break;
//
default:
sop = "????";
}
std::string str = ind + "BinaryStatement, Operation: " + sop + "\n";
str += t_lhs->dump(indent + 1);
str += t_rhs->dump(indent + 1);
return str;
}
static FailureOr BinaryOperation(const Value&,const Value&, bOps);
};
class ReturnStatement : public Expression {
ASTNode *held_expr;
public:
bool has_expr{ false };
virtual const std::string class_name() const override { return "ReturnStatement"; }
ReturnStatement()
:held_expr(nullptr) // Very tiny memory optimization; we don't store anything and just return null if this is a nullptr.
{
}
ReturnStatement(ASTNode* node, int linenum = 0)
:held_expr(node)
,has_expr(true)
{
my_line = linenum;
}
virtual ~ReturnStatement()
{
delete held_expr; // Deleting nullptr is ok!
}
virtual Value resolve(Interpreter& interp) override;
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "ReturnStatement:\n";
if(has_expr)
str += held_expr->dump(indent + 1);
else
str += Literal(Value()).dump(indent + 1); // rvalues my beloved
return str;
}
};
class CallExpression : public Expression {
ASTNode* func_expr;
std::vector<ASTNode*> args;
public:
CallExpression(ASTNode* f)
:func_expr(f)
{
}
CallExpression(ASTNode* f, std::vector<ASTNode*> arr, int linenum = 0)
:func_expr(f)
,args(arr) // Arr!!
{
my_line = linenum;
}
virtual ~CallExpression()
{
delete func_expr;
for (ASTNode* ptr : args)
{
delete ptr;
}
}
CallExpression* append_arg(ASTNode* expr)
{
args.push_back(expr);
return this;
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
virtual const std::string class_name() const override { return "CallExpression"; }
virtual std::string dump(int indent) override {
std::string ind = std::string(indent, ' ');
std::string str = ind + "CallExpression\n";
str += ind +"@Function:\n" + func_expr->dump(indent+1);
str += ind + "(Args:\n";
for (size_t i = 0; i < args.size(); ++i)
{
str += args[i]->dump(indent + 1);
}
str += ind + ")\n";
return str;
}
};
class Function : public ASTNode
{
protected:
Value returnValue = Value(); // My return value
std::vector<Expression*> statements; // The statements which're executed when I am run
std::vector<Value> t_args;
std::vector<ImmutableString> t_argnames;
//std::vector<Expression> args;
std::string t_name; // My name
Object* obj = nullptr; // I don't know.
Value my_value; // A value reference which represents me. of type Function; necessary for somethings sometimes
Function()
:my_value(Value(this))
{
}
public:
virtual ~Function()
{
//we don't actually own that $obj value so
for (Expression* exp : statements)
{
delete exp;
}
}
Value& to_value() { return my_value; }
const std::string& get_name() const { return t_name; }
Object* get_obj() const { return obj; }
void set_obj(Object* o) { obj = o; };
Function(const std::string& name, Expression* expr)
:my_value(Value(this))
{
t_name = name;
statements = std::vector<Expression*>{ expr };
}
Function(const std::string& name, std::vector<Expression*> exprs) // argument-less-ness
:my_value(Value(this))
{
t_name = name;
statements = exprs;
}
Function(const std::string& name, std::vector<Expression*>&& exprs, std::vector<ImmutableString>&& sargs, int linenum = 0)
:my_value(Value(this))
{
t_name = name;
statements = exprs;
t_argnames = sargs;
my_line = linenum;
}
void give_args(Interpreter&, std::vector<Value>&, Object*);
Function* append(Expression* expr)
{
statements.push_back(expr);
return this;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "Function"; }
virtual std::string dump(int indent) override
{
//std::string ind = std::string(indent, ' ');
std::string str = "Function, name: " + t_name + "\n";
str += "@Params:\n";
for (size_t i = 0; i < t_argnames.size(); ++i)
{
str += " " + t_argnames[i].to_string() + "\n";
}
str += "=Statements:\n";
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
return str;
}
};
template <typename Lambda>
class NativeFunction : public Function
{
protected:
/*
So this kinda overrides a lot of the typical behavior of a function, instead deferring it into some kickass lambda stored within.
*/
Lambda lambda;
// Value lambda() {};
public:
NativeFunction(std::string n, Lambda lamb)
:Function()
,lambda(lamb)
{
my_line = 0; // These functions aren't really supposed to BE anywhere in the code per se so..
t_name = n;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "NativeFunction"; }
virtual std::string dump([[maybe_unused]] int indent) override
{
return "NativeFunction, name: " + t_name + "\n";
}
};
//Similar to a NativeFunction except it requires a handle to an object its acting within.
class NativeMethod final : public Function
{
public:
using Lambda = std::function<Value(const std::vector<Value>& args, Object* obj)>;
private:
Lambda lambda;
public:
bool is_static = false; // True if it actually doesn't need an object to act on
NativeMethod(std::string n, Lambda lamb)
:Function()
,lambda(lamb)
{
my_line = 0;
t_name = n;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "NativeMethod"; }
virtual std::string dump([[maybe_unused]] int indent) override
{
return "NativeMethod, name: " + t_name + "\n";
}
};
class Block : public Expression
{
protected:
std::vector<Expression*> statements;
Block(const std::vector<Expression*>& s)
:statements(s)
{
}
virtual ~Block()
{
for (Expression* exp : statements)
{
delete exp;
}
}
//A more abstract function used for iterating over any arbitrary block of statements.
//It's strange, but necessary for a class that is this abstract.
Value iterate(const std::vector<Expression*>&, Interpreter&);
//Iterate over our statements specifically.
Value iterate_statements(Interpreter&);
};
class IfBlock final : public Block
{
ASTNode* condition = nullptr;
IfBlock* Elseif = nullptr;
public:
IfBlock(std::vector<Expression*>& st)
:Block(st)
{
}
IfBlock(ASTNode* cond, std::vector<Expression*>& st, int linenum = 0)
:Block(st)
,condition(cond)
{
my_line = linenum;
statements = st;
}
virtual ~IfBlock()
{
delete condition;
delete Elseif;
}
void append_else(IfBlock* elif)
{
if (Elseif) // If we already have an elif
Elseif->append_else(elif); // append this to the bottom of the current chain. FIXME: Recursion is bad and you should feel bad.
else
Elseif = elif;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "IfBlock"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind;
if (condition)
{
str += "IfBlock\n";
str += ind + "?Cond:\n" + condition->dump(indent + 2);
}
else
{
str += "ElseBlock\n";
}
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
if (Elseif)
{
str += Elseif->dump(indent);
}
return str;
}
};
class ForBlock final : public Block
{
ASTNode* initializer = nullptr;
ASTNode* condition = nullptr;
ASTNode* increment = nullptr;
public:
ForBlock(ASTNode* init, ASTNode* cond, ASTNode* inc, std::vector<Expression*>& st, int linenum = 0)
:Block(st),
initializer(init),
condition(cond),
increment(inc)
{
my_line = linenum;
}
virtual ~ForBlock()
{
delete initializer;
delete condition;
delete increment;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "ForBlock"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "ForBlock\n";
str += ind + "=Init:\n" + initializer->dump(indent + 2);
str += ind + "?Cond:\n" + condition->dump(indent + 2);
str += ind + "+Inc:\n" + increment->dump(indent + 2);
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
return str;
}
};
class ForEachBlock final : public Block
{
ImmutableString key_name;
ImmutableString value_name;
ASTNode* table_node;
public:
ForEachBlock(const ImmutableString& k , const ImmutableString& v, ASTNode* tn, const std::vector<Expression*>& st, int linenum = 0)
:Block(st),
key_name(k),
value_name(v),
table_node(tn)
{
my_line = linenum;
}
virtual ~ForEachBlock()
{
delete table_node;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "ForEachBlock"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "ForEachBlock\n";
str += ind + "=Pair: " + key_name.to_string() + "," + value_name.to_string() + "\n";
str += ind + "<in:\n" + table_node->dump(indent + 2);
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
return str;
}
};
class WhileBlock final : public Block
{
ASTNode* condition = nullptr;
public:
WhileBlock(ASTNode* cond, std::vector<Expression*>& st, int linenum = 0)
:Block(st)
,condition(cond)
{
my_line = linenum;
}
virtual ~WhileBlock()
{
delete condition;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "WhileBlock"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "WhileBlock\n";
str += ind + "?Cond:\n" + condition->dump(indent + 2);
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
return str;
}
};
class BreakStatement final : public Expression
{
int breaknum;
public:
BreakStatement(int br = 1, int linenum = 0)
:breaknum(br)
{
my_line = linenum;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "BreakStatement"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "Break " + std::to_string(breaknum) + ";\n";
return str;
}
};
class ContinueStatement final : public Expression
{
public:
ContinueStatement(int linenum = 0)
{
my_line = linenum;
}
virtual Value resolve(Interpreter&) override;
virtual const std::string class_name() const override { return "ContinueStatement"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "Continue;\n";
return str;
}
};
class MemberAccess final : public ASTNode
{
ASTNode* front;
ASTNode* back;
public:
MemberAccess(ASTNode* f, ASTNode* b, int linenum = 0)
:front(f)
,back(b)
{
my_line = linenum;
}
virtual ~MemberAccess()
{
delete front;
delete back;
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
virtual const std::string class_name() const override { return "MemberAccess"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "MemberAccess\n";
str += front->dump(indent + 1);
str += back->dump(indent + 1);
return str;
}
};
class ClassDefinition final : public ASTNode
{
std::vector<LocalAssignmentStatement*> statements;
public:
ImmutableString direct;
ClassDefinition(std::string& d, std::vector<LocalAssignmentStatement*> &s, int linenum = 0)
:statements(s)
,direct(d)
{
my_line = linenum;
}
virtual ~ClassDefinition()
{
for (LocalAssignmentStatement* ptr : statements)
{
delete ptr;
}
}
//virtual Value resolve(Interpreter&) override;
Hashtable<ImmutableString, Value> resolve_properties(Parser&);
void append_properties(Parser&, ObjectType*);
virtual const std::string class_name() const override { return "ClassDefinition"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = ind + "ClassDefinition " + direct.to_string() + ";\n";
for (size_t i = 0; i < statements.size(); ++i)
{
str += statements[i]->dump(indent + 1);
}
return str;
}
};
class Construction : public ASTNode
{
ImmutableString type;
std::vector<ASTNode*> args;
public:
Construction(const ImmutableString& t, std::vector<ASTNode*> a, int linenum = 0)
:type(t)
,args(a)
{
my_line = linenum;
}
virtual ~Construction()
{
for (ASTNode* arg : args)
{
delete arg;
}
}
virtual const std::string class_name() const override { return "Construction"; }
virtual std::string dump(int indent) override
{
std::string ind = std::string(indent, ' ');
std::string str = std::string(indent, ' ') + "Construction, type: " + type.to_string() + "\n";
str += ind + "(Args:\n";
for (size_t i = 0; i < args.size(); ++i)
{
str += args[i]->dump(indent + 1);
}
str += ind + ")\n";
return str;
}
virtual Value resolve(Interpreter&) override;
};
class ParentAccess : public ASTNode
{
public:
ImmutableString prop;
ParentAccess(const ImmutableString& p, int linenum = 0)
:prop(p)
{
my_line = linenum;
}
virtual const std::string class_name() const override { return "ParentAccess"; }
virtual std::string dump(int indent) override
{
return std::string(indent, ' ') + "ParentAccess, property: " + prop.to_string() + "\n";
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
};
class ParentGet : public ASTNode
{
public:
ParentGet(int linenum = 0)
{
my_line = linenum;
}
virtual const std::string class_name() const override { return "ParentGet"; }
virtual std::string dump(int indent) override
{
return std::string(indent, ' ') + "ParentGet\n";
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
};
class GrandparentAccess : public ASTNode
{
unsigned int depth;
ImmutableString prop;
public:
GrandparentAccess(unsigned int d,const ImmutableString& p, int linenum = 0)
:depth(d)
,prop(p)
{
my_line = linenum;
}
virtual const std::string class_name() const override { return "GrandparentAccess"; }
virtual std::string dump(int indent) override
{
return std::string(indent, ' ') + "GrandparentAccess, property: " + prop.to_string() + "; depth: " + std::to_string(depth) + "\n";
}
virtual Value resolve(Interpreter&) override;
virtual Value& handle(Interpreter&) override;
};
class GlobalAccess : public ASTNode
{
public:
ImmutableString var;
GlobalAccess(const ImmutableString& v, int linenum = 0)
:var(v)
{
my_line = linenum;
}
virtual const std::string class_name() const override { return "GlobalAccess"; }
virtual std::string dump(int indent) override
{
return std::string(indent, ' ') + "GlobalAccess, property: " + var.to_string() + "\n";
}
virtual Value resolve(Interpreter&) override;