Skip to content

Commit

Permalink
add impl constant
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukef committed Dec 15, 2024
1 parent c80f0dc commit 69b76c5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions DNCL3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const reserved = [

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;
};

export class DNCL3 {
constructor(s, callbackoutput) {
Expand Down Expand Up @@ -276,6 +284,7 @@ export class DNCL3 {
const op = this.getToken();
if (op.type != "operator" || op.operator != "=") throw new Error("代入は変数の後に = で続ける必要があります");
const val = this.getExpression();
if (isConstantName(token2.name) && this.vars[token2.name] !== undefined) throw new Error("定数には再代入できません");
this.vars[token2.name] = val;

const op2 = this.getToken();
Expand Down
5 changes: 5 additions & 0 deletions DNCL3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ Deno.test("var", () => {
t.assertEquals(run("a = 1\nb = 2\nc = a + b\nprint c"), ["3"]);
t.assertEquals(run(`a = "1"\nb = "2"\nc = a + b\nprint c`), ["12"]);
});
Deno.test("const", () => {
t.assertEquals(run("A = 1\nprint A"), ["1"]);
t.assertThrows(() => run("A = 1\nA = 2"));
t.assertEquals(run("Aa = 1\nAa = 2\nprint Aa"), ["2"]);
});
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ deno run -A DNCL3.example.js bmi
```

※ TODO: 下記は未実装です
- 定数 / 配列 / input / 複数行if / while / do / until / for / function / return
- 配列 / input / 複数行if / while / do / until / for / function / return

## 1 変数と値

Expand Down
2 changes: 2 additions & 0 deletions examples/const.dncl3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A = 3.2
A = 5

0 comments on commit 69b76c5

Please sign in to comment.