forked from code4fukui/DNCL3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWirth.js
1241 lines (1230 loc) · 37.2 KB
/
Wirth.js
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
const BLACKET_MODE = false;
const reserved = [
"print",
//"input",
"if", "else", "elseif", "endif",
"for", "to", "step", "next", "while", "do", "until", "break",
"function", "return", "end",
"and", "or", "not",
];
const isNumber = (c) => "0123456789".indexOf(c) >= 0;
const isOperator = (c) => "+-*/%=!<>,".indexOf(c) >= 0;
const isUpperAlphabet = (c) => "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c) >= 0;
const isConstantName = (s) => {
for (const c of s) {
if (!isUpperAlphabet(c) && c != "_") return false;
}
return true;
};
const MAX_LOOP = 1000;
class Return {
constructor(val) {
this.val = val;
}
getValue() {
return this.val;
}
}
class Break {
}
class Scope {
constructor(parent = null) {
this.vars = {};
this.parent = parent;
}
isDefined(name) {
for (let scope = this; scope; scope = scope.parent) {
if (scope.vars[name] !== undefined) {
return true;
}
}
return false;
}
getVar(name) {
for (let scope = this; scope; scope = scope.parent) {
if (scope.vars[name] !== undefined) {
return scope.vars[name];
}
}
//throw new Error("定義されていない変数 " + name + " が使われました");
throw new Error("undefined var " + name + " is used");
}
setVar(name, o, forcelocal = false) {
if (!forcelocal) {
for (let scope = this; scope; scope = scope.parent) {
if (scope.vars[name] !== undefined) {
scope.vars[name] = o;
return;
}
}
}
this.vars[name] = o;
}
}
export class Wirth {
constructor(s, callbackoutput) {
this.s = s.replaceAll("\r", "");
this.vars = {};
this.callbackoutput = callbackoutput;
//this.parseTokens();
this.parse();
}
output(s) {
if (this.callbackoutput) {
this.callbackoutput(s);
} else {
console.log(s);
}
}
getChar() {
const res = this.s[this.p];
this.p++;
return res;
}
getToken(reteol = false) {
const STATE_FIRST = 0;
const STATE_WORD = 1;
const STATE_STRING = 2;
const STATE_COMMENT = 3;
const STATE_COMMENT_SINGLE = 4;
const STATE_COMMENT_MULTI = 5;
const STATE_COMMENT_MULTI2 = 6;
const STATE_NUMBER = 7;
const STATE_OPERATOR = 8;
let state = STATE_FIRST;
const res = [];
const pos = this.p;
for (;;) {
const c = this.getChar();
if (state == STATE_FIRST) {
if (reteol && c == "\n") {
return { pos, type: "eol" };
} else if (c == " " || c == "\t" || c == "\n") {
continue;
} else if (c === undefined) {
return { pos, type: "eof" };
} else if (c == "#") {
state = STATE_COMMENT;
} else if (c == "{" || c == "}" || c == "(" || c == ")" || c == "[" || c == "]") {
return { pos, type: c };
} else if (c == '"') {
state = STATE_STRING;
} else if (isNumber(c) || c == ".") {
res.push(c);
state = STATE_NUMBER;
} else if (isOperator(c)) {
res.push(c);
state = STATE_OPERATOR;
} else {
res.push(c);
state = STATE_WORD;
}
} else if (state == STATE_WORD) {
if (c == " " || c == "\t" || c == "\n" || isOperator(c) || c == "(" || c == ")" || c == "[" || c == "]" || c === undefined) {
this.p--;
const w = res.join("");
if (reserved.indexOf(w) >= 0) {
return { pos, type: w };
} else {
return { pos, type: "var", name: w };
}
} else {
res.push(c);
}
} else if (state == STATE_STRING) {
if (c == '"') {
const w = res.join("");
return { pos, type: "string", value: w };
} else if (c == "\n" || c === undefined) {
//throw new Error("文字列が閉じられていません");
throw new Error(`string is not closed with '"'`);
} else {
res.push(c);
}
} else if (state == STATE_COMMENT) {
if (c == "=") {
state = STATE_COMMENT_MULTI;
} else {
this.p--;
state = STATE_COMMENT_SINGLE;
}
} else if (state == STATE_COMMENT_SINGLE) {
if (c == "\n") {
return { pos, type: "eol" };
} else if (c === undefined) {
return { pos, type: "eof" };
}
} else if (state == STATE_COMMENT_MULTI) {
if (c == "=") {
state = STATE_COMMENT_MULTI2;
} else if (c === undefined) {
return { pos, type: "eof" };
}
} else if (state == STATE_COMMENT_MULTI2) {
if (c == "#") {
return { pos, type: "eol" };
} else if (c === undefined) {
return { pos, type: "eof" };
} else {
state = STATE_COMMENT_MULTI;
}
} else if (state == STATE_NUMBER) {
if (isNumber(c) || c == ".") {
res.push(c);
} else {
this.p--;
const w = res.join("");
return { pos, type: "num", value: parseFloat(w) };
}
} else if (state == STATE_OPERATOR) {
if (isOperator(c)) {
res.push(c);
} else {
this.p--;
const w = res.join("");
return { pos, type: "operator", operator: w };
}
}
}
}
backToken(token) {
this.p = token.pos;
}
getExpression1() {
let res = this.getValue();
for (;;) {
const op = this.getToken();
if (op.type != "operator" || (
op.operator != "*" &&
op.operator != "/" &&
op.operator != "%" &&
op.operator != "//"
)) {
this.backToken(op);
return res;
}
const v2 = this.getValue();
const o = op.operator;
if (o != "*" && o != "/" && o != "%" && o != "//") {
//throw new Error("非対応の演算子が使われています: " + o);
throw new Error("illegal operator: " + o);
}
res = {
type: "BinaryExpression",
left: res,
operator: o,
right: v2,
};
}
}
getExpression() {
let res = this.getExpression1();
for (;;) {
const op = this.getToken();
if (op.type != "operator" || op.operator == ",") {
this.backToken(op);
return res;
}
const v2 = this.getExpression1();
if (op.operator == "+") {
res = {
type: "BinaryExpression",
left: res,
operator: "+",
right: v2,
};
} else {
if (typeof res == "string" || typeof v2 == "string") {
//throw new Error("文字列では使用できない演算子です");
throw new Error("this operator can not use for string");
}
if (op.operator == "-") {
res = {
type: "BinaryExpression",
left: res,
operator: "-",
right: v2,
};
} else {
//throw new Error("未対応の演算子です : " + op.operator);
this.backToken(op);
return res;
}
}
}
}
getValue() {
const t1 = this.getToken();
if (t1.type == "(") {
const res = this.getExpression();
const t2 = this.getToken();
if (t2.type != ")") {
//throw new Error("カッコが閉じられていません");
throw new Error("missing close blacket");
}
return res;
}
if (t1.type == "[") {
const elements = [];
for (;;) {
const t2 = this.getToken();
if (t2.type == "]") break;
this.backToken(t2);
elements.push(this.getExpression());
const t3 = this.getToken();
if (t3.type == "]") break;
if (t3.type != "operator" && t3.operator != ",") {
//throw new Error("配列の定義は , で区切る必要があります");
throw new Error(`array values must separate by ","`);
}
}
return {
type: "ArrayExpression",
elements,
};
}
if (t1.type == "num" || t1.type == "string") {
return {
type: "Literal",
value: t1.value,
};
} else if (t1.type == "operator" && t1.operator == "-") {
return {
type: "UnaryExpression",
operator: "-",
argument: this.getValue(),
};
} else if (t1.type == "var") {
const chk = this.getToken();
if (chk.type != "(") {
this.backToken(chk);
return this.getVar(t1.name);
}
// function call
const args = [];
const chk1 = this.getToken();
if (chk1.type != ")") {
this.backToken(chk1);
for (;;) {
args.push(this.getExpression());
const chk2 = this.getToken();
if (chk2.type == ")") break;
if (chk2.type != "operator" && chk2.operator != ",") {
//throw new Error("関数呼び出しのパラメータは , 区切りが必要です");
throw new Error(`function call arguments must separate by ","`);
}
}
}
return {
type: "CallExpression",
callee: {
type: "Identifier",
name: t1.name,
},
arguments: args,
};
/*
} else if (t1.operator == "-") {
const t2 = this.getToken();
if (t2.type == "num") {
return {
type: "Literal",
value: -t2.value,
};
} else {
throw new Error("式ではないものが指定されています : " + t1.type);
}
*/
} else {
//throw new Error("式ではないものが指定されています : " + t1.type);
throw new Error("illegal expression: " + t1.type);
}
}
getConditionValue1() {
const t1 = this.getToken();
if (t1.type == "(") {
const res = this.getCondition();
const t2 = this.getToken();
if (t2.type != ")") {
//throw new Error("カッコが閉じられていません");
throw new Error("missing blacket close");
}
return res;
} else {
this.backToken(t1);
}
//const v1 = this.getValue();
const v1 = this.getExpression();
const op = this.getToken();
if (op.type != "operator") {
this.backToken(op);
return v1;
}
//const v2 = this.getValue();
const v2 = this.getExpression();
if (["==", "!=", ">", "<", ">=", ">=", "<="].indexOf(op.operator) == -1) {
//throw new Error("条件式で未対応の演算子です : " + op.operator);
throw new Error("illegal operator in condition : " + op.operator);
}
return {
type: "BinaryExpression",
left: v1,
operator: op.operator,
right: v2,
};
}
getConditionValue() {
const chknot = this.getToken();
let flg = true;
if (chknot.type == "not") {
flg = false;
} else {
this.backToken(chknot);
}
const n = this.getConditionValue1();
if (flg) {
return n;
} else {
return {
type: "UnaryExpression",
operator: "not",
argument: n,
};
}
}
getConditionAnd() {
let res = this.getConditionValue();
for (;;) {
const op = this.getToken();
if (op.type != "and") {
this.backToken(op);
return res;
}
const v2 = this.getConditionValue();
if (op.type == "and") {
res = {
type: "BinaryExpression",
left: res,
operator: "and",
right: v2,
};
} else {
//throw new Error("未対応の演算子です : " + op.operator);
throw new Error("illegal operator : " + op.operator);
}
}
}
getCondition() {
let res = this.getConditionAnd();
for (;;) {
const op = this.getToken();
if (op.type != "or") {
this.backToken(op);
return res;
}
const v2 = this.getConditionAnd();
if (op.type == "or") {
res = {
type: "BinaryExpression",
left: res,
operator: "or",
right: v2,
};
} else {
//throw new Error("未対応の演算子です : " + op.operator);
throw new Error("illegal operator : " + op.operator);
}
}
}
getVar(name) {
let op = this.getToken();
const array = [];
for (;;) {
if (op.type != "[") {
this.backToken(op);
break;
}
const idx = this.getExpression();
const op2 = this.getToken();
if (op2.type != "]") {
//throw new Error("配列の要素指定が ] で囲われていません");
throw new Error("missing close array blacket");
}
array.push(idx);
op = this.getToken();
}
if (array.length == 0) {
return {
type: "Identifier",
name: name,
};
}
let left = {
type: "MemberExpression",
object: {
type: "Identifier",
name: name,
},
property: array[0],
computed: true,
//optional: false,
};
for (let i = 1; i < array.length; i++) {
left = {
type: "MemberExpression",
object: left,
property: array[i],
computed: true,
//optional: false,
};
}
return left;
}
parseCommand(body, forcetoken) {
const token = forcetoken || this.getToken();
//console.log("parseCommand", token);
if (token.type == "eof") return false;
if (BLACKET_MODE) {
if (token.type == "}") {
this.backToken(token);
return false;
}
} else {
if (["endif", "else", "elseif", "next", "until", "end"].indexOf(token.type) >= 0) {
this.backToken(token);
return false;
}
}
if (token.type == "print") {
const res = [];
const chk = this.getToken(true);
if (chk.type == "eol" || chk.type == "eof") {
this.backToken(chk);
} else {
this.backToken(chk);
for (;;) {
res.push(this.getExpression());
const op = this.getToken();
if (op.type == "eol" || op.type == "eof") {
this.backToken(op);
break;
}
if (op.operator != ",") {
this.backToken(op);
break;
//throw new Error("表示はコンマ区切りのみ対応しています");
}
}
}
body.push({
type: "ExpressionStatement",
expression: {
type: "CallExpression",
callee: {
type: "Identifier",
name: "print",
},
arguments: res,
},
});
} else if (token.type == "var" || token.type == "input") {
const chk = this.getToken();
if (chk.type == "(") { // function
const params = [];
const chk = this.getToken();
if (chk.type != ")") {
this.backToken(chk);
for (;;) {
params.push(this.getExpression());
const cma = this.getToken();
if (cma.type == ")") break;
if (cma.type != "operator" && cma.operator != ",") {
//throw new Error("引数の区切り , が必要です");
throw new Error(`function call arguments must separete by ","`);
}
}
}
body.push({
type: "ExpressionStatement",
expression: {
type: "CallExpression",
callee: {
type: "Identifier",
name: token.name,
},
arguments: params,
},
});
} else { // var
this.backToken(chk);
let token2 = token;
const res = [];
for (;;) {
const left = this.getVar(token2.name);
const op = this.getToken();
if (op.type != "operator" || op.operator != "=") {
//throw new Error("代入は変数の後に = で続ける必要があります");
throw new Error(`assign operation must have "="`);
}
const right = this.getExpression();
res.push({
type: "AssignmentExpression",
operator: "=",
left,
right,
});
//if (isConstantName(token2.name) && this.vars[token2.name] !== undefined) throw new Error("定数には再代入できません");
//this.vars[token2.name] = val;
const op2 = this.getToken();
if (op2.type == "eol" || op2.type == "eof") {
this.backToken(op2);
break;
}
if (op2.operator != ",") {
//throw new Error("代入はコンマ区切りのみ対応しています");
this.backToken(op2);
break;
}
token2 = this.getToken();
if (token2.type != "var") {
//throw new Error("コンマ区切りで続けられるのは代入文のみです");
throw new Error("only assign operation after comma");
}
}
if (res.length == 1) {
body.push({
type: "ExpressionStatement",
expression: res[0],
});
} else {
body.push({
type: "ExpressionStatement",
expression: {
type: "SequenceExpression",
expressions: res,
}
});
}
}
} else if (token.type == "if") {
const cond = this.getCondition();
if (BLACKET_MODE) {
const tthen = this.getToken();
//console.log("tthen", tthen);
if (tthen.type != "{") {
//throw new Error(`if文の条件の後に"{"がありません`);
throw new Error(`missing "{" after if`);
}
}
const then = [];
for (;;) {
if (!this.parseCommand(then)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") {
//throw new Error(`"}"で閉じられていません`);
throw new Error(`missing "}"`);
}
break;
} else {
if (endblacket.type != "endif" && endblacket.type != "else" && endblacket.type != "elseif") {
throw new Error("if must have endif, else or elseif");
}
this.backToken(endblacket);
break;
}
}
}
const telse = this.getToken();
if (!BLACKET_MODE && telse.type == "elseif") {
const bodyelse = [];
const forcetoken = { type: "if" };
this.parseCommand(bodyelse, forcetoken);
body.push({
type: "IfStatement",
test: cond,
consequent: {
type: "BlockStatement",
body: then,
},
alternate: bodyelse[0],
});
} else if (telse.type == "else") {
if (BLACKET_MODE) {
const telse2 = this.getToken();
if (telse2.type == "if") {
this.backToken(telse2);
const bodyelse = [];
this.parseCommand(bodyelse);
body.push({
type: "IfStatement",
test: cond,
consequent: {
type: "BlockStatement",
body: then,
},
alternate: bodyelse[0],
});
return true;
}
if (telse2.type != "{") throw new Error(`else文の条件の後に"{"がありません`);
}
const bodyelse = [];
for (;;) {
if (!this.parseCommand(bodyelse)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") {
//throw new Error(`"}"で閉じられていません`);
throw new Error(`missing "}"`);
}
} else {
if (endblacket.type != "endif") {
throw new Error("else must have endif");
}
//this.backToken(endblacket);
}
break;
}
}
body.push({
type: "IfStatement",
test: cond,
consequent: {
type: "BlockStatement",
body: then,
},
alternate: {
type: "BlockStatement",
body: bodyelse,
},
});
} else if (telse.type == "endif") {
body.push({
type: "IfStatement",
test: cond,
consequent: {
type: "BlockStatement",
body: then,
},
alternate: null,
});
}
} else if (token.type == "while") {
const cond = this.getCondition();
//console.log("tthen", tthen);
if (BLACKET_MODE) {
const tthen = this.getToken();
if (tthen.type != "{") throw new Error(`while文の条件の後に"{"がありません`);
}
const then = [];
for (;;) {
if (!this.parseCommand(then)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") throw new Error(`while文が"}"で閉じられていません`);
} else {
if (endblacket.type != "next") throw new Error(`while must have next`);
}
break;
}
}
body.push({
type: "WhileStatement",
test: cond,
body: {
type: "BlockStatement",
body: then,
}
});
} else if (token.type == "do") {
if (BLACKET_MODE) {
const tthen = this.getToken();
if (tthen.type != "{") throw new Error(`do文の条件の後に"{"がありません`);
}
const then = [];
for (;;) {
if (!this.parseCommand(then)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") throw new Error(`do文が"}"で閉じられていません`);
} else {
if (endblacket.type != "until") throw new Error(`do must have until`);
this.backToken(endblacket);
}
break;
}
}
const whileoruntil = this.getToken();
if (BLACKET_MODE && whileoruntil.type == "while") {
const cond = this.getCondition();
body.push({
type: "DoWhileStatement",
body: {
type: "BlockStatement",
body: then,
},
test: cond,
});
} else if (whileoruntil.type == "until") {
const cond = this.getCondition();
body.push({
type: "DoWhileStatement",
body: {
type: "BlockStatement",
body: then,
},
test: {
type: "UnaryExpression",
operator: "not",
argument: cond,
},
});
} else {
if (BLACKET_MODE) {
throw new Error("do文の後は while または until が必要です");
} else {
throw new Error("do must have until");
}
}
} else if (token.type == "for") {
const varname = this.getToken();
if (varname.type != "var") {
//throw new Error("for文の後は変数名が必要です");
throw new Error("need var name after for");
}
const eq = this.getToken();
if (eq.operator != "=") {
// throw new Error("for文の変数名の後は = が必要です");
throw new Error("need = after for var name");
}
const initval = this.getExpression();
const to = this.getToken();
if (to.type != "to") {
//throw new Error("for文の初期変数設定の後は to が必要です");
throw new Error("need to after for initial assign");
}
const endval = this.getExpression();
const chkstep = this.getToken();
let step = {
type: "Literal",
value: 1,
};
if (chkstep.type != "step") {
this.backToken(chkstep);
} else {
step = this.getExpression();
if (!(
step.type == "Literal" ||
step.type == "UnaryExpression" && step.operator == "-" && step.argument.type == "Literal"
)) {
//throw new Error("stepには数値のみ指定可能です");
throw new Error("step must be literal");
}
}
if (BLACKET_MODE) {
const tthen = this.getToken();
if (tthen.type != "{") throw new Error(`for文の後に"{"がありません`);
}
const then = [];
for (;;) {
if (!this.parseCommand(then)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") throw new Error(`for文が"}"で閉じられていません`);
} else {
if (endblacket.type != "next") throw new Error(`for must have "next"`);
}
break;
}
}
const astvar = {
type: "Identifier",
name: varname.name,
};
const stepval = step.type == "Literal" ? step.value : -step.argument.value;
body.push({
type: "ForStatement",
init: {
type: "AssignmentExpression",
operator: "=",
left: astvar,
right: initval,
},
test: {
type: "BinaryExpression",
left: astvar,
operator: stepval > 0 ? "<=" : ">=",
right: endval,
},
update: {
type: "AssignmentExpression",
operator: "=",
left: astvar,
right: {
type: "BinaryExpression",
left: astvar,
operator: "+",
right: step,
},
},
body: {
type: "BlockStatement",
body: then,
},
});
} else if (token.type == "function") {
const varname = this.getToken();
if (varname.type != "var") {
//throw new Error("function文の後は関数名が必要です");
throw new Error("need function name after function");
}
const blacket = this.getToken();
if (blacket.type != "(") {
//throw new Error("関数名の後は ( が必要です");
throw new Error(`need "(" after function name`);
}
const params = [];
const chk = this.getToken();
if (chk.type != ")") {
this.backToken(chk);
for (;;) {
const chk = this.getToken();
if (chk.type != "var") {
//throw new Error("引数名がありません");
throw new Error("need parameter name");
}
params.push(chk.name);
const cma = this.getToken();
if (cma.type == ")") break;
if (cma.type != "operator" && cma.operator != ",") {
//throw new Error("引数の区切り , が必要です");
throw new Error(`need comma for separation of parameters`);
}
}
}
if (BLACKET_MODE) {
const b2 = this.getToken();
if (b2.type != "{") throw new Error("関数の中身記述に { が必要です");
}
const body2 = [];
for (;;) {
if (!this.parseCommand(body2)) {
const endblacket = this.getToken();
if (BLACKET_MODE) {
if (endblacket.type != "}") throw new Error(`関数が"}"で閉じられていません`);
} else {
if (endblacket.type != "end") throw new Error(`function must have end`);
}
break;
}
}
body.push({
type: "FunctionDeclaration",
id: {
type: "Identifier",
name: varname.name,
},
params: params.map(i => ({ type: "Identifier", name: i })),
body: {
type: "BlockStatement",
body: body2,
}
});
} else if (token.type == "return") {
body.push({
type: "ReturnStatement",
argument: this.getExpression(),
});
} else if (token.type == "break") {
body.push({
type: "BreakStatement",
});
} else {
//throw new Error("対応していない type " + token.type + " です");
}
//console.log(token);
return true;
}
parse() {
this.p = 0;
const body = [];
while (this.parseCommand(body));
const ast = { "type": "Program", body };
this.ast = ast;
}
parseTokens() {
this.p = 0;
for (;;) {
const c = this.getToken(true);
console.log(c);
if (c.type == "eof") break;
}
}
getArrayIndex(ast, scope) {
const prop = this.calcExpression(ast, scope);
if (prop < 0 || typeof prop == "string" && parseInt(prop).toString() != prop) {
throw new Error("配列には0または正の整数のみ指定可能です");
}
return prop;
}
runBlock(ast, scope) {
const body = ast.type == "BlockStatement" ||
ast.type == "Program" ? ast.body :
ast.type == "SequenceExpression" ? ast.expressions : [ast];
for (const cmd of body) {
//console.log(cmd)
if (cmd.type == "ExpressionStatement") {
this.runBlock(cmd.expression, scope);
} else if (cmd.type == "AssignmentExpression") {
const name = this.getVarName(cmd.left);
if (scope.isDefined(name) && isConstantName(name)) {
//throw new Error("定数には再代入できません");
throw new Error("constant can't assign again");
}
if (cmd.left.type == "Identifier") {
scope.setVar(name, this.calcExpression(cmd.right, scope));
} else if (cmd.left.type == "MemberExpression") {
if (!scope.isDefined(name)) {
scope.setVar(name, []);