-
Notifications
You must be signed in to change notification settings - Fork 0
/
thecode.js
429 lines (355 loc) · 12 KB
/
thecode.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
var market_left_arrow = 10;
var market_right_arrow = 10;
var market_plus = 10;
var market_moins = 10;
var market_point = 10;
var market_comma = 10;
var market_left_bracket = 10;
var market_right_bracket = 10;
var p_left_arrow = 10;
var p_right_arrow = 10;
var p_plus = 10;
var p_moins = 10;
var p_point = 10;
var p_comma = 10;
var p_left_bracket = 10;
var p_right_bracket = 10;
var p_currency = 1000;
var poss_r_arrow = 25;
var poss_l_arrow = 25;
var poss_plus = 150;
var poss_moins = 50;
var poss_point = 250;
var poss_comma = 25;
var poss_lbracket = 25;
var poss_rbracket = 25;
function bf_interpreter() {
var code = document.getElementById('ide').value;
var tableau = Array(30000).fill(0);
var data_pointeur = 0;
var instr_pointeur = 0;
var output = [];
while (instr_pointeur < code.length - 1) {
var current_instr = code[instr_pointeur];
if (current_instr == ">") {
data_pointeur += 1;
} else if (current_instr == "<") {
data_pointeur -= 1;
} else if (current_instr == "+") {
tableau[data_pointeur] += 1;
} else if (current_instr == "-") {
tableau[data_pointeur] -= 1;
} else if (current_instr == ".") {
var temp = String.fromCharCode(tableau[data_pointeur]);
output.push(temp);
} else if (current_instr == ",") {
var input = window.prompt("Enter one byte of data: ");
tableau[data_pointeur] = input[0];
} else if (current_instr == "[") {
// var test = window.prompt("test [");
if (tableau[data_pointeur] == 0) {
var temp_stack = ["["];
var current_instrc = instr_pointeur;
while (temp_stack.length > 0) {
current_instrc += 1;
if (code[current_instrc] == "[") {
temp_stack.push(current_instrc);
} else if (code[current_instrc] == "]") {
temp_stack.pop();
}
}
instr_pointeur = current_instrc;
}
} else if (current_instr == "]") {
// var test = window.prompt("test ]");
if (tableau[data_pointeur] != 0) {
var temp_stack = ["]"];
var current_instrc = instr_pointeur;
while (temp_stack.length > 0) {
current_instrc -= 1;
if (code[current_instrc] == "]") {
temp_stack.push(current_instrc);
} else if (code[current_instrc] == "[") {
temp_stack.pop();
}
}
instr_pointeur = current_instrc;
}
}
instr_pointeur += 1;
}
document.getElementById('terminal').innerHTML = output.join('');
return 0;
}
function updateTTType() {
updateMarket();
updatePortefeuille();
}
function updateMarket() {
market_left_arrow = newValue(market_left_arrow);
market_right_arrow = newValue(market_right_arrow);
market_plus = newValue(market_plus);
market_moins = newValue(market_moins);
market_point = newValue(market_point);
market_comma = newValue(market_comma);
market_left_bracket = newValue(market_left_bracket);
market_right_bracket = newValue(market_right_bracket);
document.getElementById("m_left_arrow").innerHTML = market_left_arrow;
document.getElementById("m_right_arrow").innerHTML = market_right_arrow;
document.getElementById("m_+").innerHTML = market_plus;
document.getElementById("m_-").innerHTML = market_moins;
document.getElementById("m_.").innerHTML = market_point;
document.getElementById("m_,").innerHTML = market_comma;
document.getElementById("m_[").innerHTML = market_left_bracket;
document.getElementById("m_]").innerHTML = market_right_bracket;
}
function updatePortefeuille() {
var code = document.getElementById('ide').value;
p_left_arrow = (code.match(/</g) || []).length;
p_right_arrow = (code.match(/>/g) || []).length;
p_plus = (code.match(/\+/g) || []).length;
p_moins = (code.match(/-/g) || []).length;
p_point = (code.match(/./g) || []).length;
p_comma = (code.match(/,/g) || []).length;
p_left_bracket = (code.match(/\[/g) || []).length;
p_right_bracket = (code.match(/\]/g) || []).length;
document.getElementById("p_right_arrow").innerHTML = p_right_arrow;
document.getElementById("p_left_arrow").innerHTML = p_left_arrow;
document.getElementById("p_+").innerHTML = p_plus;
document.getElementById("p_-").innerHTML = p_moins;
document.getElementById("p_.").innerHTML = p_point;
document.getElementById("p_,").innerHTML = p_comma;
document.getElementById("p_[").innerHTML = p_left_bracket;
document.getElementById("p_]").innerHTML = p_right_bracket;
}
function decideExecution() {
if ((poss_l_arrow - p_left_arrow) < 0 ||
(poss_r_arrow - p_right_arrow) < 0 ||
(poss_plus - p_plus) < 0 ||
(poss_moins - p_moins) < 0 ||
(poss_point - p_point) < 0 ||
(poss_comma - p_comma) < 0 ||
(poss_lbracket - p_left_bracket) < 0 ||
(poss_rbracket - p_right_bracket) < 0
) {
document.getElementById("terminal").innerHTML = "ERROR: Cannot execute this code, symbols are missing from the portfolio.";
} else {
var done_exec = bf_interpreter();
if (done_exec == 0) {
updatePossession();
}
}
}
function updatePossession() {
poss_l_arrow -= p_left_arrow;
poss_r_arrow -= p_right_arrow;
poss_plus -= p_plus;
poss_moins -= p_moins;
poss_point -= p_point;
poss_comma -= p_comma;
poss_rbracket -= p_right_bracket;
poss_lbracket -= p_left_bracket;
updatePossessionValues()
}
function updatePossessionValues() {
document.getElementById("possession_right_arrow").innerHTML = poss_r_arrow;
document.getElementById("poss_left_arrow").innerHTML = poss_l_arrow;
document.getElementById("poss_plus").innerHTML = poss_plus;
document.getElementById("poss_moins").innerHTML = poss_moins;
document.getElementById("poss_point").innerHTML = poss_point;
document.getElementById("poss_comma").innerHTML = poss_comma;
document.getElementById("poss_lbracket").innerHTML = poss_lbracket;
document.getElementById("poss_rbracket").innerHTML = poss_rbracket;
}
function newValue(nb) {
nb = parseFloat(nb);
return Math.round(Math.max(2.0 * Math.random(), (nb * (1 + randn_bm()))));
}
// Standard Normal variate using Box-Muller transform. (stackoverflow)
function randn_bm() {
var u = 0,
v = 0;
while (u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while (v === 0) v = Math.random();
return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
}
function buy_arrow_r() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a > sign: not enough money.";
} else {
poss_r_arrow += 1;
p_currency -= market_right_arrow;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_arrow_l() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a < sign: not enough money.";
} else {
poss_l_arrow += 1;
p_currency -= market_left_arrow;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_plus() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a + sign: not enough money.";
} else {
poss_plus += 1;
p_currency -= market_plus;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_moins() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a - sign: not enough money.";
} else {
poss_moins += 1;
p_currency -= market_moins;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_point() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a , sign: not enough money.";
} else {
poss_point += 1;
p_currency -= market_point;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_comma() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a , sign: not enough money.";
} else {
poss_comma += 1;
p_currency -= market_comma;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_lbracket() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a [ sign: not enough money.";
} else {
poss_lbracket += 1;
p_currency -= market_left_bracket;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
function buy_rbracket() {
if (p_currency < market_plus) {
document.getElementById("terminal").innerHTML = "Cannot buy a plus sign: not enough money.";
} else {
poss_rbracket += 1;
p_currency -= market_right_bracket;
updatePossessionValues();
updateMonnaie();
}
updateMarket();
}
//////////////////////////////////////////// SELL
function sell_arrow_r() {
if (poss_r_arrow < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a > sign : not any in posession.";
} else {
poss_r_arrow -= 1;
p_currency += market_right_arrow;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_arrow_l() {
if (poss_l_arrow < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a < sign : not any in posession.";
} else {
poss_l_arrow -= 1;
p_currency += market_left_arrow;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_plus() {
if (poss_plus < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a + sign : not any in posession.";
} else {
poss_plus -= 1;
p_currency += market_plus;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_moins() {
if (poss_moins < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a - sign : not any in posession.";
} else {
poss_moins -= 1;
p_currency += market_moins;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_point() {
if (poss_point < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a . sign : not any in posession.";
} else {
poss_point -= 1;
p_currency += market_point;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_comma() {
if (poss_comma < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a , sign : not any in posession.";
} else {
poss_comma -= 1;
p_currency += market_comma;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_lbracket() {
if (poss_lbracket < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a [ sign : not any in posession.";
} else {
poss_lbracket -= 1;
p_currency += market_left_bracket;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function sell_rbracket() {
if (poss_rbracket < 1) {
document.getElementById("terminal").innerHTML = "Cannot sell a ] sign : not any in posession.";
} else {
poss_rbracket -= 1;
p_currency += market_right_bracket;
updatePossessionValues();
updateMonnaie();
updateMarket();
}
}
function updateMonnaie() {
document.getElementById("monnaie").innerHTML = p_currency;
}