From 939a7f47ca0d22dcb2a3404153e146289a5621b5 Mon Sep 17 00:00:00 2001 From: Taisuke Fukuno Date: Tue, 24 Dec 2024 16:00:50 +0900 Subject: [PATCH] fix parsing conditions --- DNCL3.js | 13 +++++++------ README.md | 3 +++ examples/for.dncl | 4 ++-- examples/printbin.dncl | 11 +++++++++++ 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 examples/printbin.dncl diff --git a/DNCL3.js b/DNCL3.js index 7541b61..22f054b 100644 --- a/DNCL3.js +++ b/DNCL3.js @@ -172,7 +172,9 @@ export class DNCL3 { right: v2, }; } else { - throw new Error("未対応の演算子です : " + op.operator); + //throw new Error("未対応の演算子です : " + op.operator); + this.backToken(op); + return res; } } } @@ -234,13 +236,15 @@ export class DNCL3 { } else { this.backToken(t1); } - const v1 = this.getValue(); + //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.getValue(); + const v2 = this.getExpression(); if (["==", "!=", ">", "<", ">=", ">=", "<="].indexOf(op.operator) == -1) { throw new Error("条件式で未対応の演算子です : " + op.operator); } @@ -411,7 +415,6 @@ export class DNCL3 { this.backToken(telse2); const bodyelse = []; this.parseCommand(bodyelse); - console.log(bodyelse) body.push({ type: "IfStatement", test: cond, @@ -564,7 +567,6 @@ export class DNCL3 { runBlock(ast) { const body = ast.type == "BlockStatement" || ast.type == "Program" ? ast.body : [ast]; for (const cmd of body) { - console.log(cmd) if (cmd.type == "ExpressionStatement") { this.runBlock(cmd.expression); } else if (cmd.type == "AssignmentExpression") { @@ -613,7 +615,6 @@ export class DNCL3 { if (ast.type == "Literal") { return ast.value; } else if (ast.type == "Identifier") { - console.log("vars", this.vars) if (this.vars[ast.name] === undefined) { throw new Error("初期化されていない変数 " + ast.name + " が使われました"); } diff --git a/README.md b/README.md index 07dadfd..7d38c9b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ deno run -A DNCL3.example.js bmi ``` +- ブラウザで動作するテスト環境 [dncl2js](https://code4fukui.github.io/DNCL3/dncl2js.html) + ※ TODO: 下記は未実装です - 配列 / input / do / until / function / return @@ -360,3 +362,4 @@ atai = 乱数() # 0以上1未満のランダムな小数をataiに代入する ## reference - [共通テスト手順記述標準言語 (DNCL) の説明 独立行政法人大学入試センター 2022年1月](https://www.dnc.ac.jp/albums/abm.php?d=67&f=abm00000819.pdf&n=R4_%E5%85%B1%E9%80%9A%E3%83%86%E3%82%B9%E3%83%88%E6%89%8B%E9%A0%86%E8%A8%98%E8%BF%B0%E6%A8%99%E6%BA%96%E8%A8%80%E8%AA%9E%EF%BC%88DNCL%EF%BC%89%E3%81%AE%E8%AA%AC%E6%98%8E.pdf) +- [令和7年度大学入学共通テスト 試作問題「情報」の概要 独立行政法人大学入試センター](https://www.dnc.ac.jp/albums/abm.php?d=511&f=abm00003141.pdf&n=6-1_%E6%A6%82%E8%A6%81%E3%80%8C%E6%83%85%E5%A0%B1%E3%80%8D.pdf) diff --git a/examples/for.dncl b/examples/for.dncl index 040e788..cddfa05 100644 --- a/examples/for.dncl +++ b/examples/for.dncl @@ -1,3 +1,3 @@ -for i = 0 to 10 { - print i +for i = 0 to 15 { + print i, i // 3, i / 3 } diff --git a/examples/printbin.dncl b/examples/printbin.dncl new file mode 100644 index 0000000..b7111f3 --- /dev/null +++ b/examples/printbin.dncl @@ -0,0 +1,11 @@ +n = 100 +bin = "" +for i = 0 to 8 { + if n % 2 == 1 { + bin = "1" + bin + } else { + bin = "0" + bin + } + n = n // 2 +} +print bin