-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.js
157 lines (151 loc) · 3.88 KB
/
prelude.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
const raylib = require('raylib');
const ws = require('ws');
const fakeDelay = 0;
const rt_str = (s) => {
return String.fromCharCode(...s);
}
const Exit = class extends Error {
constructor(code) {
super(`exit ${code}`);
this.code = code;
}
}
const main = async (f) => {
try {
await f();
} catch (e) {
if (e instanceof Exit) {
return e.code;
} else {
throw e;
}
}
return 0;
};
raylib.SetTraceLogLevel(raylib.LOG_WARNING);
const rt_load = (name) => {
if (raylib[name] != null) {
if (raylib[name] instanceof Function) {
return raylib[name];
} else {
return () => raylib[name];
}
}
if (/^math-/.test(name)) {
name = name.slice(5);
if (Math[name] instanceof Function) {
return Math[name];
} else {
return () => Math[name];
}
}
if (/^on-/.test(name)) {
return (ws, f) => {
ws.on(name.slice(3), (...args) => {
return f(...args);
});
};
}
switch (name) {
case 'if': return async(c, t, f) => {
if (c) {
return await t();
} else {
return await f();
}
};
case 'wss-new': return (port) => {
return new ws.WebSocketServer({
port,
});
};
case 'stop-interval': return (n) => {
clearInterval(n);
return 0;
};
case 'start-interval': return (ms, f) => {
return setInterval(f, ms);
};
case 'ws-send': return (ws, obj) => {
if (fakeDelay === 0) {
ws.send(obj);
} else {
setTimeout(() => ws.send(obj), fakeDelay);
}
};
case 'ws-new': return (ip) => {
return new ws.WebSocket(ip, {});
};
case 'from-json': return (s) => {
return JSON.parse(String(s));
}
case 'to-json': return (o) => {
return JSON.stringify(o);
};
case 'get-time': return () => {
return (new Date()).getTime();
};
case 'time-seconds': return () => {
return () => new Date().getSeconds();
};
case 'time-minutes': return () => {
return () => new Date().getMinutes();
};
case 'time-hours': return () => {
return () => new Date().getHours();
};
case 'random': return (low, high) => {
return Math.floor(Math.random()*(high-low)+low);
}
case 'str': return (c) => {
return String(c);
};
case 'while': return async(c, v) => {
while (await c()) {
await v();
}
};
case 'cat': return (x, y) => {
return `${x}${y}`;
}
case 'get': return (o, p) => {
return o[p];
};
case 'set': return (o, p, v) => {
o[p] = v;
return v;
};
case 'object': return () => {
return {};
};
case 'not': return (x) => {
return !x;
};
case 'print': return (a) => {
console.log(a);
return 0;
};
case 'above': return (x, y) => {
return x > y;
};
case 'equal': return (x, y) => {
return y === x;
};
case 'add': return (x, y) => {
return y + x;
};
case 'sub': return (x, y) => {
return y - x;
};
case 'mul': return (x, y) => {
return y * x;
};
case 'div': return (x, y) => {
return y / x;
};
case 'mod': return (x, y) => {
return y % x;
};
}
throw new Error(`unknown: [${name}]`);
}