-
Notifications
You must be signed in to change notification settings - Fork 12
/
coliru.js
216 lines (170 loc) · 8.26 KB
/
coliru.js
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
/*
Coliru compiler web service JavaScript library.
@licstart The following is the entire license notice for the
Javascript code in this page.
Copyright (C) 2014 Alexander Lamaison
The Javascript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the Javascript code in this page.
*/
'use strict';
var coliru = (function() {
function displayColiruOutput(codeBlock, compileArea) {
// Remove button so the compile-display process can
// only happen once
compileArea.removeChild(compileArea.firstChild);
var sourceCode = codeBlock.textContent;
coliru.compile(coliru.makeSourceRunnable(sourceCode),
function(response, state) {
if (state == 'running') {
compileArea.textContent = 'Running...\n';
}
else if (state == 'error') {
compileArea.textContent = 'Error: ' + reponse;
}
else {
compileArea.textContent = '';
if (response != '') {
var outputArea = document.createElement('pre');
compileArea.appendChild(outputArea);
outputArea.className += ' coliru-output';
// Pygments output formatting
outputArea.className += ' go';
outputArea.textContent = response;
}
var status = document.createElement('div');
compileArea.appendChild(status);
status.className += ' coliru-status';
if (state == 0) {
status.textContent = 'Success';
}
else {
status.textContent =
'Failed (return code ' + state + ')';
}
var credit = document.createElement('div');
compileArea.appendChild(credit);
credit.className += ' coliru-credit';
credit.innerHTML += [
'Powered by <a ',
'href="http://coliru.stacked-crooked.com/">',
'Coliru</a>'
].join('');
}
// Allow style to change formatting to reflect state
compileArea.setAttribute('data-coliru-state', state);
});
}
function createCompileButton(compileArea, compileAction) {
var compileButton = document.createElement('span');
compileButton.textContent = 'Run this code';
compileButton.className += ' runbutton';
compileButton.onclick = compileAction;
compileButton.onkeydown = compileAction;
compileArea.appendChild(compileButton);
}
return {
compile: function(sourceCode, compileReadyResponse) {
var linkOptions = '';
for (var i=0; i < coliru.linkLibraries.length; ++i) {
linkOptions += '-l' + coliru.linkLibraries[i] + ' ';
}
var compileCommand = [
//'g++-4.8 -std=c++11 ',
'clang++ -std=c++11 ',
//'clang++ -std=c++11 -stdlib=libc++ -lsupc++ ',
'-O2 -Wall -pedantic -pthread ',
'main.cpp ',
linkOptions,
'&& ./a.out; echo COLIRUSTATUS $?'].join('');
var coliruConnection = new XMLHttpRequest();
coliruConnection.onreadystatechange = function() {
if (coliruConnection.readyState == 4) {
if (coliruConnection.status != 200) {
compileReadyResponse(coliruConnection.response, 'error');
}
else {
// The command above wil *always* output the last return code
// at the end of the command. We extract it and use it as
// the status
var rawResponse = coliruConnection.response;
var pattern = /^([\s\S]*)COLIRUSTATUS (\d+)\n$/m;
var splitResponse = rawResponse.match(pattern);
compileReadyResponse(splitResponse[1],
parseInt(splitResponse[2]));
}
}
else {
compileReadyResponse(coliruConnection.response, 'running');
}
}
coliruConnection.open("POST",
"http://coliru.stacked-crooked.com/compile",
true);
coliruConnection.send(
JSON.stringify({ "cmd": compileCommand, "src": sourceCode }));
},
linkLibraries: [],
magicIncludes: [],
makeSourceRunnable: function(sourceCode) {
if (coliru.containsMainMethod(sourceCode)) {
return sourceCode;
}
else {
var runnableSourceCode = '';
for (var i=0; i < coliru.magicIncludes.length; ++i) {
runnableSourceCode += '#include <'
+ coliru.magicIncludes[i] + '>\n';
}
// No return needed (see C++ spec)
runnableSourceCode += 'int main() {\n' + sourceCode + '\n}';
return runnableSourceCode;
}
},
containsMainMethod: function(sourceCode) {
return sourceCode.match(/int\s+main\([^\)]*\)\s*[\{;]/);
},
// Expects codeBlock to be preceded by a <pre>
createCompileArea: function(codeBlock) {
var compileArea = document.createElement('div');
// Allow coliru-specific formatting
compileArea.className += ' coliru';
// Formatting for coliru before it has run the code
compileArea.setAttribute('data-coliru-state', 'not-run');
createCompileButton(compileArea,
function(codeBlock, compileArea) {
return function() {
displayColiruOutput(codeBlock,
compileArea);
}
}(codeBlock, compileArea));
var preCode = codeBlock.parentNode;
preCode.parentNode.insertBefore(compileArea, preCode.nextSibling);
},
// Onlu update code block inside a <pre> tag and with
// data-lang=c++ attribute
updateCodeBlock: function(codeBlock) {
if (codeBlock.getAttribute('data-lang') == 'c++' &&
codeBlock.parentNode.tagName == 'PRE') {
coliru.createCompileArea(codeBlock);
}
},
addRunButtonsToCodeBlocks: function() {
var els = document.getElementsByTagName('code');
for (var i = 0; i < els.length; ++i) {
coliru.updateCodeBlock(els[i]);
}
},
}
})()