forked from code4fukui/Wirth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnor2js.html
130 lines (114 loc) · 3.3 KB
/
nor2js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>Nor → AST → like JavaScript → output</title>
</head><body>
<h1>Nor → AST → like JavaScript → output</h1>
<main>
<div class=diveditor id=divprog></div>
<div class=diveditor id=divast></div>
<div class=diveditor id=divprog2></div>
<div class=diveditor id=divrun></div>
</main>
<a href=https://github.com/code4fukui/Nor/>src on GitHub</a>
<style>
h1 {
margin: 0;
}
.diveditor {
width: calc(25% - .5em);
padding: .1em;
display: inline-block;
height: calc(100vh - 6em);
}
a {
color: gray !important;
}
</style>
<script type="module">
import { Nor } from "./Nor.js";
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,
autoIndent: true,
//autoIndent: "none",
//formatOnPaste: true,
//formatOnType: true,
suggestOnTriggerCharacters: false, // トリガーキャラクターでの補完を無効化
acceptSuggestionOnEnter: 'off', // Enterでの補完選択を無効化
parameterHints: false, // パラメータヒントを無効化
quickSuggestions: false, // 自動的に補完候補を表示しない
inlineSuggest: { enabled: false }, // インライン補完を無効化
tabSize: 2,
minimap: { enabled: false },
//overflow: "auto",
automaticLayout: true,
theme: "vs-dark",
});
window.addEventListener("resize", () => {
editor.layout();
});
return editor;
};
const prog = makeEditor(divprog, "");
const ast = makeEditor(divast, "JSON");
const prog2 = makeEditor(divprog2, "");
const run = makeEditor(divrun, "");
const removeStartEnd = (json) => {
for (const name in json) {
const v = json[name];
if (typeof v == "object") {
removeStartEnd(v);
} else if (name == "start" || name == "end") {
delete json[name];
}
}
};
let bkval = null;
const onchange = () => {
const src = prog.getValue();
try {
const runtime = new Nor(src, (s) => {
run.setValue(run.getValue() + s + "\n");
});
const astjson = runtime.ast;
removeStartEnd(astjson);
ast.setValue(JSON.stringify(astjson, null, 2));
prog2.setValue(escodegen.generate(astjson));
run.setValue("");
runtime.run();
} catch (e) {
console.log(e);
//ast.setValue(JSON.stringify(e, null, 2));
run.setValue(e.toString());
prog2.setValue("");
}
bkval = src;
};
//prog.onDidChangeModelContent = onchange; // なぜか初回のみ
setInterval(() => {
const txt = prog.getValue();
if (bkval == txt) return;
onchange();
}, 500);
//const fn = "bmi";
//const fn = "while";
//const fn = "for";
//const fn = "array";
//const fn = "func";
//const fn = "string";
//const fn = "geo3x3";
//const fn = "varja";
//const fn = "funcja";
//const fn = "dowhile";
//const fn = "comment";
//const fn = "input";
//const fn = "test";
//const fn = "elseif";
const fn = "nor";
const fn0 = location.hash.substring(1) || fn;
const firstprog = await (await fetch("examples/" + fn0 + ".nor")).text();
prog.setValue(firstprog);
onchange();
</script>
</body></html>