-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.js
66 lines (60 loc) · 2.64 KB
/
Lexer.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
module.exports = class Lexer {
constructor() {}
static validCommands() {
return {
// header
'WHITE': { fn: 'begin', args: null},
'WHITE:WHITE': { fn: 'begin', args: null},
// robot commands
'DGREEN:DGREEN' : { fn: 'front', args: null},
'RED:RED' : { fn: 'right', args: null},
'ORANGE:ORANGE' : { fn: 'left', args: null},
'BLUE:BLUE' : { fn: 'beep', args: null},
// Hack
'RED:ORANGE' : { fn: 'right', args: null},
'ORANGE:ORANGE' : { fn: 'right', args: null},
// operate with last mentioned register
'PURPLE:PURPLE' : { fn: 'readFromSensor', args: null},
'PURPLE:BEIGE' : { fn: 'incrementRegister', args: null},
'PURPLE:ORANGE' : { fn: 'decrementRegister', args: null},
'PURPLE:BLUE' : { fn: 'jumpFalse', args: null},
'PURPLE:RED' : { fn: 'jumpTrue', args: null},
// select a register
'BROWN:PURPLE' : {fn: 'selectRegister', args: 'PURPLE'} ,
'BROWN:BEIGE' : {fn: 'selectRegister', args: 'BEIGE'} ,
'BROWN:DGREEN' : {fn: 'selectRegister', args: 'DGREEN'} ,
'BROWN:LGREEN' : {fn: 'selectRegister', args: 'LGREEN'} ,
'BROWN:ORANGE' : {fn: 'selectRegister', args: 'ORANGE'} ,
'BROWN:BLUE' : {fn: 'selectRegister', args: 'BLUE'} ,
'BROWN:RED' : {fn: 'selectRegister', args: 'RED'} ,
// save bookmark
'ORANGE:PURPLE' : {fn: 'saveBookmark', args: 'PURPLE'},
'ORANGE:BEIGE' : {fn: 'saveBookmark', args: 'BEIGE'},
'ORANGE:DGREEN' : {fn: 'saveBookmark', args: 'DGREEN'},
'ORANGE:BROWN' : {fn: 'saveBookmark', args: 'BROWN'},
'ORANGE:BLUE' : {fn: 'saveBookmark', args: 'BLUE'},
'ORANGE:RED' : {fn: 'saveBookmark', args: 'RED'},
// goto saved bookmark
'LGREEN:PURPLE' : {fn: 'goToBookmark', args: 'PURPLE'},
'LGREEN:BEIGE' : {fn: 'goToBookmark', args: 'BEIGE'},
'LGREEN:DGREEN' : {fn: 'goToBookmark', args: 'DGREEN'},
'LGREEN:LGREEN' : {fn: 'goToBookmark', args: 'LGREEN'},
'LGREEN:BROWN' : {fn: 'goToBookmark', args: 'BROWN'},
'LGREEN:BLUE' : {fn: 'goToBookmark', args: 'BLUE'},
'LGREEN:RED' : {fn: 'goToBookmark', args: 'RED'},
}
}
static validate(command) {
return command.join(":") in Lexer.validCommands()
}
static findOperation(command) {
return Lexer.validCommands()[command.join(":")]
}
static name(command) {
let op = Lexer.findOperation(command)
if (!op) {
throw new Error(`Command not found: ${command[0]}:${command[1]}`)
}
return op.fn + (op.args ? '→' + op.args : '')
}
}