|
| 1 | +<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:"> |
| 2 | +<title>JavaScript → AST → JavaScript</title> |
| 3 | +</head><body> |
| 4 | +<h1>JavaScript → AST → JavaScript</h1> |
| 5 | +<main> |
| 6 | +<div class=diveditor id=divprog></div> |
| 7 | +<div class=diveditor id=divast></div> |
| 8 | +<div class=diveditor id=divprog2></div> |
| 9 | +</main> |
| 10 | + |
| 11 | +<a href=https://github.com/code4fukui/DNCL3/>src on GitHub</a> |
| 12 | + |
| 13 | +<style> |
| 14 | +h1 { |
| 15 | + margin: 0; |
| 16 | +} |
| 17 | +.diveditor { |
| 18 | + width: calc(33% - .5em); |
| 19 | + padding: .1em; |
| 20 | + display: inline-block; |
| 21 | + height: calc(100vh - 6em); |
| 22 | +} |
| 23 | +a { |
| 24 | + color: gray !important; |
| 25 | +} |
| 26 | +</style> |
| 27 | + |
| 28 | +<script type="module"> |
| 29 | +import { parseModule } from "https://code4fukui.github.io/acorn-es/parseModule.js"; |
| 30 | +import escodegen from "https://code4fukui.github.io/escodegen/escodegen.js"; |
| 31 | +import { monaco } from "https://code4fukui.github.io/monaco-editor/monaco.js"; |
| 32 | + |
| 33 | + |
| 34 | +export const makeEditor = (div, language) => { |
| 35 | + const editor = monaco.editor.create(div, { |
| 36 | + language, |
| 37 | + autoIndent: true, |
| 38 | + //autoIndent: "none", |
| 39 | + //formatOnPaste: true, |
| 40 | + //formatOnType: true, |
| 41 | + |
| 42 | + suggestOnTriggerCharacters: false, // トリガーキャラクターでの補完を無効化 |
| 43 | + acceptSuggestionOnEnter: 'off', // Enterでの補完選択を無効化 |
| 44 | + parameterHints: false, // パラメータヒントを無効化 |
| 45 | + quickSuggestions: false, // 自動的に補完候補を表示しない |
| 46 | + inlineSuggest: { enabled: false }, // インライン補完を無効化 |
| 47 | + |
| 48 | + tabSize: 2, |
| 49 | + minimap: { enabled: false }, |
| 50 | + //overflow: "auto", |
| 51 | + automaticLayout: true, |
| 52 | + theme: "vs-dark", |
| 53 | + }); |
| 54 | + window.addEventListener("resize", () => { |
| 55 | + editor.layout(); |
| 56 | + }); |
| 57 | + return editor; |
| 58 | +}; |
| 59 | + |
| 60 | +const prog = makeEditor(divprog, "javascript"); |
| 61 | +const ast = makeEditor(divast, "JSON"); |
| 62 | +const prog2 = makeEditor(divprog2, "javascript"); |
| 63 | + |
| 64 | +const removeStartEnd = (json) => { |
| 65 | + for (const name in json) { |
| 66 | + const v = json[name]; |
| 67 | + if (typeof v == "object") { |
| 68 | + removeStartEnd(v); |
| 69 | + } else if (name == "start" || name == "end") { |
| 70 | + delete json[name]; |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +const onchange = () => { |
| 76 | + const src = prog.getValue(); |
| 77 | + try { |
| 78 | + const astjson = parseModule(src); |
| 79 | + removeStartEnd(astjson); |
| 80 | + ast.setValue(JSON.stringify(astjson, null, 2)); |
| 81 | + prog2.setValue(escodegen.generate(astjson)); |
| 82 | + } catch (e) { |
| 83 | + console.log(e); |
| 84 | + //ast.setValue(JSON.stringify(e, null, 2)); |
| 85 | + ast.setValue(e.toString()); |
| 86 | + prog2.setValue(""); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +//prog.onDidChangeModelContent = onchange; // なぜか初回のみ |
| 91 | +let bkval = null; |
| 92 | +setInterval(() => { |
| 93 | + const txt = prog.getValue(); |
| 94 | + if (bkval == txt) return; |
| 95 | + onchange(); |
| 96 | + bkval = txt; |
| 97 | +}, 500); |
| 98 | + |
| 99 | +const firstprog = `const height = 176; |
| 100 | +const weight = 64; |
| 101 | +const height_m = height / 100; |
| 102 | +const bmi = weight / (height_m * height_m); |
| 103 | +console.log("BMIは", bmi); |
| 104 | +if (bmi < 18.5) { console.log("低体重"); } |
| 105 | +if (bmi >= 18.5 && bmi < 25.0) { console.log("普通体重"); } |
| 106 | +if (bmi >= 25.0 && bmi < 30.0) { console.log("肥満(1度)"); } |
| 107 | +if (bmi >= 30.0 && bmi < 35.0) { console.log("肥満(2度)"); } |
| 108 | +if (bmi >= 35.0 && bmi < 40.0) { console.log("肥満(3度)"); } |
| 109 | +if (bmi >= 40.0) { console.log("肥満(4度)"); } |
| 110 | +`; |
| 111 | + |
| 112 | +prog.setValue(firstprog); |
| 113 | +onchange(); |
| 114 | + |
| 115 | +</script> |
| 116 | + |
| 117 | +</body></html> |
0 commit comments