forked from andreGarvin/wordScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
363 lines (257 loc) · 8.85 KB
/
app.js
File metadata and controls
363 lines (257 loc) · 8.85 KB
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
var editor = ace.edit("editor"); // the numbering
editor.setTheme("ace/theme/monokai"); // theme
editor.getSession().setMode("ace/mode/javascript"); // language want to use
editor.setValue("# Start your work here\n"); // adding a value
editor.session.setOption("useWorker", false); //disable the corrections
var editor2 = ace.edit("editor2"); // the numbering
editor2.setTheme("ace/theme/chrome"); // theme
editor2.getSession().setMode("ace/mode/javascript"); // language want to use
editor2.setReadOnly(true); // make the editor only read
editor2.setValue("// Javascript will appear here\n");
editor2.session.setOption("useWorker", false);
var data = {
variables: [],
func: [],
wordGist: [],
code: ''
};
function includes( array, item ) {
for ( var i in array ) {
if ( array[i] === item ) {
return true;
}
}
return false
}
function run() {
try {
eval( data.code );
}
catch(e) {
console.log('*error: ' + e.message);
}
}
function print( input ) {
input = input.splice(1, input.length);
if ( input.length === 1 ) {
// return back the translated javascript code
return 'console.log('+ input.join(' ') +');';
}
/*
checks with the input is a variable already
declared in the data obj variables property
*/
for ( var i in data.variables ) {
/*
if it exsit then return back the
stringifyed sjavascript code with
the value of the concated inside of the string
*/
if ( data.variables[i].name === input ) {
return 'console.log('+ data.variables[i].value +');';
}
}
if ( input[0] === 'call' ) {
var func = callFunction( input );
// console.log( func.slice(0, func.length - 1) );
return 'console.log( ' + func.slice(0, func.length - 1) + ' );';
}
}
// arg = [x,y,z];
// "[x,y,z]"
// arg.slice(1, arg.length - 1)
// "x,y,z"
// arg.slice(1, arg.length - 1).split(',')
// ["x", "y", "z"]
function makeFunction( input ) {
data.func = [];
var func_name = input[2];
data.func.push({
func_name: input[2],
params: input[3][0] !== '[' ? null : input[3].slice(1, input[3].length - 1).split(','),
body: input.length === 7 ? input.splice(3, input.length).join(' ') : input.splice(4, input.length).join(' ')
});
// arg = [x,y,z];
// "[x,y,z]"
// arg.slice(1, arg.length - 1)
// "x,y,z"
// arg.slice(1, arg.length - 1).split(',')
// ["x", "y", "z"]
for(var i = 0; i < data.func.length; i++) {
if (data.func[i].func_name === func_name ){
var body = data.func[i].body.split(' ');
if ( body[0] !== 'return' && body[0] !== 'print' ) {
body = 'return ' + body.join(' ') + ';';
}
else if ( body[0] === 'print' ) {
body = print( body.splice(1, body.length) );
}
else {
body = body.join(' ') +';';
}
return "function " + data.func[i].func_name +"("+ data.func[i].params +") { \n \t" + body +" \n}";
}
}
}
function callFunction( input ) {
for(var i = 0; i < data.func.length; i++) {
if (data.func[i].func_name === input[1]){
for ( var i in data.variables ) {
if ( input[input.length - 1] === data.variables[i] ) {
return input[1] + '('+ data.variables[i] +');';
}
}
return input[1] + '('+ input[input.length - 1] +');';
}
}
}
function makeVariable( input ) {
/*
slice the list to remove the word 'make' and 'var'
and only have '<variable_name>' ':' '<value>'
*/
input = input.slice(2, input.length );
/*
if the array's last offset is splited
to return back array which has a length
greater then 1 then follow this statement.
ex: '<variable>:<value>'
and that it that the last frist offset in
the last item of the array is not a quotation.
*/
if ( input[input.length - 1].length > 1
&& input[input.length - 1][0] !== "'"
&& input[input.length - 1][0] !== 't'
&& input[input.length - 1][0] !== 'f' ) {
// split the last offset by the colon
input = input[input.length - 1].split(':');
/*
get the desired target values
from the return splited array
and push them to the data obj
to variable property.
*/
data.variables.push({
name: input[0],
value: input[1],
});
// lastly return the javascript code of the fromated variable
return 'var ' + input[0] + ' = ' + input[1] + ';' ;
}
/*
if the codintion was not true then:
join the list then split by colon
input before: [ '<variable_name>' ':' '<value>' ]
input after: [ '<variable_name>' '<value>' ]
*/
input = input.join(' ').split(':');
/*
get the desired variables in the
array new input array then puh them
to the data obj and tore them in
the variables property
*/
data.variables.push({
name: input[0].split(' ').join(''),
value: input[1].split(' ').join('')
});
// lastly return the javascript code of the fromated variable
return 'var ' + input[0].split(' ').join('') + ' = ' + input[1].split(' ').join('') + ';';
}
// saves the code that the user word in wordScript
function makeWordGist() {
/*
pushes the code to the
data obj to property wordGist
*/
data.wordGist.push({
name: prompt('name'),
code: editor.getValue(),
translated_code: data.code
});
}
// makes a loop
function makeLoop( input ) {
// gets the input of the frist and last offset
var start = input[2];
var end = input[input.length - 1];
// return the translated javascript code
return 'for ( var i = '+ start +'; i <= '+ end +'; i++ ) {\n console.log( i );\n}'
}
// parsers the worScrit code
function parseInput( input ) {
input = input.split(' ');
var output;
// input data: 'make variable x : 3'
// output data: ['make' 'variable' 'x' ':' '3']
switch ( input[0].toLowerCase() ) {
case 'make':
switch ( input[1].toLowerCase() ) {
case 'loop':
output = makeLoop( input );
break;
case 'var':
// target index two as the variable
output = makeVariable( input );
break;
case 'function':
output = makeFunction( input );
break;
}
break;
case 'print':
output = print( input );
break;
case 'call':
output = callFunction( input );
break;
case '#':
output = '// ' + input.splice(1, input.length).join(' ');
break;
default:
output = input;
break;
}
return output;
}
$(document).ready(function(){
$("#editor").keydown(function(key){
if(key.which === 13){
var input = editor.getValue().split('\n');
data.code = '';
for(var i in input){
data.code += parseInput( input[i] ) + '\n';
if ( data.code !== undefined ) {
editor2.setValue( data.code );
}
}
}
});
$('#run').click(function() {
run();
});
$('#save').click(function() {
makeWordGist();
});
$("#print").click(function(){
editor.insert("\nprint ");
});
$("#function").click(function(){
editor.insert("\nmake function ");
});
$("#parameter").click(function(){
editor.insert("\n[ ]");
});
$("#make").click(function(){
editor.insert("\nmake ");
});
$("#call").click(function(){
editor.insert("\ncall ");
});
$("#loop").click(function(){
editor.insert("\nmake loop");
});
$("#var").click(function(){
editor.insert("\nmake var ");
});
});