-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
176 lines (159 loc) · 4.43 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>Mochi Playground</title>
</head><body>
<main>
<div class=divheader>
<h1>Mochi Playground</h1>
<div class=divchks>
<label><input type=radio name=radiotype checked id=chkast>AST (構文木)</label>
<label><input type=radio name=radiotype id=chkwat>WAT (wasm S式)</label>
<label><input type=radio name=radiotype id=chkwasm>WASM (バイナリ)</label>
<label><input type=radio name=radiotype id=chkoutput>RUN</label>
</div>
</div>
<div class=diveditor id=divprog></div>
<div class=diveditor id=divrun></div>
</main>
<hr>
<a href=https://github.com/code4fukui/Mochi/>src on GitHub</a>
<script type="module">
import { monaco } from "https://code4fukui.github.io/monaco-editor/monaco.js";
import { Mochi } from "./Mochi.js";
import { WABT } from "https://code4fukui.github.io/WABT-es/WABT.js";
const wabt = await WABT();
const wat2wasm = (wat) => {
const module = wabt.parseWat("test.wast", wat);
const mbin = module.toBinary({ log: false });
return mbin.buffer;
};
const bin2hex = (bin) => {
const res = [];
const hex = [];
for (let i = 0; i < bin.length; i++) {
const n = bin[i].toString(16);
hex.push(n.length == 1 ? "0" + n : n);
if (i % 16 == 15) {
res.push(hex.join(" "));
hex.length = 0;
}
}
if (hex.length > 0) {
res.push(hex.join(" "));
}
return res.join("\n");
}
const getEntryFunc = (module) => {
const exp = module.instance.exports;
let entryfunc = null;
for (const name in exp) {
entryfunc = name;
if (name.indexOf("start") >= 0) {
entryfunc = name;
break;
}
}
if (!entryfunc) return null;
return exp[entryfunc];
};
const runWASM = async (wasm) => {
const module = await WebAssembly.instantiate(wasm);
console.log(module.instance.exports);
const entry = getEntryFunc(module);
const res = await entry();
return res;
};
const compile = (src) => {
const output = Mochi.compile(src);
console.log(output);
return output;
};
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, "javascript");
const run = makeEditor(divrun, "");
const onchange = async () => {
const txt = prog.getValue();
if (chkwat.checked) {
const output = compile(txt);
run.setValue(output.toString());
} else if (chkast.checked) {
const ast = Mochi.parse(txt);
run.setValue(JSON.stringify(ast, null, 2));
} else if (chkwasm.checked) {
const wat = compile(txt);
const wasm = wat2wasm(wat)
const wasmhex = bin2hex(wasm);
run.setValue(wasmhex);
} else if (chkoutput.checked) {
const wat = compile(txt);
const wasm = wat2wasm(wat)
const output = await runWASM(wasm);
const soutput = (output || "").toString();
console.log("out", output, soutput)
run.setValue(soutput);
}
};
//prog.onDidChangeModelContent = onchange; // なぜか初回のみ
let bkval = null;
setInterval(() => {
const txt = prog.getValue();
if (bkval == txt) return;
onchange();
bkval = txt;
}, 500);
const src = await (await fetch("example/sum.mochi.js")).text();
prog.setValue(src);
chkast.oninput = () => onchange();
chkwat.oninput = () => onchange();
chkwasm.oninput = () => onchange();
chkoutput.oninput = () => onchange();
</script>
<style>
main {
x-display: grid;
x-grid-template-columns: 1fr 1fr;
x-gap: 0.5em;
}
.divheader {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.5em;
}
.diveditor {
display: inline-block;
height: 80vh;
width: calc(50% - .4em);
padding: .1em;
}
h1 {
margin: 0;
}
.divchks {
padding-top: 1em;
}
a {
color: gray !important;
}
</style>
</body></html>