-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
1768 lines (1509 loc) · 53.1 KB
/
interpreter.c
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// interpretec.c
/******************************************************************************
* Implementace interpretu imperativniho jazyka IFJ12 *
* Autori: *
* Tomas Nestrojil (xnestr03) *
* *
* Interpreter of instruction list *
******************************************************************************
*/
#include <math.h>
#include "interpreter.h"
#include "errnum.h"
#include "global.h"
#include "libstring.h"
#include "runtime_stack.h"
#include "variable.h"
ecode interpreterInit(tIList *instrList);
ecode insertNewVariable(int offset);
String *convertVariableToString(tVariable *srcVar);
String *stringPower(String *base, double power);
// Funkce pro jednotlive instrukce
ecode instructionGoto(tIList *instrList, tInstruction *instruction);
ecode instructionIfGoto(tIList *instrList, tInstruction *instruction);
ecode instructionCall(tIList *instrList, tInstruction *instruction);
ecode instructionRet(tIList *instrList, tInstruction *instruction);
ecode instructionAdd(tInstruction *instruction);
ecode instructionSubtract(tInstruction *instruction);
ecode instructionMultiply(tInstruction *instruction);
ecode instructionDivide(tInstruction *instruction);
ecode instructionPower(tInstruction *instruction);
ecode operationRelational(tInstruction *instruction);
ecode instructionSubstring(tInstruction *instruction);
ecode instructionPush(tInstruction *instruction);
ecode instructionPushStack(tInstruction *instruction);
ecode instructionPop(tInstruction *instruction);
ecode instructionInput(tInstruction *instruction);
ecode instructionNumeric(tInstruction *instruction);
ecode instructionPrint(tInstruction *instruction);
ecode instructionTypeOf(tInstruction *instruction);
ecode instructionLen(tInstruction *instruction);
ecode instructionFind(tInstruction *instruction);
ecode instructionSort(tInstruction *instruction);
ecode instructionMov(tInstruction *instruction);
ecode instructionMovStack(tInstruction *instruction);
ecode instructionRemoveStack(tInstruction *instruction);
tRuntimeStack *runtimeStack;
/**
* Nastaveni aktivni instrukce na prvni. Inicializace runtime stack a
* interpretace instrukci dokud nenarazi na instrukci INSTR_HALT
* @param *instrList Ukazatel na seznam instrukci pro interpretaci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode interpreter(tIList *instrList)
{
ecode error;
runtimeStack = tRuntimeStackInit();
tInstruction *currentInstruction;
// ------------------ Nastavi prvni instrukci na aktivni -------------------------
error = interpreterInit(instrList);
if (error != ERR_OK) return error;
currentInstruction = tIListGetActiveInstruction(instrList);
if (currentInstruction == NULL) return ERR_LIST;
// ------------------ Nastavi prvni instrukci na aktivni -------------------------
while (currentInstruction->instruction != INSTR_HALT)
{
switch (currentInstruction->instruction)
{
case INSTR_GOTO:
error = instructionGoto(instrList, currentInstruction);
break;
case INSTR_IFGOTO:
error = instructionIfGoto(instrList, currentInstruction);
break;
case INSTR_CALL:
error = instructionCall(instrList, currentInstruction);
break;
case INSTR_RET:
error = instructionRet(instrList, currentInstruction);
break;
case INSTR_HALT:
case INSTR_LABEL:
// Tyto instrukce se neinterpretuji, maji funkci navseti
break;
case INSTR_ADD:
error = instructionAdd(currentInstruction);
break;
case INSTR_SUBTRACT:
error = instructionSubtract(currentInstruction);
break;
case INSTR_MULTIPLY:
error = instructionMultiply(currentInstruction);
break;
case INSTR_DIVIDE:
error = instructionDivide(currentInstruction);
break;
case INSTR_POWER:
error = instructionPower(currentInstruction);
break;
case INSTR_LESSER:
case INSTR_GREATER:
case INSTR_EQUAL:
case INSTR_LESSER_OR_EQUAL:
case INSTR_GREATER_OR_EQUAL:
case INSTR_NOT_EQUAL:
error = operationRelational(currentInstruction);
break;
case INSTR_SUBSTRING:
error = instructionSubstring(currentInstruction);
break;
case INSTR_PUSH:
error = instructionPush(currentInstruction);
break;
case INSTR_PUSH_STACK:
error = instructionPushStack(currentInstruction);
break;
case INSTR_POP:
error = instructionPop(currentInstruction);
break;
case INSTR_INPUT:
error = instructionInput(currentInstruction);
break;
case INSTR_NUMERIC:
error = instructionNumeric(currentInstruction);
break;
case INSTR_PRINT:
error = instructionPrint(currentInstruction);
break;
case INSTR_TYPEOF:
error = instructionTypeOf(currentInstruction);
break;
case INSTR_LEN:
error = instructionLen(currentInstruction);
break;
case INSTR_FIND:
error = instructionFind(currentInstruction);
break;
case INSTR_SORT:
error = instructionSort(currentInstruction);
break;
case INSTR_MOV:
error = instructionMov(currentInstruction);
break;
case INSTR_MOV_STACK:
error = instructionMovStack(currentInstruction);
break;
case INSTR_REMOVE_STACK:
error = instructionRemoveStack(currentInstruction);
break;
}
if(error != ERR_OK)
{
tRuntimeStackDispose(runtimeStack);
return error;
}
if (currentInstruction->instruction != INSTR_RET && instrList->active->instruction->instruction != INSTR_HALT)
{
error = tIListNext(instrList);
if(error != ERR_OK)
return error;
}
currentInstruction = tIListGetActiveInstruction(instrList);
if (currentInstruction == NULL)
{
tRuntimeStackDispose(runtimeStack);
return ERR_LIST;
}
}
tRuntimeStackDispose(runtimeStack);
runtimeStack = NULL;
return ERR_OK;
}
/**
* Zinicializuje interpret pro interpretaci, nastveni prvni instrukce
* hlavni funkce a nastaveni zasobniku
* @param *instrList Ukazatel na seznam instrukci pro interpretaci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode interpreterInit(tIList *instrList)
{
ecode error;
tTableItem *functionRecord;
tIListItem *firstInstruction;
String *mainFunctionName;
// ------------------ Ziskani prvni instrukce ------------------------------------
mainFunctionName = charToString(MAIN_FUNCTION_NAME);
if (mainFunctionName == NULL)
return ERR_MEMORY;
functionRecord = searchItem(functionTable, mainFunctionName);
if (functionRecord == NULL)
{
deallocString(mainFunctionName);
return ERR_INTERNAL;
}
deallocString(mainFunctionName);
firstInstruction = ((tFunctionData *) functionRecord->data)->firstInstruction;
// ------------------ Nastavi prvni instrukci na aktivni -------------------------
error = tIListGoto(instrList, firstInstruction);
if (error != ERR_OK)
return error;
error = tRuntimeStackMoveSP(runtimeStack,((tFunctionData *) functionRecord->data)->varTabHead->itemCount + 1);
if (error)
return error;
return ERR_OK;
}
/**
* Vytvori novou promennou tVariable typu UNDEFINED a vlozi ji na offset do
* runtimeStack
* @param offset Cele cislo urcujici posunuti na zasobniku
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode insertNewVariable(int offset)
{
ecode error;
tVariable *newVar;
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = createNewVariable(&newVar, UNDEFINED, NULL);
if (error != ERR_OK)
return error;
// -------------- Vlozeni promenne na zasobnik -------------------------------
error = tRuntimeStackInsert(runtimeStack, offset, newVar);
if (error != ERR_OK)
{
freeVariable(&newVar);
return error;
}
return ERR_OK;
}
/**
* Funkce pro prevod promenne na retezec
* @param *srcVar Ukazatel na prevadenou promennou
* @return Vraci ukazatel na String obsahujici obsah promenne, pri chybe NULL
*/
String *convertVariableToString(tVariable *srcVar)
{
String *result;
if (srcVar->semantic == STRING)
{
result = stringCopy(srcVar->value);
}
else if (srcVar->semantic == NUMERIC)
{
result = doubleToString( *((double *) srcVar->value) );
}
else if (srcVar->semantic == LOGICAL)
{
if (*((bool *)srcVar->value) == true)
result = charToString("true");
else
result = charToString("false");
}
else if (srcVar->semantic == NIL)
result = charToString("Nil");
else
result = NULL;
return result;
}
/**
* Funkce provadi mocninu retezce
* @param *base Umocnovany retezec
* @param power Mocnina retezce
* @return Vraci ukazatel na String, pokud nastane chyba tak NULL
*/
String *stringPower(String *base, double power)
{
String *newString;
newString = charToString("");
if (newString == NULL)
return NULL;
int i = power;
while (i--)
{
newString = stringConcatenate(newString, base);
if (newString == NULL)
{
deallocString(newString);
return NULL;
}
}
return newString;
}
/**
* Změna aktivni instrukce - skok na instrukci LABEL v op1
* @param *instrList Ukazatel na seznam instrukci
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionGoto(tIList *instrList, tInstruction *instruction)
{
ecode error;
tIListItem **label;
if (instruction->op1 == NULL || instruction->op2 != NULL || instruction->op3 != NULL)
return ERR_INSTR_WRONG_OPERANDS;
label = (tIListItem **) instruction->op1;
error = tIListGoto(instrList, *label);
if (error != ERR_OK)
return error;
return ERR_OK;
}
/**
* Zmena aktivni instrukce na instrukci LABEL v op1 na zaklade podminky ulozene
* na zasobniku na offsetu v op2, skace se pokud je podminka splnena
* @param *instrList Ukazatel na seznam instrukci
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionIfGoto(tIList *instrList, tInstruction *instruction)
{
ecode error;
tVariable *condition;
tIListItem **jumpLabel;
jumpLabel = (tIListItem **) instruction->op1;
bool doJump = false;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 != NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne podminky ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &condition);
if (error != ERR_OK)
return error;
// Kontrola datoveho typu vyhodnoceneho vyrazu
if (condition->semantic == LOGICAL)
{
if ( ! *((bool *) condition->value))
doJump = true;
}
else if (condition->semantic == NUMERIC)
{
if ( *( (double*) condition->value ) == 0.0 )
doJump = true;
}
else if (condition->semantic == STRING)
{
if ( ((String*) condition->value)->length == 0)
doJump = true;
}
else if (condition->semantic == NIL)
{
doJump = true;
}
else
{
return ERR_INTERNAL;
}
if ( doJump )
{
error = tIListGoto(instrList, *jumpLabel);
if (error != ERR_OK)
return error;
}
return ERR_OK;
}
/**
* Provedeni instrukce call, vlozeni IP a pote BP na runtimeStack (BP == SP),
* posunuti SP pro lokani promenne a skok na prvni instrukci funkce v op1
* @param *instrList Ukazatel na seznam instrukci
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionCall(tIList *instrList, tInstruction *instruction)
{
ecode error;
int *basePointer;
tVariable *pVariable = NULL;
tFunctionData *functionRecord = NULL;
if (instruction->op1 == NULL || instruction->op2 != NULL || instruction->op3 != NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni zaznamu volane funkce ---------------------------
functionRecord = ((tFunctionData *) instruction->op1);
// -------------- Vytvoreni promenne instruction pointeru ------------------
error = createNewVariable(&pVariable, INSTRUCTION_POINTER, instrList->active->nextItem);
if (error != ERR_OK)
return error;
// -------------- Vlozeni instruction pointeru na runtime stack ------------
error = tRuntimeStackPush(runtimeStack, pVariable);
if (error != ERR_OK)
return error;
basePointer = (int*) malloc(sizeof(int));
*basePointer = *runtimeStack->bp;
// -------------- Vytvoreni promenne base ponteru -------------------------
error = createNewVariable(&pVariable, BASE_POINTER, basePointer);
if (error != ERR_OK)
return error;
// -------------- Vlozeni base pointeru na runtime stack ------------------
error = tRuntimeStackPush(runtimeStack, pVariable);
if (error != ERR_OK)
return error;
*runtimeStack->bp = runtimeStack->sp;
// -------------- Vytvoreni mista pro lokalni promenne --------------------
error = tRuntimeStackMoveSP(runtimeStack, functionRecord->varTabHead->itemCount + 1);
if (error != ERR_OK)
return error;
// -------------- Skok na prvni instrukci funkce -------------------------
error = tIListGoto(instrList, functionRecord->firstInstruction);
if (error != ERR_OK)
return error;
return ERR_OK;
}
/**
* Provedeni instrukce navratu z funkce
* Odstraneni dat vytvorenych funkci na zasobniku jejich pocet je v op1
* obnoveni hodnoty BP, vycteni a nastaveni IP, nacteni navratove hodnoty ze
* zasobniku, mazani parametru a opetovne vlozeni navratove hodnoty na zasobnik
* @param *instrList Ukazatel na seznam instrukci
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionRet(tIList *instrList, tInstruction *instruction)
{
ecode error;
int paramsCount;
tVariable *pVariable;
tVariable *retVal;
if (instruction->op1 == NULL || instruction->op2 != NULL || instruction->op3 != NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni poctu mazanych parametru ------------------------
paramsCount = *((int *) instruction->op1) + 1;
// -------------- Odstranovani dat z vrcholu -----------------------------
while (runtimeStack->sp != *(runtimeStack->bp))
{
error = tRuntimeStackPop(runtimeStack);
if (error != ERR_OK)
return error;
}
// -------------- Nacteni base pointeru z vrcholu -------------------------
error = tRuntimeStackTop(runtimeStack, (void **) &pVariable);
if (error != ERR_OK)
return error;
// Nastaveni puvodni hodnoty base pointeru
*(runtimeStack->bp) = *((int *)pVariable->value);
// -------------- Odstraneni base pointeru z vrcholu ----------------------
error = tRuntimeStackPop(runtimeStack);
if (error != ERR_OK)
return error;
// -------------- Nacteni instruction pointeru z vrcholu ------------------
error = tRuntimeStackTop(runtimeStack, (void **) &pVariable);
if (error != ERR_OK)
return error;
// -------------- Nastaveni instruction pointeru --------------------------
tIListGoto(instrList, ((tIListItem *) pVariable->value));
// -------------- Odstraneni instruction pointeru z vrcholu zasobniku -----
error = tRuntimeStackPop(runtimeStack);
if (error != ERR_OK)
return error;
// -------------- Nacteni navratove hodnoty z vrcholu zasobniku -----------
error = tRuntimeStackTop(runtimeStack, (void **) &pVariable);
if (error != ERR_OK)
return error;
// -------------- Zkopirovani navratove hodnoty ---------------------------
error = copyVariable(pVariable, &retVal);
if (error)
return error;
// -------------- Mazani parametru ----------------------------------------
while (paramsCount != 0)
{
error = tRuntimeStackPop(runtimeStack);
if (error != ERR_OK)
return error;
paramsCount--;
}
error = tRuntimeStackPush(runtimeStack, retVal);
if (error)
return error;
return ERR_OK;
}
/**
* Provedeni instrukce scitani nad operandy ulozenymi v zasobniku na
* offsetu op2 a op3 vyledek je nastaven na offset op3
* Jedna se bud o aritmeticke scitani nebo konkatenaci
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionAdd(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
String *convertedVar;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Operace scitani nad dvema cisly ----------------------------
if (op1->semantic == NUMERIC && op2->semantic == NUMERIC)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, NUMERIC);
if (error != ERR_OK)
return error;
// -------------- Vysledek operace -------------------------------------------
*((double *) result->value) = *((double *) op1->value) + *((double *) op2->value);
}
// -------------- Konkatenace retezce a druheho operandu ---------------------
else if (op1->semantic == STRING)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, STRING);
if (error != ERR_OK)
return error;
// Pokud neni druhy operand retezec, tak ho z nej vytvorime
if (op2->semantic != STRING)
{
// -------------- Konverze druheho operandu na retezec -------------------
convertedVar = convertVariableToString(op2);
if (convertedVar == NULL)
return ERR_MEMORY;
// -------------- Konkatenace dvou retezcu -------------------------------
result->value = stringConcatenateNew(op1->value, convertedVar);
deallocString(convertedVar);
}
else
result->value = stringConcatenateNew(op1->value, op2->value);
if (result->value == NULL)
return ERR_MEMORY;
}
else // Semanticka chyba - nepodporovane datove typy
{
return ERR_RUNTIME_INCOMPATIBLE_TYPES;
}
return ERR_OK;
}
/**
* Provedeni instrukce odcitani nad operandy ulozenymi v zasobniku na
* offsetu op2 a op3 vyledek je nastaven na offset op3
* Platna pouze pro cisela
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionSubtract(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Operace odcitani nad dvema cisly ---------------------------
if (op1->semantic == NUMERIC && op2->semantic == NUMERIC)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, NUMERIC);
if (error != ERR_OK)
return error;
// -------------- Rozdil dvou cisel --------------------------------------
*((double *) result->value ) = *((double *) op1->value) - *((double *) op2->value);
}
else
{
return ERR_RUNTIME_INCOMPATIBLE_TYPES;
}
return ERR_OK;
}
/**
* Provedeni instrukce nasobeni nad operandy ulozenymi v zasobniku na
* offsetu op2 a op3 vyledek je nastaven na offset op3
* Jedna se o aritmeticke nasobeni nad cisly nebo mocninu retezce
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionMultiply(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Operace nasobeni nad dvema cisly ---------------------------
if (op1->semantic == NUMERIC && op2->semantic == NUMERIC)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, NUMERIC);
if (error != ERR_OK)
return error;
// -------------- Soucin dvou cisel --------------------------------------
*((double *) result->value ) = *((double *) op1->value) * *((double *) op2->value);
}
// -------------- Operace nasobeni nad retezcem a cislem ---------------------
else if (op1->semantic == STRING && op2->semantic == NUMERIC)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, STRING);
if (error != ERR_OK)
return error;
// -------------- Mocnina retezce ----------------------------------------
result->value = stringPower(op1->value, *((double *) op2->value));
if (result->value == NULL)
return ERR_MEMORY;
}
else
{
return ERR_RUNTIME_INCOMPATIBLE_TYPES;
}
return ERR_OK;
}
/**
* Provedeni instrukce deleni nad operandy ulozenymi v zasobniku na
* offsetu op2 a op3 vyledek je nastaven na offset op3
* Plati poze pro cisla, kontrola deleni nulou
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionDivide(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Operace deleni nad dvema cisly -----------------------------
if (op1->semantic == NUMERIC && op2->semantic == NUMERIC)
{
// -------------- Deleni nulou -------------------------------------------
if ( *( (double*) op2->value ) == 0)
return ERR_RUNTIME_ZERO_DIVISION;
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, NUMERIC);
if (error != ERR_OK)
return error;
// -------------- Podil dvou cisel ---------------------------------------
*((double *) result->value ) = *((double *) op1->value) / *((double *) op2->value);
}
else
{
return ERR_RUNTIME_INCOMPATIBLE_TYPES;
}
return ERR_OK;
}
/**
* Provedeni instrukce umocneni nad operandy ulozenymi v zasobniku na
* offsetu op2 a op3 vyledek je nastaven na offset op3
* Plati pouze pro cisla
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode instructionPower(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Operace mocniny dvou cisel ---------------------------------
if (op1->semantic == NUMERIC && op2->semantic == NUMERIC)
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, NUMERIC);
if (error != ERR_OK)
return error;
// -------------- Mocnina dvou cisel -------------------------------------
*((double *) result->value ) = pow( *( (double *) op1->value ), *( (double *) op2->value ) );
}
else
{
return ERR_RUNTIME_INCOMPATIBLE_TYPES;
}
return ERR_OK;
}
/**
* Provedeni relacni instrukce nad operandy ulozenymi v zasobniku na offsetu
* op2 a op3 vyledek je nastaven na offset op3
*
* @param *instruction Ukazatel na provadenou instrukci
* @return ERR_OK pokud nenastala zadana chyba, jinak chybovy kod
*/
ecode operationRelational(tInstruction *instruction)
{
ecode error;
tVariable *result, *op1, *op2;
if (instruction->op1 == NULL || instruction->op2 == NULL || instruction->op3 == NULL)
return ERR_INSTR_WRONG_OPERANDS;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
// -------------- Nacteni prvniho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op2), (void **) &op1);
if (error != ERR_OK)
return error;
// -------------- Nacteni druheho operandu -----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op3), (void **) &op2);
if (error != ERR_OK)
return error;
if (result == NULL) // Promenna pro vysledek jeste nebyla definovana
{
// -------------- Vytvoreni nove nedefinovane promenne -----------------------
error = insertNewVariable(*((int *) instruction->op1));
if (error != ERR_OK)
return error;
// -------------- Nacteni promenne vysledku ----------------------------------
error = tRuntimeStackRead(runtimeStack, *((int *) instruction->op1), (void **) &result);
if (error != ERR_OK)
return error;
error = changeVariableType(result, LOGICAL);
if (error)
return error;
}
if (op1 == NULL || op2 == NULL)
return ERR_RUNTIME_OTHER; // Promenna operandu nebyla definovana
// -------------- Relacni operace nad dvema cisly nebo retezci ---------------
if ((op1->semantic == NUMERIC && op2->semantic == NUMERIC) ||
(op1->semantic == STRING && op2->semantic == STRING))
{
// -------------- Nastaveni datoveho typu vysledku -----------------------
error = changeVariableType(result, LOGICAL);
if (error != ERR_OK)
return error;
if (op1->semantic == NUMERIC)
{
// -------------- Porovnani dvou cisel -----------------------------------
switch (instruction->instruction)
{
case INSTR_LESSER:
*( (bool *) result->value ) = *( (double *) op1->value ) < *( (double *) op2->value );
break;
case INSTR_GREATER:
*( (bool *) result->value ) = *( (double *) op1->value ) > *( (double *) op2->value );
break;
case INSTR_LESSER_OR_EQUAL:
*( (bool *) result->value ) = *( (double *) op1->value ) <= *( (double *) op2->value );
break;
case INSTR_GREATER_OR_EQUAL:
*( (bool *) result->value ) = *( (double *) op1->value ) >= *( (double *) op2->value );
break;
case INSTR_EQUAL:
*( (bool *) result->value ) = *( (double*) op1->value ) == *( (double*) op2->value );
break;
case INSTR_NOT_EQUAL:
*( (bool *) result->value ) = *( (double*) op1->value ) != *( (double*) op2->value );
break;
default:
break;
}
}
else if(op1->semantic == STRING)
{
// -------------- Porovnani dvou retezcu ---------------------------------
int comparison;
comparison = stringCompare(op1->value, op2->value);
switch (instruction->instruction)
{
case INSTR_LESSER:
*((bool *) result->value) = (comparison < 0);
break;
case INSTR_GREATER:
*((bool *) result->value) = (comparison > 0);
break;