-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.html
162 lines (160 loc) · 6.89 KB
/
interpreter.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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>YASL Demo</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div>
<h1>YASL Demo</h1>
<p> Try YASL in your browser before downloading it. Enter your YASL program below or choose one of the sample programs. </p>
<div>
<button onclick="YASL_helloworld();" type="button">hello world</button>
<button onclick="YASL_fact();" type="button">factorial</button>
<button onclick="YASL_twosum();" type="button">two sum</button>
</div>
</div>
<div>
<textarea style="resize:none;" cols="80" rows="30" id="input" placeholder="Please enter some YASL..."></textarea>
<textarea style="resize:none;" cols="80" rows="30" id="output" readonly ></textarea>
<br/>
<div class="emscripten" id="status"><p>Downloading...</p></div>
<!-- <button onclick="YASL();" type="button">Run it!</button> -->
</div>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var Module = {
preRun: [],
postRun: [],
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
// These replacements are necessary if you render to raw HTML
//text = text.replace(/&/g, "&");
//text = text.replace(/</g, "<");
//text = text.replace(/>/g, ">");
//text = text.replace('\n', '<br>', 'g');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
}
if (text) {
statusElement.innerHTML = text;
} else {
var button = document.createElement("button");
button.innerHTML = "Run it!";
button.onclick = YASL;
statusElement.replaceChild(button, statusElement.childNodes[0]);
}
// statusElement.innerHTML = text || "Ready";
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
</script>
<script>
function YASL() {
document.getElementById('output').value = '';
var text = document.getElementById('input').value;
console.log(text);
var S = Module.ccall("YASL_newstate_bb", "number", ["string", "number"], [text, text.length]);
Module.ccall("YASL_decllib_collections", "number", ["number"], [S]);
Module.ccall("YASL_decllib_math", "number", ["number"], [S]);
Module.ccall("YASL_decllib_mt", "number", ["number"], [S]);
Module.ccall("YASL_setprintout_tostr", null, ["number"], [S]);
Module.ccall("YASL_setprinterr_tostr", null, ["number"], [S]);
var status = Module.ccall("YASL_execute", "number", ["number"], [S]);
if (status) {
Module.ccall("YASL_loadprinterr", null, ["number"], [S])
} else {
Module.ccall("YASL_loadprintout", null, ["number"], [S])
}
var output = Module.ccall("YASL_popcstr", 'string', ["number"], [S]);
document.getElementById('output').value = output;
}
function YASL_helloworld() {
document.getElementById('output').value = '';
document.getElementById('input').value = '# The first program in any language\n\necho \'hello world!\'\n';
}
function YASL_fact() {
document.getElementById('output').value = '';
document.getElementById('input').value =
'# simple program that iteratively calculates factorials\n\n' +
'fn range(a, b) {\n' +
'\tlet tmp = []\n' +
'\tfor let i = a; i < b; i += 1 {\n' +
'\t\ttmp->push(i)\n' +
'\t}\n\n' +
'\treturn tmp\n' +
'}\n\n' +
'fn fact(n) {\n' +
'\tlet tmp = 1\n' +
'\twhile n > 0 {\n' +
'\t\ttmp *= n\n' +
'\t\tn -= 1\n' +
'\t}\n\n' +
'\treturn tmp\n' +
'}\n\n\n' +
'for i <- range(0, 7) {\n' +
'\techo "fact(#{i}): #{fact(i)}"\n' +
'}\n'
}
function YASL_twosum() {
document.getElementById('output').value = '';
document.getElementById('input').value =
'/* The challenge is to find all the pairs of two \n' +
' * integers in an unsorted array \n' +
' * `ls` that sum up to a given number `t`.\n' +
' * This can be done in linear time in the length \n' +
' * of `ls`, as shown below.\n' +
' */\n\n' +
'const fn twosum(const ls, const target) {\n' +
'\tconst mapped_values = { target - val : val for val <- ls }\n' +
'\tfor val <- ls {\n' +
'\t\tif mapped_values[val] !== undef {\n' +
'\t\t\treturn "#{mapped_values[val]} + #{val} == #{target}"\n' +
'\t\t}\n' +
'\t}\n' +
'\treturn \'not found\'\n' +
'}\n\n' +
'echo twosum([1, 2, 13, 4, 5], 17)\n';
}
</script>
<script async type="text/javascript" src="yasl.js"></script>
<footer> This demo was made by compiling the YASL interpreter written in C to WASM.
It runs entirely on the client side. </footer>
</body>
</html>