forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
143 lines (134 loc) · 6.52 KB
/
Copy pathindex.html
File metadata and controls
143 lines (134 loc) · 6.52 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nulang Playground</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/material-darker.min.css">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #1a1a2e; color: #e0e0e0; display: flex; flex-direction: column; height: 100vh; }
header { background: #16213e; padding: 12px 20px; display: flex; align-items: center; gap: 12px; border-bottom: 1px solid #0f3460; }
header h1 { font-size: 20px; color: #e94560; margin-right: auto; }
header select, header button { padding: 6px 12px; border-radius: 4px; border: 1px solid #0f3460; background: #0f3460; color: #e0e0e0; cursor: pointer; font-size: 13px; }
header button:hover { background: #1a4a8a; }
header button.run { background: #e94560; border-color: #e94560; font-weight: bold; }
header button.run:hover { background: #ff6b81; }
main { display: flex; flex: 1; overflow: hidden; }
#editor { flex: 1; height: 100%; }
#output { width: 380px; background: #0d1117; border-left: 1px solid #0f3460; padding: 16px; overflow-y: auto; font-family: 'Fira Code', 'Cascadia Code', monospace; font-size: 13px; white-space: pre-wrap; word-break: break-all; }
#output .out { color: #c9d1d9; }
#output .err { color: #f85149; }
#output .info { color: #58a6ff; }
#output .empty { color: #484f58; font-style: italic; }
.CodeMirror { height: 100% !important; font-size: 14px; }
#status { font-size: 12px; color: #58a6ff; padding: 0 8px; }
.cm-s-material-darker .cm-keyword { color: #c792ea; }
.cm-s-material-darker .cm-string { color: #c3e88d; }
.cm-s-material-darker .cm-number { color: #f78c6c; }
.cm-s-material-darker .cm-comment { color: #546e7a; }
.cm-s-material-darker .cm-builtin { color: #82aaff; }
</style>
</head>
<body>
<header>
<h1>Nulang Playground</h1>
<select id="examples">
<option value="">— Examples —</option>
<option value="perform IO.print("Hello, Nulang!")">Hello World</option>
<option value="let fib = fn(n) { if n <= 1 then n else fib(n - 1) + fib(n - 2) } in fib(10)">Fibonacci</option>
<option value="type Option[T] = Some(T) | None let x = Some(42) in match x with { | Some(n) => n | None => 0 }">Option Match</option>
<option value="let add = fn(x, y) { x + y } in 5 |> add(3)">Pipe Operator</option>
<option value="perform String.length("hello")">String Length</option>
<option value="perform String.concat("Hello", " World")">String Concat</option>
<option value="perform Debug.inspect("x", 42 * 2)">Debug Inspect</option>
<option value="match 42 { case 1 => 10 case 42 => 100 case _ => 0 }">Match Expression</option>
<option value="let inc = fn(n) { n + 1 } in inc(41)">Closure</option>
<option value="perform IO.log("info", "server started")">IO.log</option>
</select>
<button class="run" onclick="run()">▶ Run (Ctrl+Enter)</button>
<button onclick="share()">🔗 Share</button>
<span id="status"></span>
</header>
<main>
<div id="editor"></div>
<div id="output"><span class="empty">Output appears here — click ▶ Run</span></div>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/clike/clike.min.js"></script>
<script>
// Nulang syntax mode for CodeMirror
CodeMirror.defineSimpleMode('nulang', {
start: [
{ regex: /\/\/.*/, token: 'comment' },
{ regex: /"(?:[^\\]|\\.)*?"/, token: 'string' },
{ regex: /\b(fn|let|rec|in|if|then|else|match|with|case|handle|resume|perform|spawn|send|ask|actor|behavior|state|type|effect|workflow|import|module|pub|self|unit|nil|true|false|and|or|receive|after)\b/, token: 'keyword' },
{ regex: /\b(Int|Float|Bool|String|Unit|Nil|Actor|Option|Result|Some|None|Ok|Error|IO|Debug|Timer|Signal|Inference|LLM|Otp)\b/, token: 'builtin' },
{ regex: /0[xX][0-9a-fA-F]+/, token: 'number' },
{ regex: /[0-9]+\.[0-9]+/, token: 'number' },
{ regex: /[0-9]+/, token: 'number' },
{ regex: /[+\-*\/%<>=!|&^]+|->|=>|\|>/, token: 'operator' },
{ regex: /[a-zA-Z_][a-zA-Z0-9_]*/, token: 'variable' },
]
});
const editor = CodeMirror(document.getElementById('editor'), {
value: 'perform IO.print("Hello, Nulang!")',
mode: 'nulang',
theme: 'material-darker',
lineNumbers: true,
indentUnit: 4,
tabSize: 4,
matchBrackets: true,
autoCloseBrackets: true,
});
// Load from URL hash
if (location.hash) {
try {
const code = decodeURIComponent(location.hash.slice(1));
if (code) editor.setValue(code);
} catch(e) {}
}
// Example dropdown
document.getElementById('examples').addEventListener('change', function() {
if (this.value) editor.setValue(this.value);
});
// Ctrl+Enter to run
editor.addKeyMap({ 'Ctrl-Enter': run });
async function run() {
const code = editor.getValue();
const out = document.getElementById('output');
const status = document.getElementById('status');
out.innerHTML = '<span class="info">Running...</span>';
status.textContent = 'running...';
try {
const resp = await fetch('/run', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code }),
});
const data = await resp.json();
let html = '';
if (data.stdout) html += '<span class="out">' + escapeHtml(data.stdout) + '</span>';
if (data.stderr) html += '<span class="err">' + escapeHtml(data.stderr) + '</span>';
if (!data.stdout && !data.stderr) html = '<span class="empty">(no output)</span>';
out.innerHTML = html;
status.textContent = data.ok ? 'ok' : 'error';
status.style.color = data.ok ? '#3fb950' : '#f85149';
} catch(e) {
out.innerHTML = '<span class="err">Connection error: ' + escapeHtml(e.message) + '</span>';
status.textContent = 'offline';
status.style.color = '#f85149';
}
}
function share() {
const code = editor.getValue();
const url = location.origin + location.pathname + '#' + encodeURIComponent(code);
navigator.clipboard.writeText(url).then(() => {
document.getElementById('status').textContent = 'URL copied!';
});
}
function escapeHtml(s) { return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }
</script>
</body>
</html>