-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlists.c
More file actions
198 lines (187 loc) · 4.39 KB
/
lists.c
File metadata and controls
198 lines (187 loc) · 4.39 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
#include "lists.h"
#include "hash.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//Make List
ILIST mkList(Instr n, ILIST l1) {
ILIST l = malloc(sizeof(struct list));
l->head = n;
l->tail = l1;
return l;
}
//APPEND LISTS
ILIST append(ILIST l1, ILIST l2){
ILIST new = l1;
if(l1 == NULL) return l2;
while (l1->tail != NULL) {
l1 = l1->tail;
}
l1->tail = l2;
return new;
}
//ADD IN LAST
ILIST addLast(Instr n, ILIST l1){
ILIST l2 = mkList(n,NULL);
ILIST new = append(l1,l2);
return new;
}
//PRINT LIST
void printList(ILIST l) {
while (l != NULL) {
escrever(head(l));
l = l->tail;
}
}
//GETTERS
Instr head(ILIST l) {
return l->head;
}
ILIST tail(ILIST l) {
return l->tail;
}
int getIndex(char* s, ILIST lista){
Instr i;
while(lista != NULL){
i = head(lista);
if(i.op == LABEL){
if (strcmp(s, getName(i.first)) == 0) {
return i.index;
}
}
lista = lista->tail;
}
return 0;
}
//EXECUTE LIST
void execlist(ILIST lista) {
ILIST listastart = lista;
while (lista != NULL) {
Instr i = head(lista);
Instr g;
char* lab;
List* li;
int val, a1, a2;
switch(i.op){
case ERROR:
printf("ERROR\n");
lista->tail = NULL;
break;
case START:
break;
case QUIT:
lista->tail = NULL;
break;
case PRINT:
li = lookup(getName(i.first));
val = getHashValue(li);
printf("%d \n",val);
break;
case READ:
printf("Valor de %s = ", getName(i.first));
scanf("%d", &val);
insert(getName(i.first), val);
break;
case IF:
li = lookup(getName(i.first));
val = getHashValue(li);
if (val == 0) {
lista = lista->tail;
}
break;
case GOTO:
lista = listastart;
lab = getName(i.first);
a1 = getIndex(lab, listastart);
//lista = lista->tail;
g = head(lista);
while (a1 != g.index) {
lista = lista->tail;
g = head(lista);
}
break;
case LABEL:
break;
case ATRIB:
insert(getName(i.first), getVal(i.second));
break;
case ADD:
switch (i.type) {
case 1:
a1 = getHashValue(lookup(getName(i.second)));
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a1 + a2));
break;
case 2:
insert(getName(i.first), (getVal(i.second) + getVal(i.third)));
break;
case 3:
a1 = getHashValue(lookup(getName(i.second)));
insert(getName(i.first), (a1 + getVal(i.third)));
break;
case 4:
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a2 + getVal(i.second)));
break;
}
break;
case SUB:
switch (i.type) {
case 1:
a1 = getHashValue(lookup(getName(i.second)));
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a1 - a2));
break;
case 2:
insert(getName(i.first), (getVal(i.second) - getVal(i.third)));
break;
case 3:
a1 = getHashValue(lookup(getName(i.second)));
insert(getName(i.first), (a1 - getVal(i.third)));
break;
case 4:
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a2 - getVal(i.second)));
break;
}
break;
case MUL:
switch (i.type) {
case 1:
a1 = getHashValue(lookup(getName(i.second)));
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a1 * a2));
break;
case 2:
insert(getName(i.first), (getVal(i.second) * getVal(i.third)));
break;
case 3:
a1 = getHashValue(lookup(getName(i.second)));
insert(getName(i.first), (a1 * getVal(i.third)));
break;
case 4:
a2 = getHashValue(lookup(getName(i.third)));
insert(getName(i.first), (a2 * getVal(i.second)));
break;
}
break;
}
lista = lista->tail;
}
}
//READ LIST
ILIST readList(FILE *fp){
char* buffer = NULL;
size_t buffsize = 32;
int i = 1;
ssize_t aux;
ILIST lista = mkList(mkInstr(START,empty(),empty(),empty(),0, 0),NULL);
while ((aux = getline(&buffer, &buffsize, fp)) != -1) {
removeSpaces(buffer);
Instr inst = parseInstr(buffer, i);
i++;
lista = addLast(inst, lista);
}
return lista;
}