-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (122 loc) · 3.88 KB
/
index.html
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<link rel="icon" href="data:,">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<link rel="stylesheet" href="lib/codemirror.css">
<script src="lib/codemirror.min.js"></script>
<script src="addon/edit/matchbrackets.js"></script>
<script src="addon/mode/simple.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap" rel="stylesheet">
<style>
textarea {
width: 100%;
height: auto;
white-space: pre;
overflow-wrap: normal;
overflow-x: scroll;
font-family: "JetBrains Mono", monospace;
}
.CodeMirror {
border: 1px solid black;
font-family: "JetBrains Mono", monospace;
}
#wrapper {
display: flex;
}
#left {
flex: 0 0 40%;
}
#right {
flex: 1;
}
</style>
Program:
<div><textarea id="code" name="code"></textarea></div>
<p></p>
Program input: (passed to <code>main</code> as <code>Str</code>)
<div><textarea id="programInput" name="programInput" rows=10></textarea></div>
<p></p>
<button onclick="run(editor.getValue(), input.value)">Run</button>
<p></p>
Program output:
<div><textarea readonly id="programOutput" name="programOutput"></textarea></div>
<p></p>
Interpreter output: (errors, crashes etc.)
<div><textarea readonly id="interpreterOutput" name="interpreterOutput"></textarea></div>
<p id="version"></p>
<script>
CodeMirror.defineSimpleMode("simplemode", {
// The start state contains the rules that are initially used
start: [
{regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string"},
{regex: /(?:elif|else|fn|for|if|in|let|match|return|self|trait|type|var|while|import)\b/,
token: "keyword"},
{regex: /0x[a-f\d]+|[-+]?(?:\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/i,
token: "number"},
{regex: /[a-z][a-zA-Z0-9_]*/,
token: "variable"},
{regex: /[A-Z][a-zA-Z0-9_]*/,
token: "type"},
{regex: /#.*/, token: "comment"},
],
meta: {
dontIndentStates: ["comment"],
lineComment: "#"
}
});
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
indentUnit: 2,
mode: "simplemode",
});
var input = document.getElementById("programInput");
editor.setSize(null, 800);
function resizeTextArea(textarea) {
var numLines = textarea.innerHTML.split('\n').length;
textarea.rows = numLines;
}
function clearProgramOutput() {
var textarea = document.getElementById('programOutput');
textarea.innerHTML = '';
resizeTextArea(textarea);
}
function addProgramOutput(output) {
var textarea = document.getElementById('programOutput');
textarea.innerHTML += output;
textarea.innerHTML += '\n';
resizeTextArea(textarea);
}
function clearInterpreterOutput() {
var textarea = document.getElementById('interpreterOutput');
textarea.innerHTML = '';
resizeTextArea(textarea);
}
function addInterpreterOutput(output) {
var textarea = document.getElementById('interpreterOutput');
textarea.innerHTML += output;
textarea.innerHTML += '\n';
resizeTextArea(textarea);
}
</script>
<script type="module">
import init, { run, setupPanicHook, version } from './fir.js';
await init();
await setupPanicHook();
window.run = function(pgm, input) {
try {
run(pgm, input);
} catch (err) {
addInterpreterOutput(err);
addInterpreterOutput(err.stack);
}
}
// Show version info at the end of the page.
document.getElementById("version").innerHTML = version();
// Initially load ArithmeticExpressions.fir as the code.
fetch(new Request('ArithmeticExpressions.fir'))
.then((response) => response.text())
.then((text) => editor.setValue(text));
</script>