-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
218 lines (197 loc) · 5.83 KB
/
parser.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
function parser_t(text, robot_name){
this.robot_name = robot_name || "robot";
this.lexer = new lexer_t(text, {robot_nick: robot_name});
//console.log(this.lexer.tokens);
this.lexeme = this.lexer.get_next();
this.program = {};
this.program.execute = false;
this.program.text = '';
}
parser_t.prototype._next = function(){
this.lexeme = this.lexer.get_next();
}
parser_t.prototype._accept = function(expected_token){
if(this.lexeme.token == expected_token){
var ret = this.lexeme.text;
this._next();
return ret;
}
return '';
}
parser_t.prototype._expect = function(expected_token){
var tmp = this._accept(expected_token);
if(tmp) return tmp;
throw "Expected token not found: {expected token: " + expected_token + "," + " current token:" + this.lexeme.token + " }"
+ " current text: " + this.lexeme.text;
}
parser_t.prototype._accept_text = function(expected_text, expected_token){
if(this.lexeme.token == expected_token && this.lexeme.text == expected_text){
this._next();
return true;
}
return false;
}
parser_t.prototype._expect_text = function(expected_text, expected_token){
if(this._accept_text(expected_text, expected_token)) return true;
throw "Unexpected token: {" + expected_text + "," + expected_token + "}";
return false;
}
//returns the "compiled" js from english
parser_t.prototype.parse = function(){
if(this.salutation())
this.program.execute = true;
while(!this.lexer.finished()){
this.command();
if(!this.lexer.finished()){
this._expect("THEN");
while(this._accept("THEN")){}
}
}
}
parser_t.prototype.salutation = function(){
this._expect("HEY");
this._expect("ROBOT_NICK");
//get rid of that stupid human politness
//who cares if its gramatical
while(
this._accept_text("can", "ID") ||
this._accept_text("could", "ID")||
this._accept_text("will", "ID")||
this._accept_text("you", "ID") ||
this._accept_text("please", "ID")){}
return true;
}
parser_t.prototype.command = function(){
this.action();
}
parser_t.prototype.action = function(){
if(this._accept_text("drive", "ACTION")){
this.drive_specs();
return
}
else if(this._accept_text("turn", "ACTION")){
this.turn_specs();
return
}
throw "Did not find expected token: {" + this.lexeme.text + "}";
}
parser_t.prototype.drive_specs = function(){
var lexeme_text;
var drive_params = {};
if(lexeme_text = this._accept("DIRECTION")){
drive_params.direction = lexeme_text;
}
drive_params.distance = this.parse_distance();
this.make_drive_command(drive_params);
}
parser_t.prototype.make_drive_command = function(drive_params){
if(drive_params.distance){
this.program.text += drive_params.direction + "(" + drive_params.distance + ");\n";
}
else{
if(drive_params.direction != 'forward')
this.program.text += drive_params.direction + "(90);\n";
this.program.text += "drive(10,10);\n";
}
}
parser_t.prototype.turn_specs = function(){
var lexeme_text;
var turn_params = {};
if(lexeme_text = this._accept("DIRECTION")){
turn_params.direction = lexeme_text;
}
turn_params.angle = this.parse_angle();
this.make_turn_command(turn_params);
}
parser_t.prototype.make_turn_command = function(turn_params){
if(turn_params.direction){
this.program.text += turn_params.direction + "(";
if(turn_params.angle){
this.program.text += turn_params.angle + ");\n";
}
else{
this.program.text += "90);\n";
}
}
else if(turn_params.angle){
this.program.text += "right("+turn_params.angle+")";
}
}
parser_t.prototype.parse_expression = function(){
return this.parse_term();
}
parser_t.prototype.parse_number = function(){
var num = this._accept("NUMBER") ;
while(tmp = this._accept("NUMBER")){
num*=10;
num+=tmp;
}
var pi_part = this._accept("PI") || this._accept("TAU") || 0;
if(pi_part && num) num *= pi_part;
else if(pi_part) num = pi_part;
return num;
}
parser_t.prototype.parse_factor = function(){
var num = this.parse_number();
while(true){
if(this._accept("DIVIDE")){
num /= this.parse_number();
}
else if(this._accept("MULTIPLY")){
num *= this.parse_number();
}
else{
return num;
}
}
}
parser_t.prototype.parse_term = function(){
var num = this.parse_factor();
while(true){
if(this._accept("ADD")){
num += this.parse_factor();
}
else if(this._accept("SUBTRACT")){
num -= this.parse_factor();
}
else{
return num;
}
}
}
parser_t.prototype.parse_distance = function(){
if(num = this.parse_expression()){
var dist_unit = this._accept("DISTANCE_UNIT");
if(dist_unit == 'meter'){
num*=100;
}
else if(dist_unit == 'inch'){
num*=2.54;
}
else if(dist_unit == 'foot'){
num*=2.54*12.0;
}
console.log(num, dist_unit);
return num;
}
else
return;
}
parser_t.prototype.parse_angle = function(){
if(num = this.parse_expression()){
var angle_unit = this._accept("ANGLE_UNIT");
if(angle_unit == 'radians'){
num = (num*180)/(Math.PI);
}
}
return num;
}
parser_t.prototype.distance_convert = function(distance_text){
//right now, its a dummy function
//eventually, it has to convert to whatever the front end uses
return distance_text;
}
parser_t.prototype.angle_convert = function(angle_text){
//dummy function, needs to convert eventually
return angle_text;
}