Skip to content

Commit

Permalink
support input
Browse files Browse the repository at this point in the history
#7
async not supported yet
  • Loading branch information
taisukef committed Jan 3, 2025
1 parent 3412953 commit 89aed02
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
16 changes: 14 additions & 2 deletions DNCL3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parseModule } from "https://code4fukui.github.io/acorn-es/parseModule.js";

const reserved = [
"print", "input",
"print",
//"input",
"if", "then", "else",
"while", "do", "until", "for", "to", "step", "break",
"function", "return",
Expand Down Expand Up @@ -471,7 +472,7 @@ export class DNCL3 {
arguments: res,
},
});
} else if (token.type == "var") {
} else if (token.type == "var" || token.type == "input") {
const chk = this.getToken();
if (chk.type == "(") { // function
const params = [];
Expand Down Expand Up @@ -1001,6 +1002,17 @@ export class DNCL3 {
}
} else if (ast.type == "CallExpression") {
const name = ast.callee.name;
if (name == "input") {
if (ast.arguments.length > 1) {
throw new Error("引数の数が合っていません");
}
const q = ast.arguments.length ? this.calcExpression(ast.arguments[0]) : "入力してください";
const s = prompt(q);
if (s == null) return "";
const f = parseFloat(s);
if (!isNaN(f) && f.toString() == s) return f;
return s;
}
if (this.vars[name] === undefined) {
throw new Error("定義されていない関数 " + name + " が使われました");
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deno run -A DNCL3.example.js bmi
- ブラウザで動作するテスト環境 [dncl2js](https://code4fukui.github.io/DNCL3/dncl2js.html)

※ TODO: 下記は未実装です
- [二次元以上の配列](https://github.com/code4fukui/DNCL3/issues/2) / [配列のすべてを初期化](https://github.com/code4fukui/DNCL3/issues/3) / [input](https://github.com/code4fukui/DNCL3/issues/7) / [変数のスコープ](https://github.com/code4fukui/DNCL3/issues/13)
- [二次元以上の配列](https://github.com/code4fukui/DNCL3/issues/2) / [配列のすべてを初期化](https://github.com/code4fukui/DNCL3/issues/3) / [変数のスコープ](https://github.com/code4fukui/DNCL3/issues/13)

## 1 変数と値

Expand Down Expand Up @@ -67,8 +67,8 @@ print s[0],s[2] # A C と表示される

外部から入力された値を代入するために,次のように記述することができます。

- 例: input x
- 例: input "0から100までの好きな数を入力してください", x
- 例: x = input()
- 例: x = input("0から100までの好きな数を入力してください")

## 4 演算

Expand Down
8 changes: 4 additions & 4 deletions dncl2js.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ <h1>DNCL3 → AST → like JavaScript → output</h1>
import escodegen from "https://code4fukui.github.io/escodegen/escodegen.js";
import { monaco } from "https://code4fukui.github.io/monaco-editor/monaco.js";


export const makeEditor = (div, language) => {
const editor = monaco.editor.create(div, {
language,
Expand Down Expand Up @@ -74,6 +73,7 @@ <h1>DNCL3 → AST → like JavaScript → output</h1>
}
};

let bkval = null;
const onchange = () => {
const src = prog.getValue();
try {
Expand All @@ -93,15 +93,14 @@ <h1>DNCL3 → AST → like JavaScript → output</h1>
run.setValue(e.toString());
prog2.setValue("");
}
bkval = src;
};

//prog.onDidChangeModelContent = onchange; // なぜか初回のみ
let bkval = null;
setInterval(() => {
const txt = prog.getValue();
if (bkval == txt) return;
onchange();
bkval = txt;
}, 500);

//const fn = "bmi";
Expand All @@ -114,7 +113,8 @@ <h1>DNCL3 → AST → like JavaScript → output</h1>
//const fn = "varja";
//const fn = "funcja";
//const fn = "dowhile";
const fn = "comment";
//const fn = "comment";
const fn = "input";

const fn0 = location.hash.substring(1) || fn;
const firstprog = await (await fetch("examples/" + fn0 + ".dncl")).text();
Expand Down
4 changes: 4 additions & 0 deletions examples/input.dncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x = input()
print "input", x
x = input("二乗します")
print x * x

0 comments on commit 89aed02

Please sign in to comment.