Skip to content

Commit

Permalink
fix parsing conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukef committed Dec 24, 2024
1 parent 335e610 commit 939a7f4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
13 changes: 7 additions & 6 deletions DNCL3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -411,7 +415,6 @@ export class DNCL3 {
this.backToken(telse2);
const bodyelse = [];
this.parseCommand(bodyelse);
console.log(bodyelse)
body.push({
type: "IfStatement",
test: cond,
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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 + " が使われました");
}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions examples/for.dncl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for i = 0 to 10 {
print i
for i = 0 to 15 {
print i, i // 3, i / 3
}
11 changes: 11 additions & 0 deletions examples/printbin.dncl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 939a7f4

Please sign in to comment.