-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.js
417 lines (417 loc) · 11.9 KB
/
interpreter.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Vegetables [potato, carrot, pumpkin]
const fs = require('fs');
var fil=process.argv[2];
var onion = []
onion[0] = 0
onion[1] = 0
var int=0;var $=false;try{
fs.readFileSync(fil).toString()
var currstring = ''
}catch(Exception){
console.log(`[*~*] File '${fil}' not found`)
process.exit(1)
}var plant = fs.readFileSync(fil).toString().split('');
var Pumpkins = {}
var Carver = [0]
var Candle = 0
// parsing the code
plant.forEach(lambda => {
// check if the current character is the first character of a string
if(lambda==='"'||lambda==='\''&&!$){
// goto string mode
$=true
if (lambda === "'") {currstring="'"}
else {currstring='"'}
plant[int] = ` ${lambda} `
}// check if the current character is the last character of a string
else if(lambda===currstring&&$&&plant[int-1]!=='\\') {
// goto non-string mode
$=false
currstring = ''
plant[int] = ` ${lambda} `
};if (lambda==='\r'||lambda==='\n') {plant[int] = ' '}
if (lambda==='#' && plant[int+1]==='/' && !$) {
// comments
plant[int] = ' #/ '
plant[int+1] = ''
}
// check the current character is an arithmetic operator
if (lambda==='+'&&!$) {plant[int] = ' + '}
if (lambda==='-'&&!$) {plant[int] = ' - '}
if (lambda==='/'&&!$) {plant[int] = ' / '}
if (lambda==='%'&&!$) {plant[int] = ' % '}
if (lambda==='*'&&!$) {plant[int] = ' * '}
if (lambda==='^'&&!$) {plant[int] = ' ^ '}
if (lambda==='$'&&!$) {plant[int] = ' $ '}
if (lambda==='@'&&!$) {plant[int] = ' @ '}
if (lambda==='?'&&!$) {plant[int] = ' ? '}
if (lambda===':'&&!$) {plant[int] = ' : '}
if (lambda===':'&&plant[int+1]===':'&&!$) {
plant[int] = ' :: '
plant[int+1] = ''
}// check if its an atcall call comand
if (lambda=='@'&&plant[int+1]=='c'&&plant[int+2]=='a'&&
plant[int+3]=='l'&&plant[int+4]=='l'&&!$) {
plant[int] = ' @call '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
plant[int+4] = ''
}if ( // check if its an atwith command
lambda=='@'&&plant[int+1]=='w'&&plant[int+2]=='i'&&
plant[int+3]=='t'&&plant[int+4]=='h'&&!$
) {
plant[int] = ' @with '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
plant[int+4] = ''
}if (// check if its an atcall define command
lambda=='@'&&plant[int+1]=='d'&&
plant[int+2]=='e'&&plant[int+3]=='f'&&!$) {
plant[int] = ' @def '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
}if (lambda=='^'&&plant[int+1]=='a'&&plant[int+2]=='d'&&
plant[int+3]=='d'&&!$) {
plant[int] = ' ^add '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
plant[int+4] = ''
}if (lambda=='^'&&plant[int+1]=='b'&&plant[int+2]=='i'&&
plant[int+3]=='n'&&!$) {
plant[int] = ' ^bin '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
plant[int+4] = ''
}if (lambda=='^'&&plant[int+1]=='h'&&plant[int+2]=='e'&&
plant[int+3]=='x'&&!$) {
plant[int] = ' ^hex '
plant[int+1] = ''
plant[int+2] = ''
plant[int+3] = ''
plant[int+4] = ''}
// condition and variable things
if (lambda==='='&&!$) {
if (plant[int+1]=='=') {
plant[int] = ''
plant[int+1] = ' == '
}else if (plant[int+1]=='!') { // variable re-assignment
plant[int] = ''
plant[int+1] = ' =! '
}else{plant[int] = ' = '}
}if ((lambda==='('||lambda===')')&&!$) {
if(lambda==='('){onion[0]++}
else {onion[1]++}
plant[int] = ' '+plant[int]+' '
}if (lambda=='/'&&plant[int+1]=='?'&&!$) { // atwith string
plant[int+1] = ''
plant[int] = ' ?/ '
}if (lambda=='-'&&plant[int+1]=='>'&&!$) { // atwith string
plant[int+1] = ''
plant[int] = ' -> '
}int++;
});// set foo to the new plant
var foo='';
plant.forEach(lambda => {
foo += lambda
});if (onion[0]!==onion[1]) {
foo = foo.replace(/(\(|\))/g,' ')
}// set plant to the new foo
plant = foo.split('');foo=''
int = 0
$ = false
// remove whitespace
plant.forEach(lambda => {
if (plant[int-1]===' '&&plant[int]===' '&&!$) {
plant[int-1] = ''
}if(lambda==='"'||lambda==='\''&&!$){
$=true
if (lambda === "'") {currstring="'"}
else {currstring='"'}
}else if(lambda===currstring&&$&&plant[int-1]!=='\\') {
currstring=''
$=false
};int++;
});plant.forEach(lambda => {foo += lambda});
plant = foo.split(' ')
// variable variables
var carrot = {
root:{},
core:{},
};var carrots = 0
var potato = 0
$ = false
// start interpreting
for (let tomato = 0; tomato < plant.length; tomato++) {
const slot = plant[tomato];
if (slot === '#/') {
// comments
let value = tomato+1
while(plant[tomato] != '#/'){
if (plant[tomato] === undefined){
tomato = value
}
}
}
if (slot === '$') {
let endvalue = Math.floor((Math.random()+1)*(Math.random()+1)*(Math.random()+1))
// assigning variables
if (plant[tomato+2] === '=') {
// strings
if (plant[tomato+3] === '\'') {
let str = ''
let start = tomato
let value = tomato+4
tomato += 4
while (plant[tomato] != '\''){
if (plant[tomato-1] === '\'') {
str += plant[tomato]
}else if (plant[tomato+1] === '\'') {
str += ' '+plant[tomato]
}else if (plant[tomato+1] !== '\''&&plant[tomato-1] !== '\'') {
str += ' '+plant[tomato]+' '
}
if (plant[tomato] === undefined) {
tomato = value
break
}
tomato += 1
}
tomato = start
endvalue = str
}else if (plant[tomato+3] === '?'){// arithemtic
let one
let two
let oporatore = '+'
tomato += 3
// check if its a variable
if (plant[tomato+1]=='$') {// set one to the variable
oporatore = plant[tomato+3]
tomato++;tomato++ // 1 -> 3
one = carrot.core[carrot.root[plant[tomato]]]
}else{// set one to the value
tomato++; // 1 -> 2
oporatore = plant[tomato+1]
one = plant[tomato]
}// check if its a variable
if (plant[tomato+2]=='$') {// set two to the variable
two = carrot.core[carrot.root[plant[tomato+3]]]
} else {// set two to the value
two = plant[tomato+2]
}if (oporatore == '*') { // check if oporatore is valid
endvalue = parseFloat(one)*parseFloat(two)
}else if (oporatore == '+') { // check if its +
endvalue = parseFloat(one)+parseFloat(two)
}else if (oporatore == '-') { // check if its -
endvalue = parseFloat(one)-parseFloat(two)
}else if (oporatore == '/') { // check if its /
endvalue = parseFloat(one)/parseFloat(two)
}else if (oporatore == '%') { // check if its %
endvalue = parseFloat(one)%parseFloat(two)
}else if (oporatore == '^') { // check if its ^
endvalue = parseFloat(one)^parseFloat(two)
}else { // check if its not a valid operator
endvalue = `nil`// if not set endvalue to `nil`
}
}else{
endvalue = plant[tomato+3]
}
if (carrot.core[carrot.root[plant[tomato+1]]] === undefined) {
carrot.core[carrots] = endvalue
carrot.root[plant[tomato+1]] = carrots
carrots++
}
}
// reasigning variables
if (plant[tomato+2] === '=!') {
try {
if (plant[tomato+3]==='$') {
if (carrot.core[carrot.root[plant[tomato+4]]] == undefined) {
plant[tomato+2] = '='
tomato--
}
}else{
carrot.core[potato] = plant[tomato+3]
}
} catch (Exception) {
carrot.core[carrots] = plant[tomato+3]
carrot.root[plant[tomato+1]] = carrots
carrots++
}
}
}
if (plant[tomato]==='puts') {
if (plant[tomato+1]==='$') {
try {
console.log(carrot.core[carrot.root[plant[tomato+2]]])
} catch (Exception) {
console.log(`nil`)
}
}else if (plant[tomato+1]=='?') {
tomato++ // 0 -> 1
let one
let two
let oporatore
if (plant[tomato+1]=='$') {
oporatore = plant[tomato+3]
tomato++;tomato++ // 1 -> 3
one = carrot.core[carrot.root[plant[tomato]]]
}else{
tomato++; // 1 -> 2
oporatore = plant[tomato+1]
one = plant[tomato]
}if (plant[tomato+2]=='$') {
two = carrot.core[carrot.root[plant[tomato+3]]]
} else {
two = plant[tomato+2]
}
if (oporatore == '*') {
console.log(parseFloat(one)*parseFloat(two))
}else if (oporatore == '+') {
console.log(parseFloat(one)+parseFloat(two))
}else if (oporatore == '-') {
console.log(parseFloat(one)-parseFloat(two))
}else if (oporatore == '/') {
console.log(parseFloat(one)/parseFloat(two))
}else if (oporatore == '%') {
console.log(parseFloat(one)%parseFloat(two))
}else if (oporatore == '^') {
console.log(parseFloat(one)^parseFloat(two))
}else {
console.log(`nil`)
}
}
}if (slot=='@def') {
// define atcall
Pumpkins[plant[tomato+1]] = tomato+1
let value = tomato+2
while (true) {
if (plant[tomato]=='@') {
break
}if (plant[tomato]==undefined) {
tomato = value
break
}tomato++
}
}if (slot == '@call') {
// goto atcall mode
if (Pumpkins[plant[tomato+1]] !== undefined){
Candle++
Carver[Candle] = tomato
tomato = Pumpkins[plant[tomato+1]]
}
}if (slot === '@') {
// set the value of tomato to the value of the candle
// and subtract the value of the candle by one
tomato = Carver[Candle]
Candle--
}if (slot == '@with'){
// atwith time
if (plant[tomato+1] == 'file') {
let potato = 0
if (plant[tomato+3] == 'read') {
let dir = ''
let value = tomato+5
tomato += 5
// get directory
while(plant[tomato] != '?/'){
if (plant[tomato] === '\'') {
tomato++
while(plant[tomato] !== '\''){
if (plant[tomato-1] === '\'') {
dir += plant[tomato]
}else if (plant[tomato+1] === '\'') {
dir += ' '+plant[tomato]
}else if (plant[tomato+1] !== '\''&&plant[tomato-1] !== '\'') {
dir += ' '+plant[tomato]+' '
}
if (plant[tomato] === undefined) {
tomato = value
break
}
tomato += 1
}
}
if (plant[tomato] === undefined) {
tomato = value
potato = value
break
}
potato = tomato+1
tomato++
}tomato = potato
// read file
if (plant[tomato+1] === '->') {
tomato += 1
if (plant[tomato+1] === '$') {
tomato++
// create variabile if it does not exit
if (carrot.core[carrot.root[plant[tomato+1]]] === 'undefined') {
carrot.core[carrots] = fs.readFileSync(dir).toString()
carrot.root[plant[tomato+1]] = carrots
carrots++
}else {
carrot.core[carrot.root[plant[tomato+1]]] = carrot.core[carrots] = fs.readFileSync(dir).toString()
}
}
}
}
if (plant[tomato+3] == 'write') {
// put variabiles
let dir = ''
let value = tomato+5
tomato += 5
// get directory
while(plant[tomato] != '?/'){
if (plant[tomato] === '\'') {
tomato++
while(plant[tomato] !== '\''){
if (plant[tomato-1] === '\'') {
dir += plant[tomato]
}else if (plant[tomato+1] === '\'') {
dir += ' '+plant[tomato]
}else if (plant[tomato+1] !== '\''&&plant[tomato-1] !== '\'') {
dir += ' '+plant[tomato]+' '
}
if (plant[tomato] === undefined) {
tomato = value
break
}
tomato += 1
}
}
if (plant[tomato] === undefined) {
tomato = value
potato = value
break
}
potato = tomato+1
tomato++
}tomato = potato
// write to the file
if (plant[tomato+1] === '$') {
tomato++
if (carrot.core[carrot.root[plant[tomato+1]]] === undefined) {
1/0 // simple way to get to the catch block just divide by zero
}else {
// set the default value of the variable
let contence = 'generic text'
contence = carrot.core[carrot.root[plant[tomato+1]]].toString()
if (contence == undefined) {
// set value to 'generic text' if the variable is undefined
contence = 'generic text'
}
// write to file
fs.writeFileSync(dir, contence)
}
}// TODO: ‘:’ tag for epic variables
}// TODO: updates without 100 lines of code, just less lines of code
}// TODO: make into npm package for better code runing ablity and no more download from github
}// TODO: user input
}// console.log(carrot);console.log(Pumpkins);console.log(carrots);console.log(foo) // for debugging
// NOTE: im adding coments to every thing yay
/* do not asign value non existant variable with '=!' */