Something went wrong. Try again.
Interpreter for the i++ programming language
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280<script lang="ts"> import { onMount } from 'svelte'; import { loadWasm, getWasm, type WasmModule } from './wasm'; import Editor from './Editor.svelte';
let source = $state(''); let output: { input: string; result: string; error: boolean }[] = $state([]); let rawMode = $state(false); let wasmReady = $state(false); let wasmError = $state(''); let status = $state('Loading WASM…');
const stdLib: Record<string, string> = { '[Combinators]': `export I = i iexport K = i (i I)export S = i K
export KI = K I
export T = S (K (S I)) Kexport B = S (K S) Kexport D = B Bexport C = S (D S) (K K)
export M = S I I`, '[Math]': `import Combinators
export EXP = T 1export E = EXP 1export LN = B (B 1 EXP) 1export ZERO = LN 1
export MINUS = B C D LN EXPexport NEG = MINUS 0export PLUS = B C D MINUS NEG
PLUS 5 3MINUS 5 3NEG 5`, };
onMount(async () => { try { await loadWasm(); wasmReady = true; status = 'WASM loaded'; } catch (e) { wasmReady = false; wasmError = e instanceof Error ? e.message : String(e); status = 'WASM failed'; } });
function run() { const wasm = getWasm(); if (!wasmReady || !wasm) return;
if (wasm.reset_engine) { wasm.reset_engine(); }
const fn: ((input: string) => string) | undefined = rawMode ? wasm.parse_raw : wasm.parse;
if (!fn) { wasmError = rawMode ? 'Raw mode requires a newer WASM build.' : 'Parser not available.'; return; }
const lines = source.split('\n'); const results: { input: string; result: string; error: boolean }[] = [];
for (const line of lines) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; const result = fn(trimmed); const error = result.startsWith('Error:'); results.push({ input: trimmed, result: error ? result.replace(/^Error:\s*/, '') : result, error }); }
output = results; }
function loadExample(value: string) { if (!value || value === '---') return; source = stdLib[value] ?? value; }</script>
<div class="repl"> <div class="toolbar"> <select onchange={(e) => loadExample(e.currentTarget.value)}> <option value="" selected>Explore…</option> <option value="[Combinators]">Combinators</option> <option value="[Math]">Math</option> <option value="---" disabled>— expressions —</option> <option value="K x y">K combinator</option> <option value="S K K x">Identity (SKK)</option> <option value="B B">Nested B</option> <option value="T T T">Nested T</option> <option value="i i i">Iota reduction</option> </select>
<div class="mode"> <button class:active={!rawMode} onclick={() => (rawMode = false)}>λ</button> <button class:active={rawMode} onclick={() => (rawMode = true)}>raw</button> </div>
<button onclick={run} disabled={!wasmReady}>Run (Shift+Enter)</button> <button onclick={() => { source = ''; output = []; }}>Clear</button> </div>
<div class="workspace"> <Editor bind:value={source} onrun={run} />
<div class="output"> {#if wasmError} <div class="error">{wasmError}</div> {:else if output.length === 0} <div class="placeholder"> <p>Welcome to the <strong>i++</strong> REPL.</p> <ul> <li><code>i</code> — universal iota combinator</li> <li><code>(X Y)</code> — application</li> <li><code>DEF = ...</code> — definition</li> <li><code>import Combinators</code> — load standard combinators</li> </ul> </div> {:else} {#each output as line} <div class="line"> <div class="in">> {line.input}</div> <div class="out" class:error={line.error}>{line.result}</div> </div> {/each} {/if} </div> </div>
<div class="status" class:ok={wasmReady}>{status}</div></div>
<style> .repl { display: flex; flex-direction: column; gap: 0.75rem; height: calc(100vh - 8rem); }
.toolbar { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.toolbar select, .toolbar button { font-family: inherit; font-size: 0.85rem; padding: 0.4rem 0.75rem; border-radius: 6px; border: 1px solid #334155; background: #1e293b; color: #cbd5e1; cursor: pointer; }
.toolbar button:hover { background: #27354f; }
.toolbar button:disabled { opacity: 0.5; cursor: not-allowed; }
.mode { display: flex; gap: 0; border-radius: 6px; overflow: hidden; border: 1px solid #334155; }
.mode button { border: none; border-radius: 0; background: #111827; color: #64748b; }
.mode button.active { background: #0ea5e9; color: #0f172a; }
.workspace { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; flex: 1; min-height: 0; }
@media (max-width: 768px) { .workspace { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; } }
.output { background: #111827; border: 1px solid #334155; border-radius: 8px; padding: 1rem; font-family: inherit; font-size: 0.9rem; line-height: 1.6; color: #cbd5e1; }
.output { overflow: auto; }
.placeholder { color: #64748b; }
.placeholder ul { margin: 0.5rem 0; padding-left: 1.25rem; }
.placeholder code { background: #1e293b; padding: 0.1rem 0.35rem; border-radius: 4px; color: #38bdf8; }
.line { margin-bottom: 1rem; }
.in { color: #64748b; font-size: 0.8rem; margin-bottom: 0.25rem; }
.out { color: #cbd5e1; white-space: pre-wrap; }
.out.error, .error { color: #f87171; }
.status { font-size: 0.75rem; color: #64748b; }
.status.ok { color: #34d399; }</style>