-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_tab.c
112 lines (96 loc) · 3.21 KB
/
array_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
#include "array_tab.h"
int arrays_count;
int arrays_capacity = 10;
int arrays_vars = 0;
varArray** arrays_array;
// Utility function to initialize vars_array
void initArrayTab(){
arrays_array = malloc(arrays_capacity*sizeof(varArray*));
}
/*
Function that returns the saved variables in a given char address.
*/
void arrays_to_string(FILE *returns)
{
if(arrays_count == 0)
{
fprintf(stderr, "No arrays found.\n\n");
}
int i,y;
for (i = 0; i < arrays_count; i++){
// For each variable in the table, compare if it is the same as the input variable.
varArray* current_array = arrays_array[i];
variable* act_array = current_array->array;
rangelist_type* currant_rangelist = current_array->range;
int nbvars = current_array->nbvars;
char str_range[100];
char tmp_buff[100];
bool first = true;
while(currant_rangelist != NULL)
{
if(first)
{
snprintf(tmp_buff,100,"[%d..%d",currant_rangelist->deb, currant_rangelist->fin);
}
else
{
snprintf(tmp_buff,100,"%s,%d..%d",str_range,currant_rangelist->deb, currant_rangelist->fin);
}
snprintf(str_range,100,"%s",tmp_buff);
currant_rangelist = currant_rangelist->suivant;
first = false;
}
snprintf(tmp_buff,100,"%s]",str_range);
snprintf(str_range,100,"%s",tmp_buff);
fprintf(returns, "scalpavar: %s%s -- mipsvar: %d($sp) %d($sp)\n", act_array->scalpavar,str_range, act_array->p_memoire,nbvars*4);
}
fprintf(returns, "\n");
}
/*
Function that simply returns the code of the variable given in the array of variables.
If the variable does not exists, return NULL.
*/
varArray* getArray(char* varName){
int i;
for (i = 0; i < arrays_count; i++){
// For each variable in the table, compare if it is the same as the input variable.
if (strcmp(arrays_array[i]->array->scalpavar, varName) == 0){
return arrays_array[i]; // Return the position of the variable in the table
}
}
return NULL;
}
/*
Function that returns the result of the insertion in the vars_array.
*/
varArray* insertArray(char* varName, int context, arraytype_type* arraytype, FILE *returns)
{
int type = arraytype->type;
rangelist_type* rangelist = arraytype->rangelist;
int dim = 0;
int nbvars = 1;
// If we add the variable in the table, update the variable_capacity
if (arrays_count >= arrays_capacity) {
arrays_capacity*=2;
arrays_array = (varArray**)realloc(arrays_array, arrays_capacity * sizeof(varArray*));
}
rangelist_type* current_rangelist = rangelist;
while (current_rangelist != NULL)
{
nbvars *= current_rangelist->length;
dim ++;
current_rangelist = current_rangelist->suivant;
}
variable* array = insertVar(varName,context,5,nbvars-1);//type = array_val
varArray* new_array = malloc(sizeof(varArray));
new_array->array = array;
new_array->range = rangelist;
new_array->dim = dim;
new_array->nbvars = nbvars;
new_array->type = type;
arrays_array[arrays_count] = new_array;
arrays_count++;
arrays_vars += nbvars;
// Variable successfully added. Return true then.
return new_array;
}