-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
133 lines (121 loc) · 3.27 KB
/
index.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
<script src="./build/unlisp-local.js"></script>
<script>
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function asyncLispHandler(value) {
console.log('ASK: ' + value)
const bool = {
'#t': true,
'()': false
}
const ask = value.split(' ')
const fn = ask[0]
answer = '()'
switch (fn) {
case 'led':
answer = ask[2]
break
case 'task':
await timeout(ask[2])
answer = ask[3]
break
case 'bclicked':
answer = Math.floor(Math.random() * 2) ? '#t' : '()'
break
default:
answer = '()'
break
}
return answer
}
Module.onRuntimeInitialized = async _ => {
const api = {
version: Module.cwrap('version', 'number', []),
terminate: Module.cwrap('terminate', null, []),
lisp_evaluate: Module.cwrap(
'lisp_evaluate',
'number',
['number', 'number', 'number', 'number'],
{ async: true }
)
}
console.log(`Uniot lisp version: ${api.version()}`)
const library_ptr = Module._malloc(1024)
const input_ptr = Module._malloc(1024)
const output_ptr = Module._malloc(10485760)
const handler_out_ptr = Module._malloc(64)
Module.lisp_handler = (value, wakeUp) => {
new Promise(async resolve => {
resolve(await asyncLispHandler(value))
}).then(result => {
Module.writeAsciiToMemory(result, handler_out_ptr, false)
wakeUp(handler_out_ptr)
})
}
let error = false
let script =
'(defun odds (n) (= 1 (% n 2)))\n' +
'(defun colors (n) (% n 3))\n' +
'(defun odd (n) (= 1 (% n 2)))\n' +
'(1 2 3)\n' + // The head of a list must be a function
'(color 4)\n' + // Undefined symbol: color
'(defun color (n) (% n 3))\n' +
'(+ 1 . 2)\n' // Unexpected statement. Evaluation terminated
script = "(task -1 500 '(list (print #t_pass)))"
script = "(task 10 500 '(list (print #t_pass)))"
script = "(task 10 500 '(list (led 0 (= (% #t_pass 2) 1))))"
script =
'\n' +
'(define a #t)' +
'\n' +
'(define b ())' +
'\n' +
"(task 0 100 '" +
'\n' +
' (list ' +
'\n' +
" (print '---)" +
'\n' +
' (setq b (bclicked 0))' +
'\n' +
" (print (list 'a= a))" +
'\n' +
" (print (list 'b= b))" +
'\n' +
" (print (list 'a==b (eq a b )))" +
'\n' +
' )' +
'\n' +
')'
Module.writeAsciiToMemory(script, input_ptr, false)
Module.writeAsciiToMemory(
'(defjs led (i s)) (defjs bclicked (button_id))',
library_ptr,
false
)
setTimeout(() => {
console.log('Terminating...')
api.terminate()
}, 5000)
let output_size = await api.lisp_evaluate(
8000,
library_ptr,
input_ptr,
output_ptr,
9999
)
if (output_size < 0) {
error = true
output_size *= -1
}
const result = Module.AsciiToString(output_ptr)
console.log(`Script: ${script}`)
console.log(`Result:\n${result}`)
console.log(JSON.parse(result))
error && console.error('^^^ ERROR OCCURRED!')
Module._free(input_ptr)
Module._free(output_ptr)
Module._free(handler_out_ptr)
}
</script>