-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler-explorer.js
94 lines (88 loc) · 3.09 KB
/
compiler-explorer.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
(function () {
const ce_nodes = document.querySelectorAll('code.lang-cpp');
for (let i = 0, len = ce_nodes.length; i < len; i++) {
let element = ce_nodes[i];
let compiler = "g82";
let options = "-O2 -march=haswell";
let source = unescape(element.textContent);
let lines = source.split('\n');
source = "";
let displaySource = "";
const configMatcher = /^\s*\/\/\/\s*([^:]+):(.*)$/;
const hideMatcher = /^\s*\/\/\/\s*((un)?hide)$/;
let skipDisplay = false;
let hide = false;
for (let idx = 0; idx < lines.length; ++idx) {
let line = lines[idx];
let match = line.match(configMatcher);
if (match) {
compiler = match[1];
options = match[2];
} else {
match = line.match(hideMatcher);
if (match) {
hide = match[1] === "hide";
continue;
}
if (line === "// setup") {
skipDisplay = true;
} else if (line[0] !== ' ') {
skipDisplay = false;
}
source += line + "\n";
if (!skipDisplay && !hide)
displaySource += line + "\n"
if (line.length > 36) {
console.error(`Line too long: "${line}"`);
}
}
}
function trim(source) {
while (source.startsWith("\n")) {
source = source.slice(1, source.length);
}
while (source.endsWith("\n\n")) {
source = source.slice(0, source.length - 1);
}
return source;
}
displaySource = trim(displaySource);
source = trim(source);
options += " -Wall -Wextra -pedantic";
let content = [];
content.push({
type: 'component',
componentName: 'codeEditor',
componentState: {
id: 1,
source: source,
options: {compileOnChange: true, colouriseAsm: true},
fontScale: 2.5
}
});
content.push({
type: 'component',
componentName: 'compiler',
componentState: {
source: 1,
filters: {commentOnly: true, directives: true, intel: true, labels: true, trim: true},
options: options,
compiler: compiler,
fontScale: 3.0
}
});
let obj = {
version: 4,
content: [{type: 'row', content: content}]
};
let ceFragment = encodeURIComponent(JSON.stringify(obj));
const isLocal = !!window.location.host.match(/localhost/gi);
const baseUrl = isLocal ? 'http://localhost:10240/' : 'https://godbolt.org/';
element.onclick = (evt) => {
if (evt.ctrlKey) {
window.location.assign(baseUrl + "#" + ceFragment);
}
};
element.textContent = displaySource;
}
})();