|
| 1 | +--- |
| 2 | +title: Playground |
| 3 | +--- |
| 4 | + |
| 5 | +<!DOCTYPE HTML> |
| 6 | +<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> |
| 7 | + |
| 8 | +<head> |
| 9 | + |
| 10 | +<title>ChaiScript - Interactive Playground</title> |
| 11 | +{% include common.html %} |
| 12 | + |
| 13 | +<style> |
| 14 | + .playground-wrap { |
| 15 | + display: flex; |
| 16 | + gap: 0; |
| 17 | + min-height: 500px; |
| 18 | + border: 1px solid #ddd; |
| 19 | + border-radius: 4px; |
| 20 | + overflow: hidden; |
| 21 | + } |
| 22 | + .playground-panel { |
| 23 | + flex: 1; |
| 24 | + display: flex; |
| 25 | + flex-direction: column; |
| 26 | + min-width: 0; |
| 27 | + } |
| 28 | + .playground-panel-header { |
| 29 | + background: #f5f5f5; |
| 30 | + padding: 6px 12px; |
| 31 | + font-size: 0.8rem; |
| 32 | + text-transform: uppercase; |
| 33 | + letter-spacing: 0.1em; |
| 34 | + color: #888; |
| 35 | + border-bottom: 1px solid #ddd; |
| 36 | + } |
| 37 | + .playground-divider { |
| 38 | + width: 2px; |
| 39 | + background: #ddd; |
| 40 | + } |
| 41 | + #chai-input { |
| 42 | + flex: 1; |
| 43 | + background: #fff; |
| 44 | + color: #333; |
| 45 | + border: none; |
| 46 | + padding: 12px; |
| 47 | + font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace; |
| 48 | + font-size: 0.9rem; |
| 49 | + line-height: 1.6; |
| 50 | + resize: none; |
| 51 | + outline: none; |
| 52 | + tab-size: 2; |
| 53 | + } |
| 54 | + #chai-output { |
| 55 | + flex: 1; |
| 56 | + background: #fafafa; |
| 57 | + padding: 12px; |
| 58 | + font-family: "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace; |
| 59 | + font-size: 0.9rem; |
| 60 | + line-height: 1.6; |
| 61 | + overflow-y: auto; |
| 62 | + white-space: pre-wrap; |
| 63 | + word-break: break-all; |
| 64 | + } |
| 65 | + .chai-output-line { color: #333; } |
| 66 | + .chai-output-error { color: #d9534f; } |
| 67 | + #chai-status { font-size: 0.85rem; } |
| 68 | + #chai-status.ready { color: #5cb85c; } |
| 69 | + #chai-status.loading { color: #f0ad4e; } |
| 70 | + #chai-status.error { color: #d9534f; } |
| 71 | + .playground-controls { |
| 72 | + margin-top: 10px; |
| 73 | + display: flex; |
| 74 | + gap: 8px; |
| 75 | + align-items: center; |
| 76 | + } |
| 77 | + .playground-controls .hint { |
| 78 | + margin-left: auto; |
| 79 | + font-size: 0.75rem; |
| 80 | + color: #999; |
| 81 | + } |
| 82 | +</style> |
| 83 | + |
| 84 | +</head> |
| 85 | + |
| 86 | +<body> |
| 87 | + |
| 88 | +{% include header.html %} |
| 89 | + |
| 90 | +<div class="well well-sm"> |
| 91 | + <h3>Interactive Playground <span id="chai-status" class="loading">Loading…</span></h3> |
| 92 | + <p>Write ChaiScript code and run it directly in your browser using WebAssembly.</p> |
| 93 | +</div> |
| 94 | + |
| 95 | +<div class="body-with-margin"> |
| 96 | + <div class="playground-wrap"> |
| 97 | + <div class="playground-panel"> |
| 98 | + <div class="playground-panel-header">Input</div> |
| 99 | + <textarea id="chai-input" spellcheck="false">// Welcome to ChaiScript! |
| 100 | +// Write your code here and click Run (or press Ctrl+Enter). |
| 101 | + |
| 102 | +def greet(name) { |
| 103 | + return "Hello, " + name + "!" |
| 104 | +} |
| 105 | + |
| 106 | +print(greet("World")) |
| 107 | +print(greet("ChaiScript")) |
| 108 | + |
| 109 | +// Math example |
| 110 | +def factorial(n) { |
| 111 | + if (n <= 1) { return 1 } |
| 112 | + return n * factorial(n - 1) |
| 113 | +} |
| 114 | + |
| 115 | +print("5! = " + to_string(factorial(5))) |
| 116 | +print("10! = " + to_string(factorial(10))) |
| 117 | +</textarea> |
| 118 | + </div> |
| 119 | + <div class="playground-divider"></div> |
| 120 | + <div class="playground-panel"> |
| 121 | + <div class="playground-panel-header">Output</div> |
| 122 | + <div id="chai-output"></div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + |
| 126 | + <div class="playground-controls"> |
| 127 | + <button id="btn-run" class="btn btn-danger" disabled>Run</button> |
| 128 | + <button id="btn-clear" class="btn btn-default">Clear</button> |
| 129 | + <span class="hint">Ctrl+Enter to run</span> |
| 130 | + </div> |
| 131 | +</div> |
| 132 | + |
| 133 | +<script> |
| 134 | + var outputEl = document.getElementById('chai-output'); |
| 135 | + var inputEl = document.getElementById('chai-input'); |
| 136 | + var btnRun = document.getElementById('btn-run'); |
| 137 | + var btnClear = document.getElementById('btn-clear'); |
| 138 | + var statusEl = document.getElementById('chai-status'); |
| 139 | + |
| 140 | + function appendOutput(text, className) { |
| 141 | + var line = document.createElement('div'); |
| 142 | + line.className = className || 'chai-output-line'; |
| 143 | + line.textContent = text; |
| 144 | + outputEl.appendChild(line); |
| 145 | + outputEl.scrollTop = outputEl.scrollHeight; |
| 146 | + } |
| 147 | + |
| 148 | + var Module = { |
| 149 | + print: function(text) { |
| 150 | + appendOutput(text); |
| 151 | + }, |
| 152 | + printErr: function(text) { |
| 153 | + appendOutput(text, 'chai-output-error'); |
| 154 | + }, |
| 155 | + onRuntimeInitialized: function() { |
| 156 | + statusEl.textContent = 'Ready'; |
| 157 | + statusEl.className = 'ready'; |
| 158 | + btnRun.disabled = false; |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + function runCode() { |
| 163 | + var code = inputEl.value; |
| 164 | + if (!code.trim()) { return; } |
| 165 | + |
| 166 | + appendOutput('> Running...', 'chai-output-line'); |
| 167 | + try { |
| 168 | + Module.eval(code); |
| 169 | + } catch (e) { |
| 170 | + appendOutput('Error: ' + e.message, 'chai-output-error'); |
| 171 | + } |
| 172 | + appendOutput('', 'chai-output-line'); |
| 173 | + } |
| 174 | + |
| 175 | + btnRun.addEventListener('click', runCode); |
| 176 | + btnClear.addEventListener('click', function() { |
| 177 | + outputEl.innerHTML = ''; |
| 178 | + }); |
| 179 | + |
| 180 | + inputEl.addEventListener('keydown', function(e) { |
| 181 | + if (e.ctrlKey && e.key === 'Enter') { |
| 182 | + e.preventDefault(); |
| 183 | + runCode(); |
| 184 | + } |
| 185 | + if (e.key === 'Tab') { |
| 186 | + e.preventDefault(); |
| 187 | + var start = this.selectionStart; |
| 188 | + var end = this.selectionEnd; |
| 189 | + this.value = this.value.substring(0, start) + ' ' + this.value.substring(end); |
| 190 | + this.selectionStart = this.selectionEnd = start + 2; |
| 191 | + } |
| 192 | + }); |
| 193 | +</script> |
| 194 | +<script src="/playground/chaiscript.js"></script> |
| 195 | + |
| 196 | +</body> |
0 commit comments