Skip to content

Commit de2648e

Browse files
LingDong-Lingdong Huang
authored andcommitted
import in, elseif, iftrue, iffalse, any
1 parent ecc1c07 commit de2648e

File tree

7 files changed

+65
-16
lines changed

7 files changed

+65
-16
lines changed

lib/列經.wy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
是謂「篩剔」之術也。
2727

2828

29-
今有一術。名之曰「左併」。欲行是術。必先得一術。曰「甲」。一量。曰「乙」。一列。曰「丙」。乃行是術曰。
30-
吾有一量曰「乙」。名之曰「丁」。
29+
今有一術。名之曰「左併」。欲行是術。必先得一術。曰「甲」。一元。曰「乙」。一列。曰「丙」。乃行是術曰。
30+
吾有一元曰「乙」。名之曰「丁」。
3131
凡「丙」中之「戊」。
3232
施「甲」於「丁」於「戊」。
3333
昔之「丁」者。今其是矣。
@@ -36,8 +36,8 @@
3636
是謂「左併」之術也。
3737

3838

39-
今有一術。名之曰「右併」。欲行是術。必先得一術。曰「甲」。一量。曰「乙」。一列。曰「丙」。乃行是術曰。
40-
吾有一量曰「乙」。名之曰「丁」。
39+
今有一術。名之曰「右併」。欲行是術。必先得一術。曰「甲」。一元。曰「乙」。一列。曰「丙」。乃行是術曰。
40+
吾有一元曰「乙」。名之曰「丁」。
4141
夫「丙」之長。名之曰「戊」。
4242
恆為是。若「戊」小於一者乃止也。
4343
夫「丙」之「戊」。夫「丁」。取二以施「甲」。
@@ -107,7 +107,7 @@
107107
是謂「製列」之術也。
108108

109109

110-
今有一術。名之曰「索一」。欲行是術。必先得一量。曰「甲」。一列。曰「乙」。乃行是術曰。
110+
今有一術。名之曰「索一」。欲行是術。必先得一元。曰「甲」。一列。曰「乙」。乃行是術曰。
111111
吾有一數曰一。名之曰「戊」。
112112
恆為是。若「戊」大於「甲」之長者乃止也。
113113
若「乙」等於「甲」之「戊」者。乃得「戊」也。

src/compiler/js.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ class JSCompiler extends Base {
111111
curlvl--;
112112
js += ";";
113113
} else if (a.op == "if") {
114+
if (a.elseif) {
115+
js += "}else ";
116+
}
114117
js += "if (";
118+
if (a.not) {
119+
js += "!(";
120+
}
115121
var j = 0;
116122
while (j < a.test.length) {
117123
if (a.test[j][0] == "cmp") {
@@ -136,6 +142,9 @@ class JSCompiler extends Base {
136142
}
137143
j++;
138144
}
145+
if (a.not) {
146+
js += ")";
147+
}
139148
js += "){";
140149
curlvl++;
141150
} else if (a.op == "else") {
@@ -215,6 +224,8 @@ class JSCompiler extends Base {
215224
curlvl++;
216225
} else if (a.op == "break") {
217226
js += "break;";
227+
} else if (a.op == "continue") {
228+
js += "continue;";
218229
} else if (a.op == "not") {
219230
var v = getval(a.value);
220231
var vname = this.nextTmpVar();

src/keywords.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var KEYWORDS_DEFINE = {
1313
: ["type", "fun"],
1414
: ["type", "bol"],
1515
: ["type", "obj"],
16-
: ["type", "any"],
16+
: ["type", "any"],
1717
書之: ["print"],
1818
名之曰: ["name"],
1919
: ["call", "r"],
@@ -45,10 +45,14 @@ var KEYWORDS_DEFINE = {
4545
為是: ["ctrl", "whilen0"],
4646
: ["ctrl", "whilen1"],
4747
乃止: ["ctrl", "break"],
48+
乃止是遍: ["ctrl", "continue"],
4849

4950
若非: ["ctrl", "else"],
5051
: ["ctrl", "if"],
5152
: ["ctrl", "conj"],
53+
若其然者: ["ctrl", "iftrue"],
54+
若其不然者: ["ctrl", "iffalse"],
55+
或若: ["ctrl", "elseif"],
5256

5357
其物如是: ["ctrl", "objbody"],
5458
之物也: ["ctrl", "objend"],
@@ -84,6 +88,7 @@ var KEYWORDS_DEFINE = {
8488
: ["bool", true],
8589

8690
吾嘗觀: ["import", "file"],
91+
: ["import", "in"],
8792
之書: ["import", "fileend"],
8893
方悟: ["import", "iden"],
8994
之義: ["import", "idenend"],

src/macro.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ function extractMacros(txt, options = {}) {
2020
if (txt[i] == "吾" && txt[i + 1] == "嘗" && txt[i + 2] == "觀") {
2121
var imp = txt
2222
.slice(i + 3)
23-
.split(/[]/g)[0]
24-
.replace(/[]/g, "");
23+
.split("之書")[0]
24+
.split("中")
25+
.map(x => x.replace(/[]/g, ""))
26+
.join("/");
2527
imps.push(imp);
2628
}
2729
}

src/parser.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ function tokens2asc(
377377
}
378378
i++;
379379
asc.push(x);
380+
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "iftrue") {
381+
asc.push({ op: "if", test: [["ans"]], pos });
382+
i++;
383+
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "iffalse") {
384+
asc.push({ op: "if", test: [["ans"]], not: true, pos });
385+
i++;
386+
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "elseif") {
387+
var x = { op: "if", test: [], elseif: true, pos };
388+
i++;
389+
while (!(gettok(i, 0) == "ctrl" && gettok(i, 1) == "conj")) {
390+
x.test.push(tokens[i]);
391+
i++;
392+
}
393+
i++;
394+
asc.push(x);
380395
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "end") {
381396
asc.push({ op: "end", pos });
382397
i++;
@@ -392,6 +407,9 @@ function tokens2asc(
392407
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "break") {
393408
asc.push({ op: "break", pos });
394409
i += 1;
410+
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "continue") {
411+
asc.push({ op: "continue", pos });
412+
i += 1;
395413
} else if (gettok(i, 0) == "ctrl" && gettok(i, 1) == "else") {
396414
asc.push({ op: "else", pos });
397415
i += 1;
@@ -580,9 +598,13 @@ function tokens2asc(
580598
asc.push({ op: "take", count: cnt, pos });
581599
i += 2;
582600
} else if (gettok(i, 0) == "import" && gettok(i, 1) == "file") {
583-
typeassert(i + 2, ["import"]);
584601
var x = { op: "import", file: gettok(i + 1, 1), iden: [], pos };
585-
i += 3;
602+
i += 2;
603+
while (gettok(i, 0) == "import" && gettok(i, 1) == "in") {
604+
x.file += "/" + gettok(i + 1, 1);
605+
i += 2;
606+
}
607+
i += 1;
586608
if (tokens[i] && gettok(i, 0) == "import" && gettok(i, 1) == "iden") {
587609
i++;
588610
while (!(gettok(i, 0) == "import" && gettok(i, 1) == "idenend")) {
@@ -628,7 +650,9 @@ function tokens2asc(
628650
}
629651

630652
function jsWrapModule(name, src) {
631-
return `/*___wenyan_module_${name}_start___*/var ${name} = new function(){ ${src} };/*___wenyan_module_${name}_end___*/`;
653+
var splitted = name.split("/");
654+
var bname = splitted[splitted.length - 1];
655+
return `/*___wenyan_module_${name}_start___*/var ${bname} = new function(){ ${src} };/*___wenyan_module_${name}_end___*/`;
632656
}
633657
function pyWrapModule(name, src) {
634658
// return `#/*___wenyan_module_${name}_start___*/\nclass ${name}:\n${src.split("\n").map(x=>"\t"+x).join("\n")}\n#/*___wenyan_module_${name}_end___*/\n`;

src/typecheck.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ function typecheck(
332332
} else if (a.op == "end") {
333333
scopepop(a, "if", "else", "for", "whiletrue", "whilen");
334334
} else if (a.op == "if") {
335+
if (a.elseif) {
336+
scopepop(a, "if");
337+
}
335338
scopepush(a);
336339
} else if (a.op == "else") {
337340
scopepop(a, "if");
@@ -551,6 +554,8 @@ function typecheck(
551554
scopepush(a);
552555
} else if (a.op == "break") {
553556
//pass
557+
} else if (a.op == "continue") {
558+
//pass
554559
} else if (a.op == "not") {
555560
strayvar.push(inittype("bol"));
556561
} else if (a.op == "reassign") {
@@ -680,7 +685,9 @@ function typecheck(
680685
} else {
681686
}
682687
}
683-
scopepop({ pos: a.pos, op: "EOF" }, "global");
688+
if (a) {
689+
scopepop({ pos: a.pos, op: "EOF" }, "global");
690+
}
684691
// console.log(scope.length)
685692
// console.dir(signature,{maxArrayLength:null,depth:null})
686693

tools/test_parser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function runExample(lang, name) {
2929
console.log(utils.pyeval(sourceCode));
3030
break;
3131
case "js":
32-
// eval(sourceCode);
32+
eval(sourceCode);
3333
break;
3434
case "rb":
3535
console.log(utils.rbeval(sourceCode));
@@ -52,11 +52,11 @@ function runAll(lang, skips = []) {
5252
}
5353
}
5454

55-
// runExample("js", "quicksort");
55+
// runExample("js", "import");
5656
// runExample("js", "../lib/js/畫譜");
5757
// runExample("js", "../lib/曆法");
58-
// runAll("js", ["quine", "quine2", "tree", "tree2", "try"]);
58+
runAll("js", ["quine", "quine2", "tree", "tree2", "try"]);
5959
// runAll("js", ["quine"]);
6060

61-
runExample("js", "../../../Downloads/local_test");
61+
// runExample("js", "../../../Downloads/local_test");
6262
// runExample("py", "draw_heart");

0 commit comments

Comments
 (0)