-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecursionTest.html
More file actions
79 lines (79 loc) · 3.01 KB
/
recursionTest.html
File metadata and controls
79 lines (79 loc) · 3.01 KB
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
<!DOCTYPE html>
<!-- See Arithmetic Operators Test for explanations.-->
<html lang="en">
<head>
<title>AEC-to-WebAssembly compiler - Recursion Test</title>
<style type="text/css">
body {
font-family: sans-serif;
}
#format_as_code {
font-family: "Lucida Console", monospace;
}
h1 {
text-align: center;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Recursion Test</h1>
After I've written about 3'700 lines of code of my AEC-to-WebAssembly
<a href="https://github.com/FlatAssembler/AECforWebAssembly">compiler</a>, I
think I'm finally able to implement some simple recursive algorithms, such
as Fibonacci Sequence, a function calculating the sum of the first few
natural numbers and a function calculating the factorial, in AEC and invoke
them from JavaScript.
<br />
<b>This only works in very modern browsers.</b> I'm not too interested in
targeting archaic browsers here. This only works in Firefox 62 and newer or
Chrome 69 and newer. After a few years (if not months), all JavaScript
environments will become as capable as they are, rather than like Internet
Explorer.<br /><br />
<form>
<label for="enterN">Enter 'n':</label><br />
<input type="text" id="enterN" name="enterN" /><br />
</form>
<button onclick="invokeAECfunctions()">Invoke AEC functions!</button><br />
<span id="format_as_code">
sumFirst(n)=<span id="sumFirst"></span><br />
fib(n)=<span id="fib"></span><br />
fact(n)=<span id="fact"></span><br />
stack_pointer=<span id="stack_pointer"></span> //Should always be 0.<br /> </span
><br />
You can see the source code of the AEC program
<a
href="https://raw.githubusercontent.com/FlatAssembler/AECforWebAssembly/master/recursionTest/recursionTest.aec"
>here</a
>.
<script type="text/javascript">
const stack_pointer = new WebAssembly.Global(
{ value: "i32", mutable: true },
0
);
let memory = new WebAssembly.Memory({ initial: 1 });
let importObject = {
JavaScript: { stack_pointer: stack_pointer, memory: memory },
};
let sumFirst, fib, fact;
fetch("recursionTest.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject))
.then((results) => {
exports = results.instance.exports;
sumFirst = exports.sumFirst;
fib = exports.fib;
fact = exports.fact;
});
function invokeAECfunctions() {
let n = parseInt(document.getElementById("enterN").value);
document.getElementById("sumFirst").innerText = sumFirst(n);
document.getElementById("fib").innerText = fib(n);
document.getElementById("fact").innerText = fact(n);
document.getElementById(
"stack_pointer"
).innerText = stack_pointer.valueOf();
}
</script>
</body>
</html>