-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJaggScript.c
More file actions
564 lines (541 loc) · 11.8 KB
/
JaggScript.c
File metadata and controls
564 lines (541 loc) · 11.8 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "JaggScript.h"
#include "interpreter1.h"
#define OP_POSTINC 111111
#define OP_PREINC 111112
#define OP_POSTDEC 111113
#define OP_PREDEC 111114
object wrapint(int i);
object exOperator(nodeType* args);
object ex(nodeType *p)
{
object val;
val.type = VAR_NULL;
val.valInt = 0;
static int dropToFunctionCall = 0;
static int functionReturnValue = 0;
if (!p) return val;
switch(p->type)
{
case typeConVal:
{
return p->con.val;
}
case typeId:
{
if (p->id.position!=-1)
{
return varValues[p->id.position];
}
printf("Error: Non-existant variable %s\n", p->id.name);
val.valInt = 0;
return val;
}
case typeOpr:
{
switch(p->opr.oper)
{
case FUNCTION:
{
//printf("Case function\n");
//printf("Function called: %s\n", p->opr.op[0]->id.name);
//printf("Function position=%i\n", p->opr.op[0]->id.position);
//ex(funcValues[p->opr.op[0]->id.position]); // Execute function.
ex((nodeType*)varValues[p->opr.op[0]->id.position].valFunc); // Execute function :)
//TODO
/*if (dropToFunctionCall)
{
dropToFunctionCall = 0;
return functionReturnValue;
}*/
return val;
}
case DOTLENGTH:
{
object in1 = ex(p->opr.op[0]);
if (in1.type==VAR_STR)
{
val.type = VAR_INT;
val.valInt = strlen(in1.valStr);
}
else if (in1.type==VAR_STR_REP)
{
val.type = VAR_INT;
val.valInt = strlen(in1.valStr)*in1.valStrRep;
}
return val;
}
case WHILE:
{
char executed = 0;
while(ex(p->opr.op[0]).valBool)
{
ex(p->opr.op[1]);
executed = 1;
}
val.type = VAR_BOOL;
val.valBool = executed;
return val;
}
case FOR:
{
ex(p->opr.op[0]);
val.type = VAR_BOOL;
val.valBool = 0;
while(ex(p->opr.op[1]).valBool)
{
ex(p->opr.op[3]);
//object latestVal
val = ex(p->opr.op[2]);
//val.type = latestVal.type;
//val.valInt = latestVal.valInt;
}
return val;
}
case IF:
{
if (ex(p->opr.op[0]).valBool)
{
ex(p->opr.op[1]);
}
else if (p->opr.nops > 2)
{
ex(p->opr.op[2]);
}
else
{
}
return val;
}
case PRINT:
{
//to print:
object tp = ex(p->opr.op[0]);
if (tp.type==VAR_INT) printf("%i\n", tp.valInt);
else if (tp.type==VAR_STR) printf("%s\n", tp.valStr);
else if (tp.type==VAR_STR_REP)
{
if (strlen(tp.valStr)==0) return val;
int i = 0;
for (i=0;i<tp.valStrRep;i++)
{
printf("%s", tp.valStr);
}
}
else
{
printf("Error printerror: failed to print.\n");
}
return val;
}
case ';':
{
printf("Multiple statements\n");
ex(p->opr.op[0]);
val = ex(p->opr.op[1]);
return val;
}
case '=':
{
// It exists:
if (p->opr.op[0]->id.position!=-1)
{
return varValues[p->opr.op[0]->id.position] = ex(p->opr.op[1]);
}
printf("Error: #024\n");
// It doesn't exist:
// wtf this should never happen =.=
return val;
}
default:
{
return exOperator(p);
}
/*case OP_POSTINC:
{
int i=0;
while(i<varCount)
{
if (strcmp(varNames[i],p->opr.op[0]->id.name)==0)
{
val.valInt = varValues[i].valInt++;
return val;
}
i++;
}
printf("Error: Non-existant variable %s\n", p->opr.op[0]->id.name);
return val;
}
case OP_PREINC:
{
int i = 0;
while(i<varCount)
{
if (strcmp(varNames[i],p->opr.op[0]->id.name)==0)
{
val.valInt = ++varValues[i].valInt;
return val;
}
i++;
}
printf("Error: Non-existant variable %s\n", p->opr.op[0]->id.name);
return val;
}
case OP_POSTDEC:
{
int i = 0;
while(i<varCount)
{
if (strcmp(varNames[i],p->opr.op[0]->id.name)==0)
{
val.valInt = varValues[i].valInt--;
return val;
}
i++;
}
printf("Error: Non-existant variable %s\n", p->opr.op[0]->id.name);
return val;
}
case OP_PREDEC:
{
int i = 0;
while(i<varCount)
{
if (strcmp(varNames[i],p->opr.op[0]->id.name)==0)
{
val.valInt = --varValues[i].valInt;
return val;
}
i++;
}
printf("Error: Non-existant variable %s\n", p->opr.op[0]->id.name);
return val;
}
// Logic
case OP_LAND: return wrapint(ex(p->opr.op[0]).valInt && ex(p->opr.op[1]).valInt);
case OP_LOR: return wrapint(ex(p->opr.op[0]).valInt || ex(p->opr.op[1]).valInt);
case OP_LXOR: return wrapint(ex(p->opr.op[0]).valInt ^ ex(p->opr.op[1]).valInt);
default:
{
printf("Error: Woops! Error #023\n");
return val;
};
*/
}
}
}
printf("Error: Woopsydaysy error #0299876 ;(\n");
return val;
}
object exOperator(nodeType* args)
{
int argc = args->opr.nops;
object val;
val.type = VAR_NULL;
val.valInt = 0;
object o1 = ex(args->opr.op[0]);
object o2;
if (argc>=2)
{
o2 = ex(args->opr.op[1]);
}
else
{
o2.type = VAR_NULL;
o2.valInt = 0;
}
switch (args->opr.oper)
{
case '[':
{
if (o1.type==VAR_STR && o2.type==VAR_INT)
{
val.type = VAR_STR;
int strLen = strlen(o1.valStr);
if (o2.valInt<0 || o2.valInt>=strLen)
{
printf("Index out of bounds, idiot!\n");
exit(0);
}
char* newStr = malloc(2);
newStr[0] = o1.valStr[o2.valInt];
newStr[1] = 0x00;
val.valStr = newStr;
}
else if (o1.type==VAR_STR_REP && o2.type==VAR_INT)
{
val.type = VAR_STR;
int strLen = strlen(o1.valStr);
if (o2.valInt<0 || o2.valInt>=strLen*o1.valStrRep)
{
printf("Index out of bounds, idiot!\n");
exit(0);
}
char* newStr = malloc(2);
newStr[0] = o1.valStr[o2.valInt%strLen];
newStr[1] = 0x00;
val.valStr = newStr;
}
break;
}
case OP_BNOT:
if (o1.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = !o1.valInt;
}
break;
case OP_BAND:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt&o2.valInt;
}
break;
case OP_BOR:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt|o2.valInt;
}
break;
case OP_BXOR:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt^o2.valInt;
}
break;
case OP_EQ:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt==o2.valInt;
}
break;
case OP_NEQ:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt!=o2.valInt;
}
break;
case OP_GT:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt>o2.valInt;
}
break;
case OP_LT:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt<o2.valInt;
}
break;
case OP_GTE:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt>=o2.valInt;
}
break;
case OP_LTE:
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt<=o2.valInt;
}
break;
case OP_MINUS:
{
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt-o2.valInt;
}
break;
}
case OP_DIV:
{
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt/o2.valInt;
}
break;
}
case OP_PLUS:
{
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt+o2.valInt;
}
else if (o1.type==VAR_STR && o2.type==VAR_STR)
{
int len1 = strlen(o1.valStr);
int len2 = strlen(o2.valStr);
char* newStr;
if ((newStr = malloc(len1+len2+1)) == NULL)
{
printf("Error: Out of memory!\n");
}
memcpy(newStr, o1.valStr, len1);
memcpy(newStr+len1, o2.valStr, len2);
newStr[len1+len2] = 0x00;
val.type = VAR_STR;
val.valStr = newStr;
}
else if (o1.type==VAR_INT && o2.type==VAR_STR)
{
//int len1 = strlen(o1.valStr);
int len2 = strlen(o2.valStr);
char* intStr;
if ((intStr = malloc(16)) == NULL)
{
printf("Error: Out of memory!\n");
}
sprintf(intStr, "%d", o1.valInt);
int len1 = strlen(intStr);
char* newStr;
if ((newStr = malloc(len1+len2+1)) == NULL)
{
printf("Error: Out of memory!\n");
}
memcpy(newStr, intStr, len1);
memcpy(newStr+len1, o2.valStr, len2);
newStr[len1+len2] = 0x00;
val.type = VAR_STR;
val.valStr = newStr;
}
else if (o1.type==VAR_STR && o2.type==VAR_INT)
{
int len1 = strlen(o1.valStr);
int len2 = 16;
char* newStr;
if ((newStr = malloc(len1+len2+1)) == NULL)
{
printf("Error: Out of memory!\n");
}
memcpy(newStr, o1.valStr, len1);
sprintf(&newStr[len1], "%d", o2.valInt);
newStr[len1+len2] = 0x00;
val.type = VAR_STR;
val.valStr = newStr;
}
else
{
printf("Error: Cannot execute operator + on those data types\n");
}
break;
}
case OP_MUL:
{
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
val.valInt = o1.valInt*o2.valInt;
}
else if ((o1.type==VAR_STR_REP && o2.type==VAR_INT))
{
val = o1;
val.valStrRep *= o2.valInt;
}
else if ((o1.type==VAR_STR && o2.type==VAR_INT))
{
//val.type = VAR_STR;
val.type = VAR_STR_REP;
int strLen = strlen(o1.valStr);
char* newStr;
if ((newStr = malloc(strLen+1)) == NULL)
{
printf("Fatal error: Out of memory!\n");
exit(0);
}
memcpy(newStr, o1.valStr, strLen);
newStr[strLen] = 0x00;
//printf("newStr=%s\n", newStr);
val.valStr = newStr;
val.valStrRep = o2.valInt;
/*
if ((newStr = malloc(strLen*o2.valInt+1)) == NULL)
{
printf("Fatal error: Out of memory!\n");
exit(0);
}
int i;
for (i=0;i<o2.valInt;i++)
{
memcpy(&newStr[i*strLen], o1.valStr, strLen);
}
newStr[strLen*o2.valInt] = 0x00;
val.valStr = newStr;
*/
}
break;
}
case OP_POW:
{
if (o1.type==VAR_INT && o2.type==VAR_INT)
{
val.type = VAR_INT;
int i1 = o1.valInt;
int i2 = o2.valInt;
if (i1==0 && i2==0)
{
printf("Math error: 0 ** 0 is undefined\n");
exit(0);
}
else if (i1==0) val.valInt = 0;
else if (i1==1) val.valInt = 1;
else if (i2==0) val.valInt = 1;
else if (i2==1) val.valInt = i1;
else if ((i1&(i1-1))==0)
{
// i1 is a power of 2
}
else
{
printf("WIP error: ** is WIP :(\n");
}
}
break;
}
}
return val;
}
object wrapint(int i)
{
object val;
val.type = 1;
val.valInt = i;
return val;
}
// TODO
// undefine function should freeNode(funcValue[..]);
void defFunc(char* name, nodeType* func)
{
varNames[varCount] = name;
varValues[varCount].type = 4;
varValues[varCount].valFunc = func;
varCount++;
/*
if (funcCount==16)
{
printf("NO MORE THAN 16 FUNCTIONS ARE ALLOWED - BITCH\n");
}
if (func->type==typeOpr) printf("Funcval is typeOpr\n");
if (func->opr.oper==';') printf("Hoi ;)\n");
funcNames[funcCount] = name;
funcValues[funcCount] = func;
funcCount++;
*/
}
int objectToString(object o)
{
return o.valInt;
}