forked from s-macke/jor1k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.html
298 lines (262 loc) · 12.4 KB
/
compile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JOR1K Compiler Demo</title>
<link rel="stylesheet" href="css/default.css">
</head>
<body onload="Start()">
<div class="heading">
<a href="http://opencores.org/or1k/"><img src="images/globe.png"/></a>
<h1>Web-based C-Compiler demo for <a href='.'>jor1k</a></h1>
</div>
<div >
<table>
<tr>
<td style="vertical-align: top">
<table>
<tr><td>
<canvas id="tty" class="screen" width="640" height="384">
Terminal uses canvas
</canvas>
</td></tr>
<tr><td>
<span style="float: left">
<form>VM Control
<select id="coreselect" onchange='jor1kgui.ChangeCore(this.options[this.selectedIndex].value)'>
<option value="asm">asm.js Core</option>
<option value="std">Standard Core</option>
<option value="safe">Safe Core (Slow)</option>
</select>
<button type='button' onclick='term_output="";waitingForActivate=true;jor1kgui.Reset()'>Reset</button>
<button type='button' onclick='jor1kgui.Pause(true)'>Pause</button>
<button type='button' onclick='jor1kgui.Pause(false)'>Run</button>
</form>
<label>
<img src="images/upload.png" width="20px" height="20px" title="Upload to home folder" />
<input type="file" id="files" style="visibility:hidden;width:0; height:0;" name="files[]" onchange='OnUploadFiles(this.files)' multiple />
</label>
<img src="images/tar.png" width="20px" height="20px" onclick='jor1kgui.TAR("home/user")' title="Download of home folder" />
<img src="images/sync.png" width="20px" height="20px" onclick='jor1kgui.Sync("home/user")' title="Sync with server" />
</span>
<span id="stats" style="float: right"></span>
</td></tr>
</table>
</td>
<td style="vertical-align: top">
<div id="code" style="width: 640px; height: 388px; margin-top: 2px">
// jor1k Example: Client-side C Compiler demo
// by Neelabh Gupta and Lawrence Angrave from UIUC
// Compile this program to find the syntax error...
#include <stdio.h>
int main() {
printf("Hello World!\n")
return 0;
}
</div>
gcc options:<input type="text" maxlen=32 id="gccoptions" value="-Wall" /><button type='button' onclick= "RunCode(GetCodeText(),document.getElementById('gccoptions').value);return false;" >Compile and run</button>
<span style="margin: 0 5px 0 5px" id="gcc-compile-status"></span>
<span style="margin: 0 5px 0 5px">Errors: <span id="gcc-error-count">0</span> Warnings: <span id="gcc-warning-count">0</span></span>
Theme<select onchange='editor.setTheme("ace/theme/"+this.options[this.selectedIndex].value)'>
<option value="monokai">monokai</option>
<option value="terminal">terminal</option>
<option value="tomorrow">tomorrow</option>
<option value="xcode">xcode</option>
</select>
</td>
</tr>
</table>
</div>
<div class="infobox">
<h3>About</h3>
<ul>This interactive compile demo is by Neelabh Gupta and Lawrence Angrave from UIUC. It uses the ACE editor and jor1k's' gcc to compile the student's C program.</ul>
<ul>The output of gcc is used to highlight errors in the student's program. </ul>
<br>
<ul>This compiler demo is dual licensed under the GPL v3 and Illinoi/NCSA Open Source license.</ul>
<ul>The ACE editor is licensed under the BSD license.</ul>
<br>
<ul>Lawrence and UIUC students are building a more complete learning environment to playfully learn C and system programming at <a href='http://angrave.github.io/sys'>angrave.github.io/sys</a>.</ul>
<br>
</div>
<script src="http://danml.com/js/download.js"></script>
<script src="js/master/terminal.js"></script>
<script src="js/master/terminal-input.js"></script>
<script src="js/master/ethernet.js"></script>
<script src="js/master/master.js"></script>
<script src="js/lib/ace-editor-src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
function OnUploadFiles(files)
{
for (var i = 0, f; f = files[i]; i++) {
jor1kgui.UploadExternalFile(f);
}
}
function RandomString(length) {
var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
function Start() {
CreateEditor();
var userid = location.search.split('user=')[1];
if (!userid) {
userid = RandomString(10);
window.history.pushState([], "", location + "?user="+userid);
}
jor1kparameters = {
termid: "tty", // canvas id for the terminal
fbid: "fb", // canvas id for the framebuffer
statsid: "stats", // object id for the statistics test
kernelURL: "bin/vmlinux.bin.bz2", // kernel image
basefsURL: "bin/basefs-compile.xml", // xml file with the basic filesystem configuration.
extendedfsURL: ["../jor1k-sysroot/fs.xml"], // xml files with extended filesystem informations. Loaded after the basic filesystem has been loaded.
lazyloadimages: [
"http://jor1k.com/sync/tarballs/"+userid+".tar.bz2",
], // list of automatically loaded images after the basic filesystem has been loaded
relayURL: "", // relay url for the network
userid: userid, // unique user id string. Empty, choosen randomly, from a url, or from a cookie.
syncURL: "http://jor1k.com/sync/upload.php", // url to sync a certain folder
fps: 10, // update interval of framebuffer
memorysize: 32 // in MB, must be a power of two
}
jor1kgui = new jor1kGUI(jor1kparameters);
jor1kgui.term.OnCharReceived = terminalPutCharListener;
}
var term_output = "";
var gcc_output;
var gcc_output_capture_re = /###GCC_COMPILE###\s*([\S\s]*?)\s*###GCC_COMPILE_FINISHED###/;
var gcc_exit_code_capture_re = /GCC_EXIT_CODE: (\d+)/;
var waitingForActivate = true;
function terminalPutCharListener(c) {
term_output = term_output + c;
if(waitingForActivate && (term_output.indexOf('activate this console.') != -1)) {
SendKeysToJor1k("\n");
waitingForActivate = false;
// waitingForActivate hack can be removed once busybox inittab is updated to use respawn.
}
}
function waitForGccCompletion() {
var regex_match_array = gcc_output_capture_re.exec(term_output);
var gcc_exit_code, errorAnnotations;
var warnings_errors_info_count = {
error: 0,
warning: 0,
info: 0
};
if (regex_match_array !== null) {
gcc_output = regex_match_array[1];
gcc_exit_code = parseInt(gcc_exit_code_capture_re.exec(gcc_output)[1]);
SendKeysToJor1k("clear\n");
errorAnnotations = getErrorAnnotations(gcc_output);
editor.getSession().setAnnotations(errorAnnotations);
errorAnnotations.forEach(function (errorAnnotation) {
warnings_errors_info_count[errorAnnotation.type] += 1;
});
document.getElementById('gcc-error-count').innerHTML = warnings_errors_info_count.error.toString();
document.getElementById('gcc-warning-count').innerHTML = warnings_errors_info_count.warning.toString();
if (gcc_exit_code === 0) {
if (warnings_errors_info_count.warning > 0) {
document.getElementById('gcc-compile-status').innerHTML = '<span><a href="#" style="color: darkgoldenrod" onclick="alert(gcc_output); return false;">SUCCESSFUL WITH WARNINGS</a></span>';
}
else {
document.getElementById('gcc-compile-status').innerHTML = '<span style="color: green">SUCCESS</span>';
}
SendKeysToJor1k("./program\n");
}
else {
document.getElementById('gcc-compile-status').innerHTML = '<span><a href="#" style="color: red" onclick="alert(gcc_output); return false;">FAILED</a></span>';
}
}
else {
// output is not ready yet
document.getElementById('gcc-compile-status').innerHTML = '<span style="color: gray">COMPILING</span>';
setTimeout(waitForGccCompletion, 1000);
}
}
var gcc_output_parse_re = /(?:prog\.c|gcc|collect2):\s*(.+)\s*:\s*(.+)\s*/;
var gcc_row_col_type_parse_re = /(\d+):(\d+):\s*(.+)/;
var gcc_output_type_text_split_re = /\s*(.+)\s*:\s*(.+)\s*/;
function getErrorAnnotations(gcc_output_str) {
var match, line_col_type_match, type_text_split_match, row, col, gcc_error_type, ace_annotation_type, text, errors = [];
gcc_output_str.split("\n").forEach(function (error_line) {
if(match = gcc_output_parse_re.exec(error_line)) {
if (line_col_type_match = gcc_row_col_type_parse_re.exec(match[1])) {
row = line_col_type_match[1] - 1; // line numbers in ace start from zero
col = line_col_type_match[2];
if (type_text_split_match = gcc_output_type_text_split_re.exec(line_col_type_match[3])) {
gcc_error_type = type_text_split_match[1];
text = type_text_split_match[2] + ": " + match[2];
} else {
gcc_error_type = line_col_type_match[3];
text = match[2];
}
} else {
// some gcc output without line info
row = col = 0;
if (type_text_split_match = gcc_output_type_text_split_re.exec(match[1])) {
gcc_error_type = type_text_split_match[1];
text = type_text_split_match[2] + ": " + match[2];
} else {
gcc_error_type = match[1];
text = match[2];
}
}
// Determine the type of editor annotation. ace supports error, warning or info.
// This annotation type is also used to determine success of the compilation process.
if (gcc_error_type.toLowerCase().indexOf("error") !== -1) {
ace_annotation_type = "error";
} else if (gcc_error_type.toLowerCase().indexOf("warning") !== -1) {
ace_annotation_type = "warning";
} else {
ace_annotation_type = "info";
}
errors.push({
row: row,
column: col,
type: ace_annotation_type,
text: text
});
}
});
return errors;
}
function CreateEditor()
{
editor = ace.edit("code");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/c_cpp");
}
function GetCodeText() {
//return document.getElementById('code').value; // textarea
return editor.getSession().getValue(); //ace
}
function SendKeysToJor1k(text,quiet) {
var tty = quiet ? "tty1" : "tty0";
for(var i=0;i<text.length;i++) {
jor1kgui.SendToWorker(tty, [text.charCodeAt(i) >>>0]);
}
}
function RunCode(code,gccoptions) {
if(code.length ===0 || code.indexOf("\x03") >= 0 || code.indexOf("\x04") >= 0 ) return;
jor1kgui.Pause(false);
// wakeup tty0
SendKeysToJor1k("\x03\n\nstty -clocal\nstty crtscts\nstty -ixoff\n",false);
// wakeup hidden tty1
//tty1-unusedfornow SendKeysToJor1k("\x03\n",true);
//SendKeysToJor1k("cd ~;rm prog.c program 2>/dev/null\n");
var bufView = new Uint8Array(code.length);
for (var i=0; i<code.length; i++) {
bufView[i] = code.charCodeAt(i);
}
jor1kgui.SendToWorker("MergeFile", {name: "home/user/prog.c", data: bufView});
// For now we want gcc output to be viewable, so this goes on the main terminal
// To use tty1 we would need to synchronize gcc i.e. wait for prog.c upload
// not-reliable SendKeysToJor1k("stty echo\n\nclear\n");
term_output = "";
SendKeysToJor1k("echo \\#\\#\\#GCC_COMPILE\\#\\#\\#;clear;gcc " + gccoptions + " prog.c -o program; echo GCC_EXIT_CODE: $?; echo \\#\\#\\#GCC_COMPILE_FINISHED\\#\\#\\#;clear\n");
waitForGccCompletion();
}
</script>
</body>
</html>