-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.c
150 lines (99 loc) · 3.01 KB
/
tab.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
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "tab.h"
#define true 1
#define false 0
struct nodo *primero = NULL;
int deleteScope(int sc) {
int res = 0;
struct nodo *puntero = primero;
if(puntero != NULL) {
while(puntero != NULL && sc < puntero->scope) {
removeTop();
puntero = primero;
res++;
}
}
return res;
}
void removeTop() {
if(primero->categoria == funcion) {
while (primero->param != NULL) {
struct nodo *aux = primero->param->param;
free(primero->param);
primero->param = aux;
}
}
if (primero->array != NULL) free(primero->array);
struct nodo *sig = primero->sig;
free(primero);
primero = sig;
}
struct nodo * search(char* id, enum category categoria) {
struct nodo *puntero = primero;
while (puntero != NULL) {
if(puntero->categoria == categoria && strcmp(id, puntero->id) == 0) {
return puntero;
}
puntero = puntero->sig;
}
return NULL;
}
int countFunctionParameters(char* id) {
struct nodo *puntero = search(id, funcion);
if(puntero == NULL) return -1;
int count = 0;
while(puntero->param != NULL) {
count++;
puntero = puntero->param;
}
return count;
}
struct nodo * getParameterByNumber(char *id, int n) {
struct nodo *puntero = search(id, funcion);
if(puntero == NULL) return NULL;
int count = 0;
while(puntero != NULL && count < n) {
count++;
puntero = puntero->param;
}
return puntero;
}
int add(char* id, enum type tipo, enum category categoria, int scope, int address, struct array *array) {
if(search(id, categoria) != NULL) return false;
struct nodo *nuevo_simbolo = malloc(sizeof(struct nodo));
nuevo_simbolo->id = id;
nuevo_simbolo->tipo = tipo;
nuevo_simbolo->categoria = categoria;
nuevo_simbolo->scope = scope;
nuevo_simbolo->address = address;
nuevo_simbolo->array = array;
if(primero != NULL && categoria == param) {
struct nodo *puntero = primero;
while(puntero->param != NULL) puntero = puntero->param;
puntero->param = nuevo_simbolo;
} else {
nuevo_simbolo->sig = primero;
primero = nuevo_simbolo;
}
return true;
}
void show() {
printf("-- Inicio Tabla Simbolos --\n");
struct nodo *puntero = primero;
while (puntero != NULL) {
printf("type = %d | categ = %d | scope = %d | id = %s | address = %d\n", puntero->tipo, puntero->categoria, puntero->scope, puntero->id, puntero->address);
if(puntero->categoria == funcion) {
struct nodo *punteroParam = puntero->param;
printf("-- Inicio Parametros --\n");
while (punteroParam != NULL) {
printf("type = %d | categ = %d | scope = %d | id = %s | address = %d\n", punteroParam->tipo, punteroParam->categoria, punteroParam->scope, punteroParam->id, punteroParam->address);
punteroParam = punteroParam->param;
}
printf("-- Fin Parametros --\n\n");
}
puntero = puntero->sig;
}
printf("-- Fin Tabla Simbolos --\n\n");
}