-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_reader.js
109 lines (103 loc) · 2.59 KB
/
command_reader.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
'use strict';
var commands = {
'calc': calc_op(calculator),
'calculator': calc_op(calculator),
'base': calc_op(base_convert),
'prec': calc_op(get_precedence),
'precedence': calc_op(get_precedence),
'assoc': calc_op(get_assoc),
'level': {
'f': function (x) {
if (x === undefined) {
el('other').innerHTML = Question.get_level();
} else {
Question.set_level(parseInt(x, 10));
}
},
'arg': null
},
'world': {
'f': function (x) {
if (x === undefined) {
el('other').innerHTML = Question.get_world();
} else {
Question.set_world(parseInt(x, 10));
}
},
'arg': null
},
'limit': {
'f': function () {
el('other').innerHTML = Question.get_limit();
},
'arg': false
},
'skip': {
'f': function (x) {
Question.skip();
},
'arg': false
},
'commands': {
'f': function () {
el('other').innerHTML = Object.keys(commands).sort().join(', ');
},
'arg': false
},
'operators': {
'f': function (input) {
if (input === undefined) {
el('other').innerHTML = ops.map(function (x) {
return x.name;
}).join(', ');
} else {
Question.use_operators(input);
}
},
'arg': null
}
}
var show_invalid = function (x) {
el('other').innerHTML = 'Invalid command.';
}
var clear_boxes = function () {
el('command_input').value = '';
el('other').innerHTML = '';
}
var process_input = function (input) {
clear_boxes();
if (/^-?\d+$/.exec(input)) {
Question.answer(parseInt(input, 10));
} else {
process_typical_input(input);
}
}
var process_typical_input = function (input) {
var parts = input.split(' ');
if (!(parts[0] in commands)) {
show_invalid();
return;
}
var command = commands[parts[0]];
if (parts.length === 1) {
if (command.arg === true) {
show_invalid();
return;
}
command.f();
} else {
if (command.arg === false) {
show_invalid();
return;
}
command.f(parts.slice(1).join(' '));
}
}
window.onload = function () {
Question.init();
el('command_input').addEventListener('keyup', function (e) {
if (e.keyCode === 13) {
process_input(el('command_input').value);
}
});
}