This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathial.c
272 lines (245 loc) · 5.47 KB
/
ial.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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
#include "ial.h"
/*
Find substring in string
original input->data
looking for find->data
REQUIRED
both input structure have to contain LENGTH of data
Return
SUCCESSFUL
index which point to input index (start from "0")
UNSUCCESSFUL
length of input length
ERR_Allocation if unable to malloc
*/
extern Context *mainContext;
int FindString(String *input, String *find)
{
if(find->length == 0)
return 1;
int *Mask = GetFormula(find);
if(Mask == NULL)
{
setError(ERR_Allocation);
return 0; // did not find value + ERROR
}
uint32_t inInd=1, fiInd=1;
while(inInd<=input->length && fiInd<=find->length)
{
if(input->data[inInd-1] == find->data[fiInd-1])
{
inInd++;
fiInd++;
}
else
{
fiInd = Mask[fiInd-1];
if(fiInd==0)
{
inInd++;
fiInd++;
}
}
}
free(Mask);
Mask = NULL;
if(fiInd>find->length)
return inInd - find->length; // index where strings match
return 0; // did not find value
}
/*
Imput is string and it make Mask for KMP algorithm
required LENGTH in String structure
*/
int *GetFormula(String *find)
{
int r;
int *Mask = malloc((find->length) * sizeof(int));
if(Mask==NULL)
return NULL;
Mask[0] = 0;
for (uint32_t i = 1; i < find->length; ++i)
{
r = Mask[i-1];
while( (r>0) && (find->data[r-1] != find->data[i-1]) )
r=Mask[r-1];
Mask[i] = r+1;
}
return Mask;
}
/*
Imput is unordered array of char ended with character == 0
sort that array based on ASCII number.
used Recursive Quick Sort
*/
// get length of string and call QuickSortRecursive function
void QuickSort(String *string)
{
if(string->length < 1)
return;
QuickSortRecursive(string->data, 0, string->length - 1);
}
//QuickSort which use recursive function
void QuickSortRecursive(char *arr, int left, int right)
{
int i = left;
int j = right;
int pm = arr[(i+j)/2];
char swap;
do
{
while (arr[i] < pm)
i = i + 1;
while (arr[j] > pm)
j = j - 1;
if (i <= j)
{
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
i++;
j--;
}
}while (i<=j);
if (left < j)
QuickSortRecursive(arr,left,j);
if (i < right)
QuickSortRecursive(arr,i,right);
}
// Calculate index for HashFunction
uint32_t GetHash(const char *str, uint32_t htab_size)
{
uint32_t h=0;
const uint8_t *p;
for(p=(const uint8_t*)str; *p!='\0'; p++)
h = 65599*h + *p;
return h % htab_size;
}
Symbol *SymbolFind(Context *funCont, char *name)
{
if (funCont==NULL || name==NULL) return NULL;
/* Vypocitanie indexu kde je kluc ulozeny a ulozenie prveho listu*/
SymbolList *item;
item = funCont->locTable[GetHash(name,funCont->locSize)];
for(; item != NULL; item=item->next)
if(strcmp(item->data.name, name) == 0)
break;
return item == NULL ? NULL : &(item->data);
}
Symbol *SymbolAdd(Context *funCont, SymbolType type, char *name, Context *symbolContext, Symbol *foundSymbol, bool skipCheck)
{
Symbol *symbol = foundSymbol?foundSymbol:SymbolFind(funCont, name);
if (symbol != NULL)
{
// Symbol already exist
setError(ERR_RedefVar);
return NULL;
}
SymbolList *newItem = malloc(sizeof(SymbolList));
if (newItem == NULL)
{
setError(ERR_Allocation);
return NULL;
}
// fill newItem
newItem->data.type = type;
if (funCont == mainContext)
{
// global variables indexed from 1
newItem->data.index = ++(funCont->locCount);
}
else
{
// local variables indexed from 2
newItem->data.index = ++(funCont->locCount) + 1;
// Check if var name doesn't collide with function
if(!skipCheck)
{
SymbolList **table = mainContext->locTable;
for(int32_t index = 0; index<mainContext->locSize; index++)
{
for(SymbolList *item = table[index]; item != NULL; item=item->next)
{
if(item->data.type == T_FunPointer && strcmp(name,item->data.name) == 0)
{
setError(ERR_VarAsFunc);
return NULL;
}
}
}
}
}
newItem->data.name = name;
newItem->data.funCont = symbolContext;
newItem->data.stateFunc = FS_Undefined;
newItem->data.adressVector = NULL;
if(type == T_FunPointer)
funCont->locCount--;
uint32_t index = GetHash(name, funCont->locSize);
// save newItem to first position of hashTable[index]
newItem->next = funCont->locTable[index];
funCont->locTable[index] = newItem;
return &(newItem->data);
}
bool ResizeHashTable(Context *funCont, uint32_t NewSize)
{
SymbolList **tmp = malloc(sizeof(SymbolList*)* NewSize);
SymbolList *item;
uint32_t x;
for (int32_t i = 0; i < funCont->locSize; ++i)
{
item = funCont->locTable[i];
for(; item != NULL; item=item->next)
{
x = GetHash(item->data.name, NewSize);
item->next = tmp[x];
tmp[x] = item;
}
}
free(funCont->locTable);
funCont->locTable = tmp;
return true;
}
void ContextLocTableFree(Context *t)
{
if (t==NULL) return;
if (t->locTable == NULL) return;
SymbolList *item = NULL;
SymbolList *temp = NULL;
for (int32_t i = 0; i < t->locSize; i++)
{
if (t->locTable[i]==NULL)
continue;
for(item=t->locTable[i];item != NULL;item=temp)
{
temp=item->next;
if(item->data.type == T_FunPointer)
FreeContext(item->data.funCont);
SymbolListFree(item);
}
t->locTable[i] = NULL;
}
free(t->locTable);
t->locTable = NULL;
}
void SymbolListFree(SymbolList *t)
{
if (t==NULL) return;
//destroyString(&t->data.name);
free(t);
t = NULL;
}