-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMareInterpreter.cpp
1310 lines (1160 loc) · 46.8 KB
/
MareInterpreter.cpp
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 "MareInterpreter.h"
namespace mare_vm {
using namespace std;
Blob
MareInterpreter::getFuncInfos() {
JLOG(mutil.j_.trace()) << " * funcInfos size: " << funcInfos.size();
return toBlob(funcInfos);
}
/** 코드 변환 시작 */
void
MareInterpreter::convertToInterCode(vector<string> &src)
{
string errTypeStr = "parsing script error";
try {
convertAll(src);
errTypeStr = "verify syntax error";
chkSyntax();
errTypeStr = "initial execute error";
execute(1);
errTypeStr = "reorganize code error";
updateCode();
}
catch(ErrObj err) {
throw err;
}
catch(TECcodes err_code) {
errorExit(err_code, errTypeStr, "(tec code)");
}
catch(int err_code) {
errorExit(err_code, errTypeStr, "(Error code)");
}
catch(const char* err_msg) {
errorExit(tecUNKNOWN_CONTRACT_ERROR, err_msg);
}
// catch(std::string err_msg) { // 예외를 catch하지 못함??
// JLOG(mutil.j_.error()) << "[Contract Exception String] " << err_msg
// << " -Line: " << getLineNo();
// return tecFAILED_PROCESSING;
// }
catch(std::runtime_error err){
errorExit(tecUNKNOWN_CONTRACT_ERROR, err.what());
}
catch(std::exception err) {
errorExit(tecUNKNOWN_CONTRACT_ERROR, err.what());
}
catch(...){
errorExit(tecUNKNOWN_CONTRACT_ERROR, "Undefined error");
}
}
void
MareInterpreter::convertAll(vector<string> &src)
{
Pc = -1;
endOfFileFlag = false;
srcMaxLineNo = src.size();
srcWork = src;
if (srcMaxLineNo > MAX_LINE)
errorExit(tecEXCEED_CODE_LINES, "프로그램의 허용 코드수 초과");
srcNew.clear();
funcInfos.clear();
srcLineNo = 0;
blkNest = loopNest = 0;
funcDeclFlag = false;
codeBuf_p = codeBuf;
string strInfo;
size_t idx;
// 함수 이름만 먼저 등록
while (token=nextLineTkn(), token.kind != EofProg) {
switch (token.kind) {
case Func:
token = nextTkn(); /* return type */
token = nextTkn(); /* func name */
setSymName(Void); /* 임시값으로 저장 */
enter(tmpTb, funcId); /* 함수 인 경우 */
srcNew.push_back(srcWork[srcLineNo-1]);
strInfo = mutil.trimCopy(srcWork[srcLineNo-1]);
if (strInfo.at(4) == ' ') strInfo.erase(0, 5); // func
else strInfo.erase(0, 9); // function
idx = strInfo.find('{', 10);
if (idx != string::npos)
funcInfos.push_back(strInfo.substr(0, idx));
else funcInfos.push_back(strInfo);
break;
case For: /* for문의 초기값, 검증, 증감조건 순서를 실행시 유리하도록 변경 */
{
strInfo = srcWork[srcLineNo-1];
size_t idx1 = strInfo.find_first_of(';');
if (idx1 == string::npos) errorExit(tecINCORRECT_SYNTAX, "wrong for statement (semicolon not found)");
string newInfo = strInfo.substr(0, idx1);
size_t idx2 = strInfo.find_last_of(';');
size_t idxx = strInfo.find_first_of(';', idx1+1);
if (idx1 == idx2) errorExit(tecINCORRECT_SYNTAX, "wrong for statement(one semicolon found)");
if (idx2 != idxx) errorExit(tecINCORRECT_SYNTAX, "wrong for statement(3 or more semicolons found)");
idx = strInfo.find_last_of(')');
newInfo += strInfo.substr(idx2, (idx -idx2));
newInfo += strInfo.substr(idx1, (idx2-idx1));
newInfo += strInfo.substr(idx);
srcNew.push_back(newInfo);
}
break;
default:
srcNew.push_back(srcWork[srcLineNo-1]);
break;
} // switch
} // while
printInfos(false); // 디버깅용
if (funcInfos.size() == 0) {
JLOG(mutil.j_.warn()) << "호출 가능한 함수가 없습니다.";
}
// 내부 코드로 변환 시작
/* 0번째 행은 필요 없으므로 스크립트 버전으로 채움. (1행부터 시작하도록) */
setCode(Version, MAJOR_VERSION); setCode(Version, MINER_VERSION);
pushInternalCode();
srcWork.clear();
srcWork = srcNew;
srcLineNo = 0; /* Ready to convert */
endOfFileFlag = false;
token = nextLineTkn();
while (token.kind != EofProg) {
convert(); /* 내부 코드로 변환 */
}
pushInternalCode();
}
/**
* line의 맨 앞에 출현하는 코드를 처리.
* 나머지 부분은 convertForRest()등에서 처리됨 */
void
MareInterpreter::convert()
{
JLOG(mutil.j_.trace()) << "* convert: " << kind2Str(token.kind) << " " <<token.text;
switch (token.kind) {
case VarInt:
case VarDbl:
case VarStr:
case VarDateTime:
varDeclare(token.kind); /* 변수 선언 처리 */
break;
case ArrayList:
arrayListDeclare();
break;
case DeclareObj: /* struct 정의 */
structDeclare();
break;
case VarStruct: /* 정의된 struct로부터 변수 선언 */
objectDeclare();
break;
case Func:
chkDo = true; /* 블록 시작 체크를 위한 플래그 설정 */
funcDeclare(); /* 함수 선언 처리 */
break;
case For:
case Foreach:
case While:
++loopNest;
chkDo = true; /* 블록 시작 체크를 위한 플래그 설정 */
convertBlockSet(); /* 블록 내부 처리 */
setCodeEnd(); /* 블록 종료 처리 */
--loopNest;
break;
case If:
chkDo = true; /* 블록 시작 체크를 위한 플래그 설정 */
convertBlockSetIf(); /* if 조건식 및 내부 블록 처리 */
while (token.kind == Else) { /* else of 거나 else 인 경우가 있을 수 있음 */
chkDo = true; /* 블록 시작 체크를 위한 플래그 설정 */
Token cc = nextTkn(); /* 다음이 if인지 '{'인지 등에 따른 처리를 위해, 임시 변수로 저장 */
if (cc.kind == If) { /* else if 인 경우 처리 */
token = Elif;
convertBlockSetIf(); /* else if 조건식 및 내부 블록 처리 */
}
else if (cc.kind == Do) { /* '{'로 시작하는 경우 처리 */
chkDo = false; /* 블록 시작 체크 완료됨 */
break;
}
else if (cc.kind == EofLine) break;
else errorExit(tecINCORRECT_SYNTAX, "wrong else if format");
}
if (token.kind == Else){ /* else 인 경우 처리 */
convertBlockSet(); /* 블록 내부 처리 */
setCodeEnd(); /* 블록 종료 처리 */
}
break;
case Break:
case Continue:
if (loopNest <= 0)
errorExit(tecINCORRECT_SYNTAX, "잘못된 break/continue입니다.");
setCode(token.kind);
token = nextTkn();
convertForRest();
break;
case Return:
if (!funcDeclFlag)
errorExit(tecINCORRECT_SYNTAX, "잘못된 return입니다.");
setCode(token.kind);
token = nextTkn();
convertForRest();
break;
case Throws:
token = nextTkn();
if (token.kind == IntNum) {
if (token.numVal < 1)
errorExit(tecINCORRECT_SYNTAX, "wrong error code");
setCode(Throws, token.numVal);
}
else if (token.kind == Ident && token.text.find("tec") == 0){
auto x = transCode(token.text);
setCode(Throws, x);
}
else errorExit(tecINCORRECT_SYNTAX, "잘못된 throw 형식입니다.");
token = nextTkn();
convertForRest();
break;
case Require:
case Exit:
case Log:
case ToArray:
setCode(token.kind);
token = nextTkn();
convertForRest();
break;
case System:
case Math:
case Do:
case Close:
case End:
case GetProperty:
case SetProperty:
// 단독으로 사용할 수 없는 경우
errorExit(tecINCORRECT_SYNTAX, "잘못된 " + kind2Str(token.kind) + " 위치입니다.");
break;
default: convertForRest();
break;
}
}
/** 코드 라인의 중간에 올수 없는 키워드인지 확인 */
void
MareInterpreter::chkKinds(Token const& tk) {
switch (tk.kind) {
case VarInt: case VarDbl: case VarStr:
case VarDateTime: case VarStruct:
case ArrayList: case DeclareObj:
case Func: case End:
case If: case Elif: case Else:
case For: case Foreach: case While:
case Break: case Continue: case Return:
case Require: case Exit: case Throws:
case Log:
case ToArray:
/* 이 키워드가 도중에 나타나는 일은 없음 */
throw tecINCORRECT_SYNTAX;
default:
break;
}
}
/** 문의 나머지 부분 처리 */
void
MareInterpreter::convertForRest()
{
for (;;) {
JLOG(mutil.j_.trace()) << " convertForRest() -token:" << kind2Str(token.kind);
if (token.kind == EofLine) break;
chkKinds(token);
switch (token.kind) {
case Ident: /* 함수 호출, 혹은 변수 */
{
short tblIdx;
bool isGvar = false;
bool isVarInArr = false;
if ((tblIdx=searchName(token.text, 'G')) != -1) { /* Global symbol로 등록되어 있는지 확인 */
if (Gtable[tblIdx].symKind==funcId) {
setCode(Fcall, tblIdx);
break;
}
else {
setCode(Gvar, tblIdx);
isGvar = true;
}
}
else if ((tblIdx=searchName(token.text, 'L')) == -1) /* Local symbol로 등록되어 있는지 확인 */
errorExit(tecNO_VARIABLE, "This token is not declared.");
else
setCode(Lvar, tblIdx);
token = nextTkn();
if (token.kind == Lbracket) { convertArrIdx(); isVarInArr=true; }
if (token.kind == Dot) convertIdent(tblIdx, isGvar, isVarInArr);
continue;
}
case IntNum:
case DblNum:
setCode(token.kind, setLITERAL(token.numVal)); /* 정수도 double형으로 저장 */
break;
case String:
setCode(token.kind, setLITERAL(token.text));
break;
case System:
case Math:
{
TknKind tp = token.kind;
token = nextTkn(); token = chkNextTkn(token, '.');
setCode(tp, mutil.getIdx(tp, token.text));
break;
}
case Do:
if (chkDo) { chkDo = false; } /* 블록의 시작이 '{'인 경우 */
else errorExit(tecINCORRECT_SYNTAX, "wrong curly brackets");
break;
default: /* '+', '-', '<=' 등 처리 */
setCode(token.kind);
break;
}
token = nextTkn();
}
JLOG(mutil.j_.trace()) << "convertForRest():save";
pushInternalCode();
token = nextLineTkn();
}
/**
* 변수 선언과 동시에 Assign(=)이 있을 경우, 문의 나머지 처리
* (함수 파라미터의 초기값이 정의된 경우, literalOnly=true) */
void
MareInterpreter::convertVarAssign(bool literalOnly)
{
bool inBracket = false;
short bracketLevel = 0;
for (;;) {
JLOG(mutil.j_.trace()) << " convertVarAssign() subAssign:" << kind2Str(token.kind);
if (literalOnly) {
if (token.kind == Comma || token.kind == ')') return;
}
if (token.kind == EofLine) {
if (inBracket) errorExit(tecINCORRECT_BLACKET);
return;
}
else if (token.kind == Comma && !inBracket) return;
else if (token.kind == ')') {
--bracketLevel;
if (bracketLevel == 0) inBracket = false;
if (bracketLevel < 0) errorExit(tecINCORRECT_BLACKET);
}
else chkKinds(token);
switch (token.kind) {
case Do: case Close:
errorExit(tecINCORRECT_SYNTAX);
break;
case Ident: /* 함수 호출, 혹은 변수 */
{
if (literalOnly) errorExit(tecNEED_LITERAL_TYPE,
"Only literal values are allowed to initialize function arguments.");
short tblIdx;
bool isGvar = false;
bool isVarInArr = false;
if ((tblIdx=searchName(token.text, 'G')) != -1) { /* Global symbol로 등록되어 있는지 확인 */
if (Gtable[tblIdx].symKind==funcId) {
setCode(Fcall, tblIdx);
break;
}
else {
setCode(Gvar, tblIdx);
isGvar = true;
}
}
else if ((tblIdx=searchName(token.text, 'L')) == -1) /* Local symbol로 등록되어 있는지 확인 */
errorExit(tecNO_VARIABLE, "This token is not declared.");
else
setCode(Lvar, tblIdx);
token = nextTkn();
if (token.kind == Lbracket) { convertArrIdx(); isVarInArr=true; }
if (token.kind == Dot) convertIdent(tblIdx, isGvar, isVarInArr);
continue;
}
case IntNum: case DblNum: /* 정수도 double형으로 저장 */
setCode(token.kind, setLITERAL(token.numVal));
break;
case String:
setCode(token.kind, setLITERAL(token.text));
break;
case System:
{
if (literalOnly) errorExit(tecNEED_LITERAL_TYPE,
"Only literal values are allowed to initialize function arguments.");
TknKind tp = token.kind;
token = nextTkn(); token = chkNextTkn(token, '.');
setCode(tp, mutil.getIdx(tp, token.text));
break;
}
case Math:
{
if (literalOnly) errorExit(tecNEED_LITERAL_TYPE,
"Only literal values are allowed to initialize function arguments.");
TknKind tp = token.kind;
token = nextTkn(); token = chkNextTkn(token, '.');
setCode(tp, mutil.getIdx(tp, token.text));
break;
}
case True: case False: case Minus:
setCode(token.kind);
break;
default: /* '+', '-', '<=' 등 처리 */
if (literalOnly) errorExit(tecNEED_LITERAL_TYPE,
"Only literal values are allowed to initialize function arguments.");
setCode(token.kind);
if (token.kind == '(') { inBracket = true; ++bracketLevel; }
break;
}
token = nextTkn();
}
}
/** 변수의 속성 정보나 기타 등등 */
void
MareInterpreter::convertIdent(short symidx, bool isGvar, bool objInArr)
{
JLOG(mutil.j_.trace()) << "convertIdent " << symidx << " " << isGvar << " " << objInArr;
//setCode(Dot);
token = nextTkn();
// struct인 경우,
// 일반 변수인 경우
// array or arraylist인 경우 -> struct인지? 일반 변수인지?
bool isProperty = true;
SymKind sk = noId;
DtType dt = NON_T;
unsigned short targetSymIdx = symidx;
if (isGvar) {
sk = Gtable[symidx].symKind;
dt = Gtable[symidx].dtTyp;
}
else {
sk = Ltable[symidx].symKind;
dt = Ltable[symidx].dtTyp;
}
if (sk == objectId) {
isProperty = false;
}
else if (sk == arrayId || sk == arrListId) {
if (objInArr && dt == OBJECT_T) {
isProperty = false;
if (isGvar)
targetSymIdx = Gtable[symidx].ref;
else
targetSymIdx = Ltable[symidx].ref;
}
}
// struct object의 특정 변수인 경우
if (!isProperty) {
JLOG(mutil.j_.trace()) << " -- isStructItem: " << token.text;
// symidx 기반으로 어떤 item인지 찾아서 저장
short itemIdx = -1;
for (ItemTbl it : Itable) {
if (it.symId == targetSymIdx && it.name == token.text) {
itemIdx = it.offset;
break;
}
}
if (itemIdx ==-1) errorExit(tecINCORRECT_SYNTAX, "Not found");
//setCode(Dot);
setCode(StructItem, itemIdx);
// 다음에 변수의 속성 값이 있을 경우 처리
token = nextTkn();
if (token.kind == Dot) {
token = nextTkn();
isProperty = true;
}
}
if (isProperty) {
JLOG(mutil.j_.trace()) << " -- isProperty: " << token.text;
setCode(Dot);
TknKind Property;
short tblIdx = mutil.getPropertyIdx(token.text, Property);
setCode(Property, tblIdx);
token = nextTkn();
}
}
/** 배열의 인덱스관련 항목 처리 */
void
MareInterpreter::convertArrIdx()
{
JLOG(mutil.j_.trace()) << "convertArrIdx";
setCode(token.kind);
token = nextTkn();
for (;;) {
JLOG(mutil.j_.trace()) << " convertArrIdx() -token:" << kind2Str(token.kind);
if (token.kind == Rbracket) {
setCode(token.kind);
token = nextTkn();
return;
}
if (token.kind == EofLine)
errorExit(tecINCORRECT_SYNTAX, "wrong brackets");
chkKinds(token);
switch (token.kind) {
case Ident: /* 함수 호출, 혹은 변수 */
{
short tblIdx;
bool isGvar = false;
bool isVarInArr = false;
if ((tblIdx=searchName(token.text, 'G')) != -1) { /* Global symbol로 등록되어 있는지 확인 */
if (Gtable[tblIdx].symKind==funcId) {
setCode(Fcall, tblIdx);
break;
}
else {
setCode(Gvar, tblIdx);
isGvar = true;
}
}
else if ((tblIdx=searchName(token.text, 'L')) == -1) /* Local symbol로 등록되어 있는지 확인 */
errorExit(tecNO_VARIABLE, "This token is not declared.");
else
setCode(Lvar, tblIdx);
token = nextTkn();
if (token.kind == Lbracket) { convertArrIdx(); isVarInArr=true; }
if (token.kind == Dot) convertIdent(tblIdx, isGvar, isVarInArr);
continue;
}
case IntNum:
case DblNum:
setCode(token.kind, setLITERAL(token.numVal)); /* 정수도 double형으로 저장 */
break;
// case String:
// setCode(token.kind, setLITERAL(token.text));
// break;
case Math:
{
TknKind tp = token.kind;
token = nextTkn(); token = chkNextTkn(token, '.');
setCode(tp, mutil.getIdx(tp, token.text));
break;
}
case Plus: case Minus: case Multi: case Divi:
case Mod:
case DBPlus: case DBMinus: case DBPlusR: case DBMinusR:
setCode(token.kind);
break;
default:
errorExit(tecINCORRECT_SYNTAX, "wrong brackets");
break;
}
token = nextTkn();
}
}
/** 블록 처리 관리 (if, else if 제외) */
void
MareInterpreter::convertBlockSet()
{
JLOG(mutil.j_.trace()) << " convertBlockSet() ... " << kind2Str(token.kind);
short patch_line;
TknKind kind = token.kind;
patch_line = setCode(token.kind, NO_FIX_ADRS); /* 블록의 끝 line 정보를 임시 값으로 할당 */
token = nextTkn();
convertForRest(); /* 조건문 처리 */
convertBlock(); /* 실제 블록 처리 */
JLOG(mutil.j_.trace()) << " end block:" << kind2Str(kind) << " s_line:" << patch_line
<< " n_line:" << getLineNo();
backPatch(patch_line, getLineNo()); /* 블록의 끝 line 정보를 수정 (end행 번호) */
}
/** if, else if 블록 처리 관리 (마지막에 else로 종료되지 않으면, End code를 추가) */
void
MareInterpreter::convertBlockSetIf()
{
convertBlockSet(); /* 조건문 및 블록 처리 */
token = nextTkn();
if (token.kind == EofLine){ /* 블록 끝난 후, 처리를 위한 다음 line의 토큰 확인 */
token = nextLineTkn();
if (token.kind != Else) setCode(End); /* if 관련 종료를 위해서는 블록 끝에 end를 삽입해야 됨. */
else setCode(Close); /* Close처리를 EofLine로 변경가능하나 실행 및 검증에 대한 수정 필요함. */
pushInternalCode();
}
else if (token.kind != Else){
errorExit(tecINCORRECT_SYNTAX, "잘못된 형식입니다. (else 혹은 라인의 끝이어야 합니다.)", kind2Str(token.kind) );
}
}
/** 블록 내부 처리 (블록의 끝까지 문을 분석) */
void
MareInterpreter::convertBlock()
{
TknKind k;
++blkNest; /* 블록 내부로 들어옴에 따른 블록 레벨값 증가 */
if (chkDo){ /* '{' 로 시작하는지 확인 필요함. */
if (token.kind == Do){ /* '{' 로 시작하는 경우 처리 */
token = nextTkn();
chkDo = false;
if (token.kind == EofLine){ /* 현재 line에 '{'만 존재하는 경우 처리. */
//setCode(Do); /* 굳이 '{'를 추가할 필요가 없을 듯 */
convertForRest(); /* 현재 line 저장 및 다음 line으로 전환 */
}
}
else errorExit(tecNEED_OPEN_BLOCK, "블록 열기 에러: ", token.text);
}
while(k=token.kind, k!=Close) { /* '}'가 나올때까지 블록 내부 처리. */
JLOG(mutil.j_.trace()) << " ----- lvl[" << to_string(blkNest) << "] block line start ";
if (k==EofProg || k==Elif || k==Else || k==End || k==Func){
errorExit(tecNEED_CLOSE_BLOCK, "블록 닫음 에러: ", token.text);
}
convert(); /* 블록 내부의 명령행들을 처리 */
}
--blkNest; /* 블록이 끝남에 따른 블록 레벨값 감소 */
}
/** 변수 선언 처리 */
void
MareInterpreter::varDeclare(short const varType)
{
for (;;) {
token = nextTkn();
JLOG(mutil.j_.trace()) << " * new variable:" << kind2Str((TknKind)varType) << " " << token.text;
chkVarName(token); /* 이름 검사 */
setSymName(varType); /* 변수 등록에 사용될 SymTbl 셋팅 */
setSymAryLen(); /* 배열이면 길이 정보 설정 */
short tblNb = enter(tmpTb, varId); /* 변수등록 (주소도 등록) */
if (tmpTb.aryLen > 1) {
if (token.kind == '=') errorExit(tecINVALID_ASSIGN, "Array cannot be initialized by assignment.");
setCode(DeclareArr);
}
else setCode(DeclareVar);
if (isLocalVarName(tmpTb.name, varId))
setCode(Lvar, tblNb);
else
setCode(Gvar, tblNb);
if (token.kind == '=') { /* 선언 후, 초기값이 할당되는지 여부에 따른 처리 */
setCode('='); /* '=' (Assign) 코드 추가 */
token = nextTkn();
JLOG(mutil.j_.trace()) << " var Assign(=) " << kind2Str(token.kind) << ":" << token.text;
convertVarAssign(); /* 할당할 값 관련 내용 추가 */
}
if (token.kind != ',') break; /* ',' 이면, 인수가 계속됨 */
setCode(',');
}
setCodeEofLine();
}
/** 새로운 변수 등록을 위한 이름 확인 */
void
MareInterpreter::chkVarName(Token const& tk)
{
if (tk.kind != Ident)
errorExit(tecINVALID_NAME, "Duplicated identifier: ", tk.text);
char chkTp = isLocalScope() ? 'V' : 'A';
if (searchName(tk.text, chkTp) != -1)
errorExit(tecINVALID_NAME, "Duplicated variable name: ", tk.text);
}
/**
* 등록할 SymTbl의 이름 및 PermissionType, DataType 설정 (함수일 경우, 리턴 타입)
* 현재토큰이 다음토큰으로 변경됨 */
void
MareInterpreter::setSymName(short const dtType)
{
if (token.kind != Ident)
errorExit(tecINVALID_NAME, "Duplicated identifier: ", token.text);
if (dtType < Void || dtType > VarStruct)
errorExit(tecINCORRECT_SYNTAX, "Duplicated type identifier: ", kind2Str((TknKind)dtType));
tmpTb.clear();
tmpTb.name = token.text; /* 이름 설정 */
tmpTb.dtTyp = (DtType)dtType; /* 타입 설정 (함수일 경우, 리턴 타입) */
token = nextTkn();
}
/** 배열 크기 설정 */
void
MareInterpreter::setSymAryLen()
{
tmpTb.aryLen = 0;
if (token.kind != '[') return; /* 배열이 아닌 경우 */
token = nextTkn();
if (token.kind != IntNum)
errorExit(tecNEED_UNSIGNED_INTEGER, "The index value of array must be a positive integer only.: ", token.text);
tmpTb.aryLen = (int)token.numVal; /* int a[5]는 첨자 0~4가 유효함 */
if (MAX_ARRAY < tmpTb.aryLen)
errorExit(tecEXCEED_ARRAY_LENGTH, "The size of the array has been exceeded.");
token = chkNextTkn(nextTkn(), ']');
if (token.kind == '[')
errorExit(tecBAD_MULTI_DIMENTION_ARRAY, "Multi-dimensional arrays cannot be declared.");
tmpTb.symKind = arrayId;
}
void
MareInterpreter::arrayListDeclare()
{
JLOG(mutil.j_.trace()) << "*** arrayList declear ***";
if (blkNest > 0 || funcDeclFlag)
errorExit(tecINCORRECT_SYNTAX, "Struct declaration location is invalid.");
token = nextTkn();
token = chkNextTkn(token, Less);
TknKind varType = token.kind;
int items = 0;
unsigned short objIdx = NOT_DEFINED_ARRAY;
if (varType == VarStruct) {
objIdx = token.numVal;
for(ItemTbl it : Itable) {
if (it.symId == objIdx) items++;
}
if (items == 0) errorExit(tecINCORRECT_SYNTAX, "un-defind struct");
}
token = nextTkn();
token = chkNextTkn(token, Great);
JLOG(mutil.j_.trace()) << " * new arrayList type:" << kind2Str((TknKind)varType) << " " << token.text;
chkVarName(token); /* 이름 검사 */
setSymName(varType); /* 변수 등록에 사용될 SymTbl 셋팅 */
//setSymAryLen(); /* 길이 정보 설정 */
tmpTb.aryLen = NOT_DEFINED_ARRAY; /* 초기 배열 크기: 65535(== 0) */
tmpTb.symKind = arrListId;
if (items > 0) tmpTb.frame = items;
if (objIdx != NOT_DEFINED_ARRAY) tmpTb.ref = objIdx;
short tblNb = enter(tmpTb, varId); /* 변수등록 (주소도 등록) */
setCodeEofLine();
}
void
MareInterpreter::structDeclare()
{
JLOG(mutil.j_.trace()) << "*** struct declear ***";
if (blkNest > 0 || funcDeclFlag)
errorExit(tecINCORRECT_SYNTAX, "Struct declaration location is invalid.");
token = nextTkn();
int objectIdx = ObjectMap.size();
if (objectIdx > 0) {
auto m_iter = ObjectMap.find(token.text);
if (m_iter != ObjectMap.end())
errorExit(tecINVALID_NAME);
}
ObjectMap.insert(make_pair(token.text, objectIdx));
JLOG(mutil.j_.trace()) << " * new struct: "<< token.text << " " << objectIdx
<< " " << ObjectMap.size();
token = nextTkn();
setCodeEofLine(true);
if (token.kind == Do) {
token = nextTkn();
setCodeEofLine();
}
short itemCnt = 0;
while (token.kind != Close) {
if (token.kind < VarDbl || token.kind > VarDateTime)
errorExit(tecINCORRECT_SYNTAX, "wrong struct item type");
iTb.clear();
iTb.symId = objectIdx;
iTb.dtTyp = (DtType)token.kind;
iTb.offset = itemCnt++;
token = nextTkn();
if (token.kind != Ident)
errorExit(tecINVALID_NAME, "Duplicated identifier: ", token.text);
iTb.name = token.text;
token = nextTkn();
if (token.kind == '='){
token = nextTkn();
iTb.initTyp = iTb.dtTyp;
if (iTb.dtTyp == STR_T) {
if (token.kind == String) iTb.initStr = token.text;
else errorExit(tecINVALID_ASSIGN, "Need string literal");
}
else {
if (token.kind == IntNum || token.kind == DblNum )
iTb.initVal = token.numVal;
else errorExit(tecINVALID_ASSIGN, "Need numeric literal");
}
token = nextTkn();
}
setCodeEofLine();
Itable.push_back(iTb);
}
token = nextTkn();
setCodeEofLine();
printInfos();
}
void
MareInterpreter::objectDeclare()
{
// 아이템 수
unsigned short objIdx = token.numVal;
int items = 0;
for(ItemTbl it : Itable) {
if (it.symId == objIdx) items++;
}
token = nextTkn();
JLOG(mutil.j_.trace()) << " * new object:" << token.text << " " << objIdx;
chkVarName(token); /* 이름 검사 */
setSymName(VarStruct); /* 변수 등록에 사용될 SymTbl 셋팅 */
setSymAryLen(); /* 길이 정보 설정 */
if (tmpTb.aryLen == 0) {
tmpTb.symKind = objectId;
tmpTb.aryLen = 1;
}
tmpTb.frame = items;
tmpTb.ref = objIdx;
short tblNb = enter(tmpTb, varId); /* 변수등록 (주소도 등록) */
setCode(VarStruct);
if (isLocalVarName(tmpTb.name, varId))
setCode(Lvar, tblNb);
else
setCode(Gvar, tblNb);
setCodeEofLine();
}
/** 함수 정의 */
void
MareInterpreter::funcDeclare()
{
int tblIdx, patch_line, fncTblIdx;
if (blkNest > 0 || funcDeclFlag)
errorExit(tecINCORRECT_SYNTAX, "Function declaration location is invalid.");
funcDeclFlag = true; /* 함수 처리 시작 확인 플래그 */
localAdrs = 0; /* 로컬 영역 할당 카운터 초기화 */
setStartLtable(); /* 로컬 심볼 테이블의 시작 위치 설정 */
patch_line = setCode(Func, NO_FIX_ADRS); /* 나중에 블록의 끝(end)의 행 번호를 넣기 위해, 임시 값 할당으로 공간 확보 */
token = nextTkn(); /* return type */
short funcRtnTp = token.kind; /* return type 정보 보관 */
token = nextTkn(); /* func name */
fncTblIdx = searchName(token.text, 'F'); /* 함수명은 이미 등록되어 있기 때문에 함수명 검색을 함 */
Gtable[fncTblIdx].dtTyp = (DtType)funcRtnTp; /* 함수의 리턴 타입 설정 */
// 주석과 같이 불필요한 코드가 제거된 경우...
Gtable[fncTblIdx].adrs = getLineNo(); /* 함수의 주소 업데이트 */
bool paramAssign = false; /* parameter Assign 순서 검증용 */
// 가인수 분석
token = nextTkn(); // '('
token = chkNextTkn(token, '('); /* '(' 로 시작하는지 확인 */
setCode('(');
if (token.kind != ')') { /* 인수가 있는지 여부 확인 */
short minArgs = 0;
for (;; token = nextTkn()) {
int ttk = token.kind; /* param type */
token = nextTkn(); /* param name */
setSymName(ttk);
setSymAryLen(); /* 배열이면 길이 정보 설정 */
tblIdx = enter(tmpTb, paraId); /* 인수 등록 */
setCode(Lvar, tblIdx); /* 인수는 Lvar로서 처리됨 */
if (tmpTb.aryLen == 0)
++Gtable[fncTblIdx].args; /* 인수 개수를 +1 */
else
errorExit(tecINVALID_FUNC_ARGUMENT, "function param type");
if (token.kind == '=') { /* '=' 이면, 함수 파라미터 초기화 처리 */
setCode('='); /* '=' (Assign) 코드 추가 */
token = nextTkn();
JLOG(mutil.j_.trace()) << " var Assign(=) " << kind2Str(token.kind) << ":" << token.text;
convertVarAssign(true); /* 할당할 값 관련 내용 추가 */
minArgs++;
paramAssign = true;
}
else if (paramAssign)
errorExit(tecINVALID_FUNC_ARGUMENT, "wrong function param Assign(=)");
if (token.kind != ',') break; /* ',' 이면, 인수가 계속됨 */
setCode(',');
}
if (paramAssign) /* 최소 인수 개수 */
Gtable[fncTblIdx].aryLen = Gtable[fncTblIdx].args - minArgs;
else Gtable[fncTblIdx].aryLen = Gtable[fncTblIdx].args;
}
token = chkNextTkn(token, ')'); /* ')' 로 끝나는지 확인 */
setCode(')');
setCodeEofLine(true); /* '{'가 있는 경우도 처리됨 */
convertBlock(); /* 함수 본체 처리 */
backPatch(patch_line, getLineNo()); /* 블록의 끝으로 jump 할수있도록 블록의 끝(end)의 행 번호로 수정 */
setCodeEnd();
Gtable[fncTblIdx].frame = localAdrs; /* 프레임 크기 (함수에서 선언된 로컬 변수 개수) */
funcDeclFlag = false; /* 함수 처리 종료 */
}
/** line행에 n을 설정 */
void
MareInterpreter::backPatch(short line, short endLine)
{
*SHORT_P(intenalCode[line] + 2) = (short)endLine; // skip INTERCODE_LEN (+1)
}
/** end 인지 확인하고, 저장 처리 */
void
MareInterpreter::setCodeEnd()
{
JLOG(mutil.j_.trace()) << "setCodeEnd() : " << kind2Str(token.kind);
if (token.kind != End && token.kind != Close)
errorExit(tecNEED_CLOSE_BLOCK, "'}' 가 ", token.text + " 앞에 필요합니다.");
setCode(End);
token = nextTkn();
setCodeEofLine();
}
/**
* line의 끝인지 확인하고, Code 저장 처리
* (DoEnable이 true일 경우, '{'로 끝나는 경우에 대해서도 처리 가능}) */
void
MareInterpreter::setCodeEofLine(bool DoEnable)
{
JLOG(mutil.j_.trace()) << "setCodeEofLine(" << DoEnable << ") : " << kind2Str(token.kind);
if (DoEnable && token.kind == Do){
//setCode(Do); Do는 실제로 사용 안되므로 생략함
chkDo = false;
token = nextTkn();
}
if (token.kind != EofLine) errorExit(tecNEED_LINE_END, "Require the end of the line.");
pushInternalCode();
token = nextLineTkn(); /* 다음 행으로 진행 */
}
/** 함수 내부 블록의 코드를 처리 중인지 확인 */
bool
MareInterpreter::isLocalScope()
{