-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsonesharp.js
497 lines (400 loc) · 13.5 KB
/
jsonesharp.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//
// status line
//
var status_text = function(text) {
$('#status').text(text);
};
var halting_message = function(n, p) {
//TODO: Look into more informative halting messages
if (n==0 && p.length > 0) {
status_text('halted improperly');
}
else {
status_text('halted properly');
}
};
var eval_message = function() {
status_text('evaluating program');
};
var interrupt_message = function() {
status_text('evaluation interrupted');
};
//
// DOM register management routines
//
var resize_text_area = function(e) {
e.css('height', 'auto');
var h = (e.prop('scrollHeight') + 2).toString().concat('px');
e.css('height', h);
};
var new_register = function(n) {
var id = 'register_'.concat(n.toString());
var grp_id = 'form_group_'.concat(id);
var grp = $('<div>');
grp.attr('class', 'form-group');
grp.attr('id', grp_id);
var col2 = $('<div>');
col2.attr('class', 'col-sm-9');
var col3 = $('<div>');
col3.attr('class', 'col-sm-2');
var textarea = $('<textarea>');
textarea.attr('class', 'form-control register');
textarea.attr('rows', '1');
textarea.attr('id', id);
textarea.attr('tabindex', n + 1);
var label = $('<label>');
label.attr('class', 'control_label col-sm-1');
label.attr('for', id);
label.html('R'.concat(n.toString()));
var button = $('<button>');
button.attr('type', 'button');
button.attr('class', 'btn btn-default clear_register');
button.html('clear');
button.click(function () {
var e = $('#'.concat(id));
e.val('');
resize_text_area(e);
});
col2.append(textarea);
col3.append(button);
grp.append(label);
grp.append(col2);
grp.append(col3);
return grp;
};
var update_register_buttons = function(m) {
$('#add_register').html('add R'.concat(m.toString()));
$('#remove_register').html('remove R'.concat((m-1).toString()));
if (m==2) {
// Disable remove register button when there's only one register
$('#remove_register').prop("disabled", true);
}
else {
$('#remove_register').prop("disabled", false);
}
};
var extend_registers = function(n) {
var m = $('.register').length + 1;
while (m <= n) {
$('#rm').append(new_register(m));
m++;
}
update_register_buttons(m);
};
var add_one_register = function() {
extend_registers($('.register').length + 1);
};
var remove_last_register = function() {
var m = $('.register').length;
if (m > 1) {
var sel = '#'.concat('form_group_register_'.concat(m.toString()));
$(sel).remove();
update_register_buttons(m);
}
};
//
// functions for copying registers stored in an array (starting at
// index 1) to DOM and back.
//
var dom_regs_to_array = function() {
var dom_regs = $('.register')
var regs = [[]];
var id = '';
var i = 0;
while (i < dom_regs.length) {
id = '#register_'.concat((i+1).toString());
regs.push($(id).val().split(''));
i++;
}
return regs;
};
var array_to_dom_regs = function(regs) {
var m = $('.registers').length
if (regs.length < m) {
while (regs.length < m) {
remove_last_register();
m--;
}
}
else {
extend_registers(regs.length-1);
}
var id = '';
var i = 1;
var h = 0;
while (i < regs.length) {
id = '#register_'.concat(i.toString());
$(id).val(regs[i].join(''));
resize_text_area($(id));
i++;
}
};
//
// Program preprocessing routines
//
var parse_program = function() {
var p = $('#program').val();
p = clean(p);
var parsed = parse(p);
if (parsed[0] < p.length) {
status_text('syntax error');
eval_button_ready();
return [false, parsed[1]];
}
return [true, parsed[1]];
};
//
// Button events and states
//
var clear_program = function() {
$('#program').val('')
};
var eval_button_ready = function() {
$('#interrupt').prop('disabled', true);
$('#interrupt').off('click');
$('#evaluate').prop('disabled', false);
$('#eval_slow').prop('disabled', false);
};
var eval_button_busy = function() {
$('#interrupt').prop('disabled', false);
$('#evaluate').prop('disabled', true);
$('#eval_slow').prop('disabled', true);
};
var evaluate = function() {
eval_button_busy();
eval_message();
// Parse program
var p = vernacular_compile($('#program').val());
// if (parsed[0]) {
// var p = parsed[1];
// }
// else {
// return;
// }
// Move dom registers to array
var regs = [[]];
regs = dom_regs_to_array(regs);
// Start worker and make kill button active
var thread = new Worker('evaluate.js');
$('#interrupt').click(function() {
thread.terminate();
interrupt_message();
eval_button_ready();
});
thread.onmessage = function(e) {
halting_message(e.data[0], p);
array_to_dom_regs(e.data[1]);
eval_button_ready();
};
thread.postMessage([p, regs]);
};
var eval_slow = function() {
eval_button_busy();
eval_message();
// Parse program
var parsed = parse_program();
if (parsed[0]) {
var p = parsed[1];
}
else {
return;
}
// Move dom registers to array
var regs = [[]];
regs = dom_regs_to_array(regs);
// Start worker and make kill button active
var thread = new Worker('eval-slow.js');
$('#interrupt').click(function() {
thread.terminate();
interrupt_message();
eval_button_ready();
});
thread.onmessage = function(e) {
if (e.data[2]) {
halting_message(e.data[0], p);
eval_button_ready();
}
array_to_dom_regs(e.data[1]);
};
thread.postMessage([p, regs, 20]);
};
var load_program = function() {
var text = $(this).siblings("p.program-text").text().trim();
$("#program").val(text);
$("#editor-tab").click();
};
var add_workshop_page = function(title, program_text, active, tabpanel_id) {
// find the workshop's stacked nav
var ul = $("#" + tabpanel_id + " ul.nav-tabs");
var num_page = ul.children().size() + 1;
// find a title
if (title == null) title = "program " + num_page.toString();
var id = tabpanel_id + '-p' + num_page.toString();
// format the program text
program_text = program_text.replace(/(?:\r\n|\r|\n)/g, '\n<br>');
program_text = program_text.replace(/#1/g, '# 1');
// make a new li to add to the navigation
var new_li = $('<li>');
new_li.attr('role', 'presentation');
if (active) new_li.attr('class', 'active');
var new_tab = $('<a>');
new_tab.attr('href', '#' + id);
new_tab.attr('role', 'tab');
new_tab.attr('data-toggle', 'tab');
new_tab.text(title);
new_li.append(new_tab);
ul.append(new_li);
// add a new div with the program text and a load button
var tab_content = $("#" + tabpanel_id + " div.tab-content");
var new_panel = $('<div>');
new_panel.attr({'role': 'tabpanel', 'class': 'tab-pane', 'id': id});
if (active) new_panel.attr('class', 'tab-pane active');
var new_p = $('<p>');
new_p.attr('class', 'program-text');
new_p.html(program_text);
new_panel.append(new_p);
if (tabpanel_id == 'workshop') {
var new_button = $('<a>');
new_button.attr({'class': 'btn btn-primary wk-pload', 'type': 'button',
'role': 'button', 'data-toggle': 'tooltip', 'title': 'l'});
new_button.text('load into editor');
new_button.click(load_program);
new_button.tooltip();
new_panel.append(new_button);
}
else if (tabpanel_id == 'save-modal-panel') {
var new_button = $('<a>');
new_button.attr({'class': 'btn btn-primary save-over', 'type': 'button',
'role': 'button'});
new_button.text('overwrite this');
new_button.click(function () {
var processed_text = $('#program').val();
processed_text = processed_text.replace(/#1/g, '# 1');
sudo_set_program(title, processed_text);
clear_std_tabs();
load_library_from_cookie();
//$(this).siblings('p.program-text').html(processed_text);
$('div.modal').modal('hide');
});
new_panel.append(new_button);
}
else if (tabpanel_id == 'open-modal-panel') {
var new_button = $('<a>');
new_button.attr({'class': 'btn btn-primary open', 'type': 'button',
'role': 'button'});
new_button.text('open');
new_button.click(function () {
$('#program').val($(this).siblings('p.program-text').text());
$('div.modal').modal('hide');
});
new_panel.append(new_button);
}
tab_content.append(new_panel);
return title;
};
var replenish_std_lib = function() {
sudo_set_program("clear1", "; clear 1\n1##### 111### 11#### 111####");
sudo_set_program("pop1", "; pop 1\n1##### 1### 1###");
sudo_set_program("copy123", "; copy 2 <- 1 using 3\n1##### 11111111### 1111### 11## 111## 11111#### 11# 111# 11111111#### 111##### 111111### 111### 1## 1111#### 1# 111111####");
sudo_set_program("write", "; write\n1##### 111111111### 11111### 11# 11## 11## 111111#### 11# 11##\n111111111#### 11### ## 111111### 111### 1## 1111#### 1# 111111####");
sudo_set_program("writelastclear", "; write last clear\n1## 1## 1## 1## 1## 1# 1# 1# 1## 1## 1## 1# 1# 1## 1## 1## 1## 1# 1# 1# 1## 1## 1## 1##");
sudo_set_program("metaclear", "; metaclear\nimport copy123\nimport clear1\nlabel start\n11##### ; cases 2\ngoto end ; blank => goto end\n1### ; 1 => next instruction\n1# ; # => write a 1\ngoto start\nlabel end\nimport writelastclear");
}
var save_as_new = function() {
var title = null;
var program_text = $('#program').val();
title = add_workshop_page(title, program_text, false, "workshop");
add_workshop_page(title, program_text, false, "save-modal-panel");
add_workshop_page(title, program_text, false, "open-modal-panel");
sudo_set_program(title, program_text);
};
var clear_tabs = function(tabpanel_id) {
$("#" + tabpanel_id + " ul.nav-tabs").empty();
$("#" + tabpanel_id + " div.tab-content").empty();
}
var clear_std_tabs = function() {
clear_tabs('workshop');
clear_tabs('save-modal-panel');
clear_tabs('open-modal-panel');
}
var load_library_from_cookie = function () {
replenish_std_lib();
var program_titles = get_titles();
var first = true;
for (var i = 0; i < program_titles.length; i++) {
var program_vernacular = get_vernacular(program_titles[i]);
add_workshop_page(program_titles[i], program_vernacular, first, "workshop");
add_workshop_page(program_titles[i], program_vernacular, first, "save-modal-panel");
add_workshop_page(program_titles[i], program_vernacular, first, "open-modal-panel");
first = false;
}
};
//
// main
//
$(document).ready(function() {
// load cookie data
load_library_from_cookie();
// prep editor tab
extend_registers(1);
eval_button_ready();
// click handlers
$('#remove_register').click(remove_last_register);
$('#add_register').click(add_one_register);
$('#clear_program').click(clear_program);
$('#evaluate').click(evaluate);
$('#eval_slow').click(eval_slow);
$('a.wk-pload').click(load_program);
$('#save_as_new').click(function () {
$('#save-modal').modal('show');
});
$('#open').click(function () {
$('#open-modal').modal('show');
});
$('#save_new_btn').click( function() {
sudo_set_program($('#save_new_title').val(), $('#program').val());
clear_std_tabs();
load_library_from_cookie();
$('div.modal').modal('hide');
});
// keyboard inputs
$('body').keypress(function(e) {
if (e.keyCode == 27) {
$('div.modal').modal('hide');
$('*').blur();
}
// intercept other keyboard inputs only if not in a text area
if (!$('input,textarea').is(':focus')) {
if (e.which == 63) { // '?'
$("#help-modal").modal('show');
}
// these shortcuts are available for editor tab only
if ($("li[class=active] > #editor-tab").length) {
if (e.which == 114) { // 'r'
$('#evaluate').click();
}
if (e.which == 115) { // 's'
$('#save-modal').modal('show');
}
if (e.which == 111) {
$('#open-modal').modal('show');
}
if (e.which == 99) { // 'c'
$('#clear_program').click();
}
if (e.which == 119) { // 'w'
$("#workshop-tab").click();
}
}
// these shortcuts are available for workshop tab only
if ($("li[class=active] > #workshop-tab").length) {
if (e.which == 101) { // 'e'
$("#editor-tab").click();
}
if (e.which == 108) { // 'l'
$('#workshop div[class="tab-pane active"] a').click();
}
}
}
});
});