-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzScript.yy
1027 lines (805 loc) · 42 KB
/
zScript.yy
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 "zcode.h"
#define protected public
#include "lex.yy.cpp"
#undef protected
/// enable debug
#define YYDEBUG 1
int yylex(yy::parser::semantic_type *lval, yy::parser::location_type *location);
enum ValueType {
Variant,
Constant
};
Z_USE_NAMESPACE
%}
/// enable debug
%debug
/// enable locations
%locations
/// 开启后,当调用yyerror时,它请求详细的错误消息字符串
%error-verbose
%union{
int valueType;
quint16 count;
QByteArray *identifier;
ZSharedVariantPointer *value;
QVarLengthArray<QByteArray*, 10> *parameterList;
QPair<ZSharedVariantPointer*, quint16> *caseKey;
QVector<QPair<ZSharedVariantPointer*, quint16>> *cases;
std::string *msg;
};
/// keyword
%token VAR FUNCTION NEW DELETE THROW IF ELSE WHILE FOR UNDEFINED GOTO RETURN BREAK CONTINUE SWITCH CASE
%token <count> DEFAULT
/// identifier
%token <identifier> IDENTIFIER INT STRING BOOL DOUBLE
/// == === != !== <= >= && || ++ -- << >>
%token EQ STEQ NEQ STNEQ LE GE LAND LOR ADDSELF SUBSELF LL GG
/// /= *= += -= %= &= |= ^= ||= &&=
%token DIVASSIGN MULASSIGN ADDASSIGN SUBASSIGN MODASSIGN ANDASSIGN ORASSIGN XORASSIGN LANDASSIGN LORASSIGN
%token <msg> ERROR
%token NEW_OBJ_BEGIN
%left ';'
%left VAR IF
%left ','
%right '=' DIVASSIGN MULASSIGN ADDASSIGN SUBASSIGN MODASSIGN ANDASSIGN ORASSIGN XORASSIGN LANDASSIGN LORASSIGN
%left COMMA
%right '?' ':'
%left LAND LOR
%left '&' '|' '^'
%left EQ NEQ STEQ STNEQ
%left '>' '<' GE LE LTGT
%left '-' '+'
%left '*' '/' '%'
%left UMINUS ADDSELF SUBSELF '!' '~'
//%left PROMOTION
%left '.'
%left '[' ']'
%left '(' ')'
%type <valueType> expression lvalue rvalue
%type <count> arguments tuple_exp tuple_lval break loopEnds object_pro
%type <value> branch_head branch_body branch_else const switch_head
%type <parameterList> parameter
%type <cases> cases
%type <caseKey> case
%%
start: | start code
| ERROR {
error(@1, *$1);
delete $1;
YYABORT;
}
;
code: GOTO IDENTIFIER ends {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Goto, ZCodeExecuter::currentCodeExecuter->getGotoLabel(*$2));
delete $2;
}
| conditional
| goto_label
| expression ends {
if(ZCodeExecuter::currentCodeExecuter->getCodeList().count() > 1
&& ZCodeExecuter::currentCodeExecuter->getCodeList().last()->action != ZCode::PopAll)
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PopAll);
}
| return ends {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Goto, ZCodeExecuter::currentCodeExecuter->createConstantByValue(ZVariant(INT32_MAX)));
}
| tuple_lval {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant(QByteArray::number($1), ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::JoinToTuple);
}
'=' expression ends {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LeftAssign);
if(ZCodeExecuter::currentCodeExecuter->getCodeList().count() > 1
&& ZCodeExecuter::currentCodeExecuter->getCodeList().last()->action != ZCode::PopAll)
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PopAll);
}
| loopEnds ends {
/// 判断是否是以break结尾
bool isBreak = !($1 & 0x8000);
auto type = ZCodeExecuter::CodeBlock::Type(ZCodeExecuter::CodeBlock::LoopStructure | ZCodeExecuter::CodeBlock::Switch | ZCodeExecuter::CodeBlock::Normal);
ZCodeExecuter::CodeBlock *block_while = ZCodeExecuter::currentCodeExecuter->getCodeBlockByType(type, ZCodeExecuter::currentCodeExecuter->getCodeBlock());
/// break的个数
qint16 break_count = ($1 & 0x7fff) - !isBreak;
while ((--break_count) > 0) {
if (!block_while) {
error(@1, ("\"break\" Can't be used here"));
YYABORT;
}
block_while = ZCodeExecuter::currentCodeExecuter->getCodeBlockByType(type, block_while->parent);
}
if (!block_while) {
error(@1, ("Can't be used here"));
YYABORT;
}
if (isBreak) {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Goto, block_while->breakIndex);
} else {
/// break_count小于0时说明只存在coutinue语句,因此需要从当前code block查找
block_while = ZCodeExecuter::currentCodeExecuter->getCodeBlockByType(ZCodeExecuter::CodeBlock::LoopStructure,
break_count < 0 ? ZCodeExecuter::currentCodeExecuter->getCodeBlock()
: block_while->parent);
if (!block_while) {
error(@1, "\"continue\" Can't be used here");
YYABORT;
}
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Goto, block_while->toLoopStructureCodeBlock()->continueIndex);
}
}
| switch {}
| ';'
| '{' {
/// For While If Switch-case 语句本身已经添加了对应的CodeBlock,此处的CodeBlock应当不做任何实际作用,也不应该占用break语句
if (ZCodeExecuter::currentCodeExecuter->getCodeBlock()->type == ZCodeExecuter::CodeBlock::Normal
|| ZCodeExecuter::currentCodeExecuter->getCodeBlock()->type == ZCodeExecuter::CodeBlock::Function) {
ZCodeExecuter::currentCodeExecuter->beginCodeBlock();
} else {
ZCodeExecuter::currentCodeExecuter->beginCodeBlock();
ZCodeExecuter::currentCodeExecuter->getCodeBlock()->breakIndex.reset();
}
} start {
if (auto break_index = ZCodeExecuter::currentCodeExecuter->getCodeBlock()->breakIndex.data()) {
*break_index = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
}
ZCodeExecuter::currentCodeExecuter->endCodeBlock();
} '}'
;
ends: ';';
break: BREAK {$$ = 1;}
| break ',' BREAK {$$ = $1 + 1;}
;
/// 循环结构的结束语句, $$的二进制首位为1代表是continue结尾的语句,否则代码break结尾的语句
loopEnds: CONTINUE {$$ = 0x8001;}
| break ',' CONTINUE {$$ = (0x8000 | ($1 + 1));}
| break {$$ = $1;}
;
switch_head:SWITCH '(' expression ')' {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Switch, ZSharedVariantPointer(new ZSharedVariant()));
$$ = &ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode()->value;
ZCodeExecuter::currentCodeExecuter->beginCodeBlock(ZCodeExecuter::CodeBlock::Switch);
}
;
switch: switch_head '{' cases '}' {
*ZCodeExecuter::currentCodeExecuter->getCodeBlock()->breakIndex.data() = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
QHash<ZVariant, int> hashSwitch;
hashSwitch.reserve($3->size());
bool existDefault = false;
for(const QPair<ZSharedVariantPointer*, quint16> &c : *$3) {
if(c.first) {
hashSwitch[*c.first->constData()] = c.second;
} else {
hashSwitch[ZVariant()] = c.second;
existDefault = true;
}
}
if(!existDefault)
hashSwitch[ZVariant()] = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
*$1->data() = QVariant::fromValue(hashSwitch);
ZCodeExecuter::currentCodeExecuter->endCodeBlock();
}
;
case: CASE const ':' {
$$ = new QPair<ZSharedVariantPointer*, quint16>($2, ZCodeExecuter::currentCodeExecuter->getCodeList().count());
}
;
cases: case code {
$$ = new QVector<QPair<ZSharedVariantPointer*, quint16>>();
$$->append(*$1);
delete $1;
}
| cases DEFAULT ':' {
/// 储存code的开始index
$2 = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
} code {
$$ = $1;
$$->append(QPair<ZSharedVariantPointer*, quint16>(Q_NULLPTR, $2));
}
| cases case code {
$$ = $1;
$$->append(*$2);
delete $2;
}
;
goto_label: IDENTIFIER ':' {
*ZCodeExecuter::currentCodeExecuter->getGotoLabel(*$1) = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
delete $1;
}
;
//statement: VAR define {
// }
// | FUNCTION IDENTIFIER '(' define ')' '{' code '}' {
// /// TODO
// }
// ;
function: '(' parameter ')' {
ZCodeExecuter::beginCodeExecuter()->beginCodeBlock(ZCodeExecuter::CodeBlock::Function);
if($2) {
for(QByteArray *id : *$2) {
ZCodeExecuter::currentCodeExecuter->getParameterList() << ZCodeExecuter::currentCodeExecuter->addIdentifier(*id);
delete id;
}
delete $2;
}
} '{' start '}' {
ZCodeExecuter::currentCodeExecuter->endCodeBlock();
ZCodeExecuter *executer = ZCodeExecuter::endCodeExecuter();
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createFunction(executer));
}
;
return: RETURN {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant("", ZVariant::Undefined));
}
| RETURN expression
| RETURN tuple_exp {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant(QByteArray::number($2), ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::JoinToTuple);
}
;
parameter: {$$ = Q_NULLPTR;}
| IDENTIFIER {
$$ = new QVarLengthArray<QByteArray*, 10>();
$$->append($1);
}
| parameter ',' IDENTIFIER {
$$ = $1;
$$->append($3);
}
;
define: IDENTIFIER {
ZCodeExecuter::currentCodeExecuter->addIdentifier(*$1);
delete $1;
}
| IDENTIFIER '=' expression {
ZCodeExecuter::currentCodeExecuter->addIdentifier(*$1);
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::currentCodeExecuter->getIdentifier(*$1));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::RightAssign);
delete $1;
}
| define ',' define
;
/// 添加%prec COMMA是为了保障此文法能优先归并为tuple_lval
tuple_lval: lvalue ',' lvalue %prec COMMA {$$ = 2;}
| tuple_lval ',' lvalue %prec COMMA {$$ = $1 + 1;}
;
tuple_exp: expression ',' expression %prec COMMA {$$ = 2;}
| tuple_exp ',' expression {$$ = $1 + 1;}
;
expression: lvalue | rvalue;
lvalue: VAR define {
$$ = ValueType::Variant;
}
| IDENTIFIER {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::currentCodeExecuter->getIdentifier(*$1));
delete $1;
}
| '_' {
$$ = ValueType::Constant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZSharedVariantPointer(new ZSharedVariant()));
}
| expression '.' IDENTIFIER {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant(*$3, ZVariant::String));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Get);
}
| lvalue '=' expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LeftAssign);
}
| expression '[' expression ']' {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Children);
}
| lvalue ADDASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::AddAssign);
}
| lvalue SUBASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::SubAssign);
}
| lvalue MULASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::MulAssign);
}
| lvalue DIVASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::DivAssign);
}
| lvalue ANDASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::AndAssign);
}
| lvalue ORASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::OrAssign);
}
| lvalue XORASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::XorAssign);
}
| lvalue MODASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::ModAssign);
}
| lvalue LORASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LOrAssign);
}
| lvalue LANDASSIGN expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LAndAssign);
}
| ADDSELF lvalue {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PrefixAddSelf);
}
| SUBSELF lvalue {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PrefixSubSelf);
}
;
const: UNDEFINED {
$$ = new ZSharedVariantPointer(ZCodeExecuter::createConstant(QByteArray(), ZVariant::Undefined));
}
| INT {
$$ = new ZSharedVariantPointer(ZCodeExecuter::createConstant(*$1, ZVariant::Int));
delete $1;
}
| STRING {
$$ = new ZSharedVariantPointer(ZCodeExecuter::createConstant(*$1, ZVariant::String));
delete $1;
}
| DOUBLE {
$$ = new ZSharedVariantPointer(ZCodeExecuter::createConstant(*$1, ZVariant::Double));
delete $1;
}
| BOOL {
$$ = new ZSharedVariantPointer(ZCodeExecuter::createConstant(*$1, ZVariant::Bool));
delete $1;
}
rvalue: const {
$$ = ValueType::Constant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, *$1);
delete $1;
}
| '[' tuple_exp ']' {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant(QByteArray::number($2), ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::JoinToList);
}
| '[' expression ']' {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant("1", ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::JoinToList);
}
| '[' ']' {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant("0", ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::JoinToList);
}
| NEW IDENTIFIER {
/// TODO
$$ = ValueType::Constant;
}
| expression '(' arguments ')' {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, ZCodeExecuter::createConstant(QByteArray::number($3), ZVariant::Int));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Call);
}
| expression '+' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value + *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Add);
}
}
| expression '-' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value - *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Sub);
}
}
| expression '*' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value * *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Mul);
}
}
| expression '/' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value / *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Div);
}
}
| expression '&' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value & *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::And);
}
}
| expression '|' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value | *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Or);
}
}
| expression '^' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value ^ *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Xor);
}
}
| expression '%' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value % *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Mod);
}
}
| expression '>' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value > *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Greater);
}
}
| expression '<' expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value < *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Less);
}
}
| expression EQ expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value == *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::EQ);
}
}
| expression NEQ expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value != *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::NEQ);
}
}
| expression STEQ expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(last_code->value->type() == pre_code->value->type()
&& *last_code->value == *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::STEQ);
}
}
| expression STNEQ expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(last_code->value->type() == pre_code->value->type()
&& *last_code->value != *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::STNEQ);
}
}
| expression LE expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value <= *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LE);
}
}
| expression GE expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value >= *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::GE);
}
}
| expression LAND expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value && *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LAnd);
}
}
| expression LOR expression {
if($1 == ValueType::Constant && $3 == ValueType::Constant) {
$$ = $1;
ValueCode *pre_code = ZCodeExecuter::currentCodeExecuter->getCodeList().takeLast()->toValueCode();
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(*last_code->value || *pre_code->value);
delete pre_code;
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::LOr);
}
}
| '~' expression {
if($2 == ValueType::Constant) {
$$ = $2;
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(~ *last_code->value);
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Contrary);
}
}
| '!' expression {
if($2 == ValueType::Constant) {
$$ = $2;
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(! *last_code->value);
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Not);
}
}
| '-' expression %prec UMINUS {
if($2 == ValueType::Constant) {
$$ = $2;
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(- *last_code->value);
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Minus);
}
}
| '+' expression %prec UMINUS {
if($2 == ValueType::Constant) {
$$ = $2;
ValueCode *last_code = ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode();
last_code->value = ZCodeExecuter::createConstantByValue(+ *last_code->value);
} else {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Abs);
}
}
| lvalue ADDSELF {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PostfixAddSelf);
}
| lvalue SUBSELF {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PostfixSubSelf);
}
| lvalue LL expression {
$$ = ValueType::Variant;
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Append);
}
| function {
$$ = ValueType::Variant;
}
| '(' expression ')' { $$ = $2;}
| object {
$$ = ValueType::Variant;
}
;
arguments: {$$ = 0;}
| expression {$$ = 1;}
| tuple_exp
;
object_pro: IDENTIFIER ':' expression {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, new ZSharedVariant(QString(*$1)));
delete $1;
$$ = 1;
}
| object_pro ',' IDENTIFIER ':' expression {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, new ZSharedVariant(QString(*$3)));
delete $3;
$$ = $1 + 1;
}
;
object: NEW_OBJ_BEGIN '}' {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, new ZSharedVariant(0));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::InitObjectProperty);
}
| NEW_OBJ_BEGIN object_pro '}' {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Push, new ZSharedVariant($2));
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::InitObjectProperty);
}
;
branch_head:IF '(' expression ')' {
$$ = Q_NULLPTR;
ZCodeExecuter::currentCodeExecuter->beginCodeBlock(ZCodeExecuter::CodeBlock::If);
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::If, ZCodeExecuter::currentCodeExecuter->createConstant("", ZVariant::Undefined));
/// 存储if语句判断为假时要跳转到的指令位置
$$ = &ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode()->value;
}
| WHILE {
ZCodeExecuter::currentCodeExecuter->beginCodeBlock(ZCodeExecuter::CodeBlock::While);
} '(' expression ')' {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::If, ZCodeExecuter::currentCodeExecuter->createConstant("", ZVariant::Undefined));
/// 存储if语句判断为假时要跳转到的指令位置
$$ = &ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode()->value;
}
| FOR '(' for_exp ends {
if(ZCodeExecuter::currentCodeExecuter->getCodeList().count() > 1
&& ZCodeExecuter::currentCodeExecuter->getCodeList().last()->action != ZCode::PopAll)
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PopAll);
ZCodeExecuter::currentCodeExecuter->beginCodeBlock(ZCodeExecuter::CodeBlock::NormalFor);
} expression ends {
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::If, ZCodeExecuter::currentCodeExecuter->createConstant("", ZVariant::Undefined));
/// 记录for循环的if指令在codeList中的index,方便修改if指令为假时要跳转到的指令位置
ZCodeExecuter::currentCodeExecuter->getCodeBlock()->toLoopStructureCodeBlock()->ifInstructionIndex = ZCodeExecuter::currentCodeExecuter->getCodeList().count() - 1;
/// 开启使用临时列表储存code
ZCodeExecuter::currentCodeExecuter->setEnableTmpCodeList(true);
} for_exp ')' {
/// 关闭使用临时列表储存code
ZCodeExecuter::currentCodeExecuter->setEnableTmpCodeList(false);
int ifInstructionIndex = ZCodeExecuter::currentCodeExecuter->getCodeBlock()->toLoopStructureCodeBlock()->ifInstructionIndex;
/// 将if语句的ValueCode的值传递到下一层,方便更改if语句判断为假时的跳转位置
$$ = &ZCodeExecuter::currentCodeExecuter->getCodeList().value(ifInstructionIndex)->toValueCode()->value;
}
// | FOR '(' lvalue ':' expression ')' {
// $$ = Q_NULLPTR;
// ZCodeExecuter::currentCodeExecuter->beginCodeBlock(ZCodeExecuter::CodeBlock::While);
// }
;
for_exps: expression | for_exps ',' expression;
for_exp: | for_exps;
branch_body :branch_head code {
if(ZCodeExecuter::currentCodeExecuter->getCodeBlock()->isLoopStructure()) {
QList<ZCode*> &codeList = ZCodeExecuter::currentCodeExecuter->getCodeList();
/// 如果是普通的for循环结构
if(ZCodeExecuter::currentCodeExecuter->getCodeBlock()->type == ZCodeExecuter::CodeBlock::NormalFor) {
QList<ZCode*> &tmpCodeList = ZCodeExecuter::currentCodeExecuter->getTmpCodeList();
/// 记录在for循环中执行continue语句时要跳转到的目标位置
*ZCodeExecuter::currentCodeExecuter->getCodeBlock()->toLoopStructureCodeBlock()->continueIndex.data() = codeList.count();
/// 将for循环的第三个表达式的指令从临时列表添加到codeList
while(!tmpCodeList.isEmpty()) {
codeList << tmpCodeList.takeAt(0);
}
/// 清空栈
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::PopAll);
}
int index = ZCodeExecuter::currentCodeExecuter->getCodeBlock()->beginCodeIndex;
/// 产生循环
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Goto, ZCodeExecuter::currentCodeExecuter->createConstant(QByteArray::number(index), ZVariant::Int));
/// 保存执行break语句时跳转到的位置
*ZCodeExecuter::currentCodeExecuter->getCodeBlock()->breakIndex.data() = codeList.count();
}
int index = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
/// index为if判断结果为假时要跳转到的位置
*$1 = ZCodeExecuter::createConstant(QByteArray::number(index), ZVariant::Int);
$$ = $1;
ZCodeExecuter::currentCodeExecuter->endCodeBlock();
}
;
branch_else:branch_body ELSE;
conditional:branch_body
| branch_else {
/// 如果if判断为真时会执行此goto指令,让其跳转到else代码块后面的语句
ZCodeExecuter::currentCodeExecuter->appendCode(ZCode::Action::Goto,
ZCodeExecuter::createConstant("", ZVariant::Undefined));
int index = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
/// 更改if判断为假时跳转到的位置
*$1 = ZCodeExecuter::createConstant(QByteArray::number(index), ZVariant::Int);
$1 = &ZCodeExecuter::currentCodeExecuter->getCodeList().last()->toValueCode()->value;
} code {
int index = ZCodeExecuter::currentCodeExecuter->getCodeList().count();
/// index为if为真时跳转到的位置
*$1 = ZCodeExecuter::createConstant(QByteArray::number(index), ZVariant::Int);
}
%%
int yyFlexLexer::yywrap()
{
return 1;
}
#undef yyFlexLexer
void yy::parser::error(const location_type& loc, const std::string& msg)
{
char lineStr[1000];
ZCodeExecuter::yyFlexLexer->yyin.seekg(-(int)loc.end.column - 1, std::ios::cur);
ZCodeExecuter::yyFlexLexer->yyin.getline(lineStr, 1000);