-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilist.c
133 lines (113 loc) · 2.82 KB
/
ilist.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
// ilist.c
/******************************************************************************
* Implementace interpretu imperativniho jazyka IFJ12 *
* Autori: *
* Tomas Nestrojil (xnestr03) *
******************************************************************************
*/
#include "ilist.h"
#include "errnum.h"
#include "global.h"
#include "variable.h"
#include <stdio.h>
#include <stdlib.h>
// Inicializace seznamu instrukci
ecode tIListInit(tIList *list)
{
if (list == NULL)
return ERR_LIST;
list->active = list->first = list->last = NULL;
return ERR_OK;
}
// Uvolneni seznamu instrukci
ecode tIListFree(tIList *list)
{
tIListItem *tmp;
if (list == NULL)
return ERR_LIST;
while (list->first != NULL)
{
tmp = list->first->nextItem;
// Uvolneni literalu alokovanych na halde
if (list->first->instruction->instruction == INSTR_PUSH)
{
freeVariable((tVariable **)&(list->first->instruction->op1));
}
else if (list->first->instruction->instruction == INSTR_MOV)
{
freeVariable((tVariable **) &(list->first->instruction->op2));
}
free(list->first->instruction);
free(list->first);
list->first = tmp;
}
return ERR_OK;
}
// Vlozeni posledni instrukce
ecode tIListInsertLast(tIList *list, tInstruction *instruction)
{
if (list == NULL)
return ERR_LIST;
tIListItem *tmp = malloc(sizeof(tIListItem));
if (tmp == NULL)
return ERR_MEMORY;
tmp->lineNumber = _lineNumber;
tmp->nextItem = NULL;
tmp->instruction = instruction;
// Prvni prvek
if (list->last == NULL)
{
list->first = tmp;
}
else
{
list->last->nextItem = tmp;
}
list->last = tmp;
return ERR_OK;
}
// Nastaveni aktivni instrukce na nasledujici
ecode tIListNext(tIList *list)
{
if (list == NULL)
return ERR_LIST;
if (list->active != NULL)
list->active = list->active->nextItem;
return ERR_OK;
}
// Skok na dalsi instrukci
ecode tIListGoto(tIList *list, tIListItem *gotoInstruction)
{
if (list == NULL)
return ERR_LIST;
list->active = gotoInstruction;
return ERR_OK;
}
// Vrati ukazatel na posledni instrukci
tIListItem *tIListGetLast(tIList *list)
{
if (list == NULL)
return NULL;
return list->last;
}
// Vrati aktivni instrukci
tInstruction *tIListGetActiveInstruction(tIList *list)
{
if (list == NULL)
return NULL;
if ( list->active == NULL )
return NULL;
return list->active->instruction;
}
tInstruction *generateInstruction(InstructionType type, void *op1, void *op2, void *op3)
{
tInstruction *newInstruction = malloc(sizeof(tInstruction));
if (newInstruction == NULL)
return NULL;
// Nastaveni operandu instrukce a typu instrukce
newInstruction->instruction = type;
newInstruction->op1 = op1;
newInstruction->op2 = op2;
newInstruction->op3 = op3;
return newInstruction;
}