Something went wrong. Try again.
Interpreter for the i++ programming language
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200<script lang="ts"> interface Props { value?: string; onrun?: () => void; }
let { value = $bindable(''), onrun }: Props = $props();
function fastEscape(str: string): string { return str .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>'); }
function highlightLine(line: string): string { const tokenRe = /(import|export)(?![A-Za-z0-9_'])|([A-Za-z][A-Za-z0-9_']*)|(\d+\.?\d*(?:j\b)?)|([()])|(=)/g; let out = ''; let last = 0;
for (const m of line.matchAll(tokenRe)) { out += fastEscape(line.slice(last, m.index)); const [tok, kw, name, num, paren, define] = m; if (kw) { out += `<span class="tok-kw">${kw}</span>`; } else if (name) { if (name === 'i') { out += `<span class="tok-i">${name}</span>`; } else { out += `<span class="tok-var">${name}</span>`; } } else if (num) { out += `<span class="tok-num">${num}</span>`; } else if (paren) { out += `<span class="tok-paren">${paren}</span>`; } else if (define) { out += `<span class="tok-define">${define}</span>`; } else { out += fastEscape(tok); } last = m.index + tok.length; }
out += fastEscape(line.slice(last)); return out; }
function highlight(code: string): string { return code .split('\n') .map((line) => { const hashIdx = line.indexOf('#'); if (hashIdx === -1) { return highlightLine(line); } return ( highlightLine(line.slice(0, hashIdx)) + `<span class="tok-comment">${fastEscape(line.slice(hashIdx))}</span>` ); }) .join('\n'); }
let editorScroll: HTMLDivElement | undefined = $state(); let lineNumbers: HTMLDivElement | undefined = $state();
function syncScroll() { if (editorScroll && lineNumbers) { lineNumbers.scrollTop = editorScroll.scrollTop; } }
function onKeydown(e: KeyboardEvent) { if (e.key === 'Enter' && e.shiftKey) { e.preventDefault(); onrun?.(); } }
const highlighted = $derived(highlight(value) + (value.endsWith('\n') ? '<br> ' : '')); const lineCount = $derived(value.split('\n').length);</script>
<div class="editor"> <div class="line-numbers" bind:this={lineNumbers}> {#each { length: lineCount } as _, i} <span>{i + 1}</span> {/each} </div> <div class="editor-scroll" bind:this={editorScroll} onscroll={syncScroll}> <div class="editor-layer"> <pre class="editor-code"><code>{@html highlighted}</code></pre> <textarea class="editor-input" bind:value onkeydown={onKeydown} spellcheck="false" placeholder="Type i++ code here… (Shift+Enter to run)" ></textarea> </div> </div></div>
<style> .editor { display: flex; flex: 1; overflow: hidden; background: #111827; border: 1px solid #334155; border-radius: 8px; font-size: 0.9rem; line-height: 1.6; }
.line-numbers { width: 2.75rem; padding: 1rem 0.5rem; text-align: right; color: #64748b; overflow: hidden; user-select: none; flex-shrink: 0; border-right: 1px solid #334155; background: rgba(255, 255, 255, 0.015); }
.line-numbers span { display: block; }
.editor-scroll { flex: 1; overflow-x: hidden; overflow-y: auto; position: relative; }
.editor-layer { position: relative; min-height: 100%; }
.editor-code { position: relative; margin: 0; padding: 1rem 1.25rem; font-family: inherit; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; pointer-events: none; color: #cbd5e1; min-height: 100%; tab-size: 2; }
.editor-input { position: absolute; top: 0; left: 0; width: 100%; height: 100%; padding: 1rem 1.25rem; margin: 0; font-family: inherit; font-size: inherit; line-height: inherit; background: transparent; color: transparent; caret-color: #cbd5e1; border: none; outline: none; resize: none; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; z-index: 2; tab-size: 2; }
.editor-input::selection { background: rgba(56, 189, 248, 0.2); color: transparent; }
.editor-input::placeholder { color: #64748b; opacity: 0.4; }
:global(.tok-i) { color: #34d399; font-weight: 600; } :global(.tok-var) { color: #fbbf24; } :global(.tok-kw) { color: #38bdf8; font-weight: 600; } :global(.tok-num) { color: #f472b6; } :global(.tok-paren) { color: #94a3b8; } :global(.tok-define) { color: #fb923c; } :global(.tok-comment) { color: #64748b; font-style: italic; }</style>