-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice2code.js
210 lines (165 loc) · 6.25 KB
/
voice2code.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
console.log("Loading voice2code.js ... No errors!");
var voice2code = {
states: {
GLOBAL: 0,
IN_LOOP: 1,
IN_CONDITIONAL: 2
}
};
var spoken_code = [], // List of vocal instructions used to generate the code
generated_code = [], // List of instructions generated from vocals
code_metadata = [], // Metadata available for each instruction
program_vars = []; // An array of used variables in the program
var input_state = [voice2code.states.GLOBAL]; // Last state = newest
// Language grammar -- will be refactored into its own file/format later
function process_speech_input(line) {
console.log("Processing: " + line);
var result = [];
// Command: run the code
result = line.match(/run the code/);
if (result != null && result.length > 0) {
run_code();
}
// For loop
result = line.match(/loop from (\d+) to (\d+)/i);
if (result != null && result.length > 0) {
var start = result[1],
end = result[2];
var iterator = random_variable();
g_code = "for (" + iterator + " = " + start + "; " + iterator
+ " <= " + end + "; " + iterator + "++) { {{loop_body}} }";
s_code = "loop from " + start + " to " + end;
metadata = {type: "loop", iterator: iterator};
append_instruction(g_code, s_code, metadata);
// Push new state AFTER instructions
input_state.push(voice2code.states.IN_LOOP);
}
// Assignment operator
result = line.match(/set ([\s\w]+) equal to (\d+)/i);
if (result != null && result.length > 0) {
var variable = result[1].replace(' ', '_'),
value = result[2];
metadata = {type: "assignment", variable: variable, value: value};
g_code = variable + " = " + value + ";";
s_code = "set " + result[1] + " equal to " + value;
program_vars.push(variable);
append_instruction(g_code, s_code, metadata);
}
// Addition
result = line.match(/add ([\s\w\d]+) to ([\s\w]+)/i);
if (result != null && result.length > 0) {
var value_delta = result[1].replace(' ', '_'),
accepting_variable = result[2].replace(' ', '_');
if (value_delta === "the_number") {
value_delta = "{{iterator}}";
}
metadata = {type: "addition",
variable: accepting_variable,
delta: value_delta};
g_code = accepting_variable + " += " + value_delta + ";";
s_code = "add " + value_delta + " to " + accepting_variable;
append_instruction(g_code, s_code, metadata);
}
// If statement with modulus (lol)
result = line.match(/if ([\s\w\d]+) is divisible by ([\s\w\d]+)/i);
if (result != null && result.length > 0) {
var variable = result[1].replace(' ', '_'),
operand = result[2].replace(' ', '_');
if (variable === "the_number") {
variable = "{{iterator}}";
}
if (operand === "the_number") {
operand = "{{iterator}}";
}
metadata = {type: "if and modulus", variable: variable, operand: operand};
g_code = "if (" + variable + " % " + operand + " == 0) { {{if_body}} }";
s_code = "if " + variable + " is divisible by " + operand + ", then";
append_instruction(g_code, s_code, metadata);
input_state.push(voice2code.states.IN_CONDITIONAL);
}
// Print statement
result = line.match(/print ([\s\w]+)/i);
if (result != null && result.length > 0) {
var variable = result[1].replace(' ', '_');
metadata = {type: "print"}
if (variable === "the_number") {
variable = "{{iterator}}";
} else if (program_vars.indexOf(variable) == -1) {
variable = "'" + variable + "'";
}
g_code = "console.log(" + variable + ");";
s_code = "print " + result[1];
append_instruction(g_code, s_code, metadata);
}
}
// After parsing the vocal instructions, save them to the "program"
function append_instruction(g_code, s_code, metadata) {
switch (input_state[input_state.length - 1]) {
case voice2code.states.GLOBAL:
generated_code.push(g_code);
spoken_code.push(s_code);
code_metadata.push(metadata);
break;
case voice2code.states.IN_LOOP:
// Move backwards in the instructions and find one with {{loop_body}}
var instruction_id = 0;
for (var i = generated_code.length - 1; i >= 0; i--) {
if (generated_code[i].indexOf("{{loop_body}}") > -1) {
instruction_id = i;
break;
}
}
// Replace any needed tokens
g_code = g_code.replace("{{iterator}}", code_metadata[i].iterator);
// Substitute the generated code in for the loop body
generated_code[i] = generated_code[i].replace("{{loop_body}}", g_code);
generated_code.push('');
// If we finished up a loop, lower the state depth
input_state.pop();
// Update other code stuff
spoken_code.push(s_code);
code_metadata.push(metadata);
break;
case voice2code.states.IN_CONDITIONAL:
// Move backwards in the instructions and find one with {{if_body}}
var instruction_id = 0;
for (var i = generated_code.length - 1; i >= 0; i--) {
if (generated_code[i].indexOf("{{if_body}}") > -1) {
instruction_id = i;
break;
}
}
// Substitute tokens
if (g_code.indexOf('{{iterator}}') > -1) {
// Move backwards to find the nearest loop
var loop_inst_id = 0;
for (var j = code_metadata.length - 1; j >= 0; j--) {
if (code_metadata[j].type == "loop") {
loop_inst_id = j;
break;
}
}
g_code = g_code.replace("{{iterator}}", code_metadata[loop_inst_id].iterator);
}
// Substitute the generated code in for the if body
generated_code[instruction_id] = generated_code[instruction_id].replace("{{if_body}}", g_code);
generated_code.push('');
// We finished up a conditional; lower the state depth
input_state.pop();
// Update other code stuff
spoken_code.push(s_code);
code_metadata.push(metadata);
break;
}
}
// Generates a random variable name and returns it
function random_variable() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(var i = 0; i < 12; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function run_code() {
eval(generated_code.join(''));
}