-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpression.cc
1606 lines (1485 loc) · 43.8 KB
/
expression.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "namespace.h"
#include "lexem_tree_root.h"
#include <stack>
#include <string>
#if ! MODERN_COMPILER
#include <stdlib.h>
#endif
extern int GetLexemPriority(lexem_type_t lex);
class ExpressionParser
{
private:
enum {
default_state,
wait_name_delimiter,
dot_state,
find_function_state
} state;
std::deque<shunting_yard_t> operands;
std::deque<shunting_yard_t> operators;
bool prev_was_operand;
namespace_t * space_state; // = nullptr;
variable_base_t * variable_state; // = nullptr;
function_parser * function_state; // = nullptr;
variable_base_t * FindVariable(shunting_yard_t * pYard);
type_t * CreateType(shunting_yard_t * yard);
expression_node_t * CreateNode(shunting_yard_t * yard, shunting_yard_t * parent, expression_node_t * left);
void CreateConstant(reg_t * reg, shunting_yard_t * yard);
int Parse(SourcePtr &source);
void FixExpression(shunting_yard_t shunt, int prio);
void PrepareArguments(call_t * call, expression_node_t * arg);
expression_node_t * CheckWord(
namespace_t * space,
std::string name,
bool no_parent_check,
expression_node_t * rvalue,
int line_num);
expression_node_t * CreateWordMode(std::string word, shunting_yard_t * parent, expression_node_t * left, expression_node_t * rvalue, int line_num);
void ParseArguments(SourcePtr * node, shunting_yard_t * parent);
public:
lexem_type_t ParseExpression(SourcePtr &source);
shunting_yard_t * PrepareExpression(void);
expression_t * BuildExpression(void);
void ParseOperator_NEW(SourcePtr &source);
void ParseOperator_SIZEOF(SourcePtr &source);
lexem_type_t ParseIncrementDecrement(SourcePtr &node);
void ParseOpenBracket(SourcePtr &node);
ExpressionParser(namespace_t * current_space)
{
state = default_state;
space_state = current_space;
prev_was_operand = false;
variable_state = nullptr;
function_state = nullptr;
}
};
int ExpressionParser::Parse(SourcePtr &source)
{
int prio = GetLexemPriority(source.lexem);
shunting_yard_t shunt(source);
switch (shunt.lexem)
{
case lt_semicolon:
fprintf(stderr, "Got semicolon in expression\n");
break;
case lt_openindex:
{
ExpressionParser index_parser(space_state);
#if MODERN_COMPILER
index_parser.ParseExpression(SourcePtr(source.sequence));
#else
SourcePtr ptr(source.sequence);
index_parser.ParseExpression(ptr);
index_parser.PrepareExpression();
#endif
FixExpression(shunt, prio);
shunting_yard_t * index_exp = new shunting_yard_t(*index_parser.PrepareExpression());
operands.push_back(*index_exp);
prev_was_operand = true;
break;
}
case lt_openbraket:
if (prev_was_operand)
throw "We got function() call; Expression state machine error";
operators.push_back(shunt);
break;
case lt_call_method:
operands.back().lexem = shunt.lexem;
break;
case lt_call:
operands.back().lexem = shunt.lexem;
break;
case lt_mul:
if (!prev_was_operand)
{
shunt.lexem = lt_indirect;
shunt.unary = true;
prio = 30;
}
FixExpression(shunt, prio);
break;
case lt_and:
if (!prev_was_operand)
{
shunt.lexem = lt_addrres;
shunt.unary = true;
prio = 30;
}
FixExpression(shunt, prio);
break;
case lt_closebraket:
throw "FSM error: lt_closebracker was reduced on lexem splitting stage";
default:
if (prio >= 1000)
{
operands.push_back(shunt);
prev_was_operand = true;
}
else
{
FixExpression(shunt, prio);
}
}
return 0;
}
void ExpressionParser::FixExpression(shunting_yard_t shunt, int prio)
{
if (!prev_was_operand)
{
// ïîêà òàê
if (shunt.lexem == lt_inc || shunt.lexem == lt_dec)
//
shunt.unary = true;
}
if (operators.size() != 0)
{
shunting_yard_t op = operators.back();
if (op.lexem == lt_namescope)
{
if (operands.size() != 0)
{
shunting_yard_t name = operands.back();
operands.pop_back();
op.right = new shunting_yard_t(name);
}
if (operands.size() != 0)
{
shunting_yard_t spacename = operands.back();
operands.pop_back();
op.left = new shunting_yard_t(spacename);
}
operators.pop_back();
operands.push_back(op);
//shunt = op;
}
else
while (GetLexemPriority(op.lexem) <= prio)
{
operators.pop_back();
if (operands.size() > 0)
{
op.right = new shunting_yard_t(operands.back());
operands.pop_back();
if (!op.unary)
{
op.left = new shunting_yard_t(operands.back());
operands.pop_back();
}
}
else
{
if (op.lexem == lt_namescope)
{
// Do notning
}
else if (op.lexem == lt_typecasting)
{
// Got it on casting unary minused integer to enumerated type;
operators.push_back(op);
break;
}
else
{
throw "Unary operation without precedence";
}
}
operands.push_back(op);
if (operators.size() == 0)
break;
op = operators.back();
}
}
operators.push_back(shunt);
prev_was_operand = false;
}
variable_base_t * ExpressionParser::FindVariable(shunting_yard_t * pYard)
{
variable_base_t * var = nullptr;
switch (pYard->lexem)
{
case lt_this:
fprintf(stderr, "debug\n");
break;
case lt_word:
var = this->space_state->FindVariable(pYard->text);
break;
case lt_openindex:
var = this->space_state->FindVariable(pYard->left->text);
break;
default:
throw "Parser stuck at ExpressionParser::FindVariable(shunting_yard_t * pYard)";
break;
}
return var;
}
type_t * ExpressionParser::CreateType(shunting_yard_t * yard)
{
if (yard->lexem == lt_pointer)
return new pointer_t(CreateType(yard->left));
type_t * type = space_state->GetBuiltinType(yard->lexem);
if (type != nullptr)
return type;
if (yard->lexem != lt_word)
return nullptr;
return this->space_state->FindType(yard->text);
}
void ExpressionParser::CreateConstant(reg_t * constant, shunting_yard_t * yard)
{
switch (yard->lexem)
{
case lt_character:
{
char ch = yard->text[0];
if (ch == '\\')
switch (yard->text[1])
{
case '0':
ch = '\0';
break;
case '\\':
case '\'':
case '\"':
ch = yard->text[1];
break;
case 'r':
ch = '\r';
break;
case 'n':
ch = '\n';
break;
case 't':
ch = '\t';
break;
default:
throw "Character sequence not parsed";
}
constant->integer_value = (ch);
break;
}
case lt_integer:
#if MODERN_COMPILER
if (yard->text.rfind("0x", 0) == 0)
constant = new constant_node_t(std::stoi(yard->text, nullptr, 16));
else
constant = new constant_node_t(std::stoi(yard->text));
#else
if (yard->text.rfind("0x", 0) == 0)
{
constant = new reg_t;
constant->integer_value = ((int)strtol(yard->text.c_str(), nullptr, 16));
}
else
{
constant->integer_value = (atoi(yard->text.c_str()));
}
#endif
break;
case lt_string:
constant->char_pointer = new char[yard->text.size() + 1];
strcpy((char*) constant->char_pointer, yard->text.c_str());
break;
case lt_floatnumber:
#if MODERN_COMPILER
constant = new constant_node_t(std::stof(yard->text));
#else
constant->float_value = ((float)atof(yard->text.c_str()));
#endif
break;
default:
throw "Constant not parsed";
}
return;
}
void ExpressionParser::PrepareArguments(call_t * call, expression_node_t * arg)
{
while (arg != nullptr)
{
expression_t * arg_expr = new expression_t(arg->left);
call->argument_frame_size += arg_expr->type->bitsize >> 3;
call->arguments.push_back(arg_expr);
arg = arg->right;
}
}
expression_node_t * ExpressionParser::CheckWord(
namespace_t * space,
std::string name,
bool no_parent_check,
expression_node_t * rvalue,
int line_num)
{
if (line_num == 849)
{
fprintf(stderr, "debug\n");
}
expression_node_t * node = nullptr;
do
{
function_parser * fptr = no_parent_check ? space->FindFunctionInSpace(name) : space->FindFunction(name);
if (fptr != nullptr)
{
call_t * call = new call_t(space, name, nullptr);
PrepareArguments(call, rvalue);
function_overload_t * overload = fptr->FindOverload(call);
node = new expression_node_t(lt_call_method);
node->call = call;
node->type = fptr->type;
break;
}
variable_base_t * var = no_parent_check ? space->FindVariableInspace(name) : space->FindVariable(name);
if (var != nullptr)
{
node = new expression_node_t(lt_variable);
node->variable = var;
node->type = var->type;
break;
}
if (no_parent_check)
{
var = space->TryEnumeration(name, no_parent_check);
if (var != nullptr)
{
node = new expression_node_t(lt_integer);
node->type = space->GetBuiltinType(lt_type_int);
node->is_constant = true;
node->value.integer_value = (333333);
break;
}
space->CreateError(line_num, -77716721, "unparsed statement '%s' ", name.c_str());
}
else
node = space->TryEnumeration(name);
}
while (false);
return node;
}
expression_node_t * ExpressionParser::CreateWordMode(std::string word, shunting_yard_t * parent, expression_node_t * left, expression_node_t * rvalue, int line_num)
{
expression_node_t * node = nullptr;
variable_base_t * var = nullptr;
static namespace_t * space = nullptr;
do
{
if ((parent != nullptr))
{
if (parent->lexem == lt_namescope)
{
return nullptr;
}
// Check varible is a field of structured type
else if ((parent->lexem == lt_dot || parent->lexem == lt_point_to))
{
if (left == nullptr)
{
if (parent->left == nullptr)
throw "Expression StateMachineError";
var = FindVariable(parent->left);
if (var == nullptr)
{
this->space_state->CreateError(line_num, -77716721, "variable '%s' not found", parent->left->text.c_str());
break;
}
type_t * vartyp = var->type;
if ((parent->lexem == lt_point_to) & (vartyp->prop == type_t::pointer_type))
{
vartyp = ((pointer_t*)vartyp)->parent_type;
}
while (vartyp->prop == type_t::typedef_type)
{
vartyp = ((typedef_t*)vartyp)->type;
}
if (vartyp->prop != type_t::compound_type)
{
this->space_state->CreateError(line_num, -77716721, "variable '%s' is not compound", var->name.c_str());
break;
}
node = new expression_node_t(lt_variable);
node->variable = var;
node->type = var->type;
break;
}
if (left->lexem == lt_this)
{
namespace_t * self = this->space_state;
while (self && self->type != namespace_t::spacetype_t::structure_space)
{
self = self->parent;
}
if (self == nullptr)
{
this->space_state->CreateError(line_num, -77716623, "operator 'this' is allowed within methods only", var->name.c_str());
break;
}
node = CheckWord(self, word, true, rvalue, line_num);
break;
}
if (left->lexem == lt_openindex)
{
if (left->type->prop != type_t::compound_type)
{
space_state->CreateError(line_num, -77716721, "unparsed statement '%s' ", word.c_str());
break;
}
typedef structure_t * pstructure_t;
namespace_t * space = pstructure_t(left->type)->space;
node = CheckWord(space, word, true, rvalue, line_num);
break;
}
if (left->lexem == lt_variable)
{
type_t * type = left->variable->type;
while (type->prop == type_t::typedef_type)
type = ((typedef_t*)type)->type;
if (parent->lexem == lt_point_to)
{
#if false
switch (type->prop)
{
case type_t::pointer_type:
type = ((pointer_t*)type)->parent_type;
break;
/* case type_t::compound_type:
fprintf(stderr, "field of compound type\n");
break;
*/
default:
// throw "field operation internal error";
space_state->CreateError(line_num, -77716761, "non-pointer type '%s' ", left->variable->name.c_str());
break;
}
#else
if (type->prop != type_t::pointer_type)
{
space_state->CreateError(line_num, -77716761, "non-pointer type '%s' ", left->variable->name.c_str());
break;
}
type = ((pointer_t*)type)->parent_type;
#endif
}
while (type->prop == type_t::typedef_type)
type = ((typedef_t*)type)->type;
if (type->prop != type_t::compound_type)
{
space_state->CreateError(line_num, -77716762, "unparsed statement '%s' ", word.c_str());
break;
}
typedef structure_t * pstructure_t;
namespace_t * space = pstructure_t(type)->space;
node = CheckWord(space, word, true, rvalue, line_num);
break;
}
if (left->lexem == lt_point_to || left->lexem == lt_dot)
{
type_t * type = left->type;
if (type == nullptr)
throw "FSM error 123";
bool run = true;
int pointer_count = 0;
namespace_t * space = nullptr;
do
{
switch (type->prop)
{
case type_t::pointer_type:
type = ((pointer_t*)type)->parent_type;
++pointer_count;
break;
case type_t::compound_type:
run = false;
space = ((structure_t*)type)->space;
continue;
default:
throw "type error";
}
} while (run);
if (space == nullptr)
{
this->space_state->CreateError(line_num, -77716763, "variable '%s' not found", parent->left->text.c_str());
break;
}
node = CheckWord(space, word, left, rvalue, line_num);
break;
}
else
throw "Parser FSM error: not parsed method call";
}
}
node = CheckWord(space_state, word, false, rvalue, line_num);
if (node == nullptr)
{
this->space_state->CreateError(line_num, -77716921, "Syntax error: Non-recognized statement '%s'", word.c_str());
break;
}
}
while (false);
return node;
}
static namespace_t * GetSpace(shunting_yard_t * yard, namespace_t * space)
{
if (yard->lexem != lt_namescope)
{
space->CreateError(yard->line_number, -7776666, "Internal syntax parser error");
return nullptr;
}
if (yard->right == nullptr || yard->right->lexem != lt_word)
{
space->CreateError(yard->line_number, -7776666, "namespace scope error");
return nullptr;
}
shunting_yard_t * ptr = yard->left;
namespace_t * result = nullptr;
if (ptr == nullptr)
{
result = space;
while (result->parent != nullptr)
result = result->parent;
}
else switch (ptr->lexem)
{
case lt_namescope:
{
result = GetSpace(ptr, space);
if (result == nullptr)
{
space->CreateError(yard->line_number, -7776666, "namespace not found");
break;
}
result = result->FindNamespaceInSpace(ptr->right->text);
break;
}
case lt_word:
{
result = space->FindNamespace(ptr->text);
break;
}
default:
space->CreateError(yard->line_number, -7776666, "Internal syntax parser error");
}
return result;
}
expression_node_t * ExpressionParser::CreateNode(shunting_yard_t * yard, shunting_yard_t * parent, expression_node_t * left)
{
//if (yard->line_number == 664)
//{
// fprintf(stderr, "debug\n");
//}
expression_node_t * node = nullptr;
expression_node_t * lvalue = nullptr;
expression_node_t * rvalue = nullptr;
variable_base_t * var = nullptr;
type_t * cast = nullptr;
function_parser * fptr = nullptr;
if (yard->lexem != lt_namescope)
{
if (yard->left && !yard->unary)
lvalue = CreateNode(yard->left, yard, nullptr);
if (yard->right)
rvalue = CreateNode(yard->right, yard, lvalue);
}
switch (yard->lexem)
{
case lt_character:
{
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(lt_type_char);
node->is_constant = true;
CreateConstant(&node->value, yard);
break;
}
case lt_integer:
{
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(lt_type_int);
node->is_constant = true;
CreateConstant(&node->value, yard);
break;
}
case lt_string:
{
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(lt_type_char);
node->type = new const_t(node->type);
node->type = new pointer_t(node->type);
node->is_constant = true;
CreateConstant(&node->value, yard);
break;
}
case lt_floatnumber:
{
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(lt_type_float);
node->is_constant = true;
CreateConstant(&node->value, yard);
break;
}
case lt_true:
case lt_false:
{
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(lt_type_bool);
node->is_constant = true;
break;
}
case lt_dot:
case lt_point_to:
case lt_indirect_pointer_to_member:
case lt_direct_pointer_to_member:
if (rvalue == nullptr)
{
space_state->CreateError(yard->line_number, -77716122, "field or method '%s' not found", yard->right->text.c_str());
return nullptr;
}
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = rvalue->type;
break;
case lt_word:
{
node = CreateWordMode(yard->text, parent, left, rvalue, yard->line_number);
break;
}
case lt_call:
{
function_parser * function = this->space_state->FindFunction(yard->text);
if (function == nullptr)
{
// Try pointer to function or typedef
variable_base_t * var = this->space_state->FindVariable(yard->text);
if (var == nullptr)
{
this->space_state->CreateError(yard->line_number, -77716721, "function '%s' not declared", yard->text.c_str());
return nullptr;
}
switch (var->type->prop)
{
case type_t::funct_ptr_type:
function = ((function_parser*)var->type);
break;
case type_t::typedef_type:
{
typedef_t * def = (typedef_t*)var->type;
if (def->type->prop == type_t::funct_ptr_type)
{
function = ((function_parser*)def->type);
break;
}
throw "Something wrong at function call";
}
case type_t::pointer_type:
{
pointer_t * ptr = (pointer_t*)var->type;
break;
}
case type_t::unsigned_type:
break;
default:
throw "Unparsed type's property";
}
}
call_t * call = new call_t(this->space_state, function->name, nullptr);
PrepareArguments(call, rvalue);
function_overload_t * overload = function->FindOverload(call);
yard->type = function->type;
node = new expression_node_t(yard->lexem);
node->call = call;
node->type = function->type;
break;
}
case lt_call_method:
{
type_t * type;
if (left == nullptr)
{
if (parent->lexem == lt_point_to || parent->lexem == lt_dot)
{
left = CreateNode(parent->left, nullptr, nullptr);
}
else
throw "State machine error on call nethod";
}
else
printf("Left is not nullptr. Is this ok?");
type = left->type;
while (type->prop == type_t::pointer_type)
type = ((pointer_t*)left->type)->parent_type;
if (type->prop != type_t::compound_type)
{
this->space_state->CreateError(yard->line_number, -77716721, "method cannot call for non compound type '%s'", type->name.c_str());
break;
}
structure_t * structure = (structure_t *)type;
function_parser * function = structure->space->FindFunctionInSpace(yard->text);
if (function == nullptr)
{
this->space_state->CreateError(yard->line_number, -77716721, "method '%s' not found in compound type", yard->text.c_str());
// This is inline method of template class
function = this->space_state->parent->FindFunction(yard->text);
if (function == nullptr)
{
variable_base_t * var = this->space_state->FindVariable(yard->text);
if (var == nullptr)
{
fprintf(stderr, "Last chance to found function is lost\n");
break;
}
type_t * t = var->type;
while (t->prop == type_t::pointer_type)
{
t = ((pointer_t*)t)->parent_type;
}
fprintf(stderr, "Did we found function ptr?\n");
if (t->prop != type_t::funct_ptr_type)
break;
function = (function_parser*)t;
}
if (function == nullptr)
break;
}
call_t * call = new call_t(this->space_state, function->name, nullptr);
PrepareArguments(call, rvalue);
function_overload_t * overload = function->FindOverload(call);
yard->type = function->type;
node = new expression_node_t(yard->lexem);
node->call = call;
node->type = function->type;
break;
}
case lt_set:
case lt_add_and_set:
case lt_sub_and_set:
case lt_mul_and_set:
case lt_div_and_set:
case lt_rest_and_set:
if (rvalue == nullptr)
{
this->space_state->CreateError(yard->line_number, -77771432, "rvalue error ");
break;
}
if (lvalue == nullptr)
{
this->space_state->CreateError(yard->line_number, -77771432, "lvalue error");
break;
}
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = lvalue->type;
break;
case lt_typecasting:
{
node = new expression_node_t(lt_typecasting);
node->type = yard->type;
node->right = rvalue;
}
break;
case lt_this:
node = new expression_node_t(yard->lexem);
node->type = this->space_state->GetBuiltinType(yard->lexem);
break;
case lt_mul:
if (lvalue != nullptr)
{
node = new expression_node_t(lt_mul);
node->left = lvalue;
node->type = rvalue->type;
node->right = rvalue;
}
else
{
// ðàçûìåíîâûâíèå óêàçàòåëÿ
node = new expression_node_t(lt_pointer);
node->type = rvalue->type;
node->right = rvalue;
}
break;
case lt_inc:
case lt_dec:
case lt_logical_not:
case lt_not:
case lt_unary_plus:
case lt_unary_minus:
if (rvalue == nullptr)
{
space_state->CreateError(0, -77770707, "expression error. Possibly duplicated error.");
return nullptr;
}
node = new expression_node_t(yard->lexem);
node->right = rvalue;
node->type = rvalue->type;
break;
case lt_indirect:
if (rvalue == nullptr)
throw "expression state machine error";
if (rvalue->type->prop != type_t::pointer_type)
{
this->space_state->CreateError(yard->line_number, -7777366, "inderection on non-pointer type value" );
return nullptr;
}
node = new expression_node_t(yard->lexem);
node->right = rvalue;
node->type = ((pointer_t*)rvalue->type)->parent_type;
break;
case lt_addrres:
node = new expression_node_t(yard->lexem);
node->right = rvalue;
node->type = new pointer_t(rvalue->type);
break;
case lt_add:
case lt_sub:
case lt_div:
case lt_and:
case lt_or:
case lt_xor:
case lt_rest:
case lt_shift_left:
case lt_shift_right:
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = lvalue->type;
break;
case lt_quest:
node = new expression_node_t(yard->lexem);
switch (lvalue->type->prop)
{
case type_t::pointer_type:
node->left = new expression_node_t(lt_not_eq);
node->left->type = space_state->GetBuiltinType(lt_type_bool);
node->left->left = lvalue;
node->left->right = new expression_node_t(lt_integer);
node->left->right->is_constant = true;
// node->left->right->constant = new constant_node_t;
break;
default:
node->left = lvalue;
break;
}
node->right = rvalue;
node->type = rvalue->type;
break;
case lt_colon:
{
compare_t comp = CompareTypes(lvalue->type, rvalue->type, false, false);
if (comp == no_cast && lvalue->IsConstZero() == true)
{
comp = CompareTypes(rvalue->type, lvalue->type, false, true);
if (comp != no_cast)
lvalue->type = rvalue->type;
}
if (comp == no_cast && rvalue->IsConstZero() == true)
{
comp = CompareTypes(lvalue->type, rvalue->type, false, true);
if (comp != no_cast)
rvalue->type = lvalue->type;
}
if (comp == no_cast)
{
this->space_state->CreateError(yard->line_number, -777790123, "ternary operation types not matched");
break;
}
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = rvalue->type;
break;
}
case lt_namescope:
{
namespace_t * space = GetSpace(yard, this->space_state);
if (space != nullptr)
{
if (yard->right == nullptr || yard->right->lexem != lt_word)
{
this->space_state->CreateError(yard->line_number, -77716921, "rvalue error after ::");
break;
}
node = CheckWord(space, yard->right->text, true, rvalue, yard->line_number);
}
break;
}
case lt_openindex:
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = ((pointer_t*)lvalue->type)->parent_type;
break;
case lt_logical_and:
case lt_logical_or:
case lt_less:
case lt_equally:
case lt_not_eq:
case lt_more:
case lt_less_eq:
case lt_more_eq:
node = new expression_node_t(yard->lexem);
node->left = lvalue;
node->right = rvalue;
node->type = this->space_state->GetBuiltinType(lt_type_bool);
break;
case lt_new:
node = new expression_node_t(yard->lexem);
if (rvalue != nullptr)
{
node->right = rvalue;
node->is_constant = rvalue->is_constant;
}
node->type = yard->type->prop == type_t::dimension_type ? yard->type : new pointer_t(yard->type);
break;