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
/
symbol.c
101 lines (87 loc) · 2.17 KB
/
symbol.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
/*
* 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 "symbol.h"
Context *InitContext()
{
// malloc Context
Context *funCont = malloc(sizeof(Context));
if(funCont == NULL)
{
setError(ERR_Allocation)
return NULL;
}
// malloc arg array
funCont->arg = malloc(sizeof(Symbol*)*DEFAULT_ARG_NUM);
if(funCont->arg == NULL)
{
setError(ERR_Allocation);
free(funCont);
return NULL;
}
// malloc HASH array
funCont->locTable = malloc(sizeof(SymbolList*)*DEFAULT_HASH_SIZE);
if (funCont->locTable == NULL)
{
setError(ERR_Allocation);
free(funCont->arg);
free(funCont);
return NULL;
}
// erase HASH array
for(unsigned i=0;i<DEFAULT_HASH_SIZE;i++)
funCont->locTable[i]=NULL;
// erase arg array
for(unsigned i=0;i<DEFAULT_ARG_NUM;i++)
funCont->arg[i]=NULL;
// set other values of Context
funCont->argCount = 0;
funCont->locCount = 0;
funCont->argMax = DEFAULT_ARG_NUM;
funCont->locSize = DEFAULT_HASH_SIZE;
funCont->instrucIndex = 0;
funCont->returnType = T_Undefined;
return funCont;
}
// to add Symbol without argument call htab_addSymbol in ial.c file
// SymbolContext is NULL if SymbolType is variable
Symbol *AddArgToContext(Context *funCont, SymbolType type, char *name, Context *symbolContext)
{
if(funCont->argCount >= funCont->argMax)
{
funCont->argMax *= 2;
funCont->arg = realloc(funCont->arg, sizeof(Symbol*)*funCont->argMax);
}
Symbol *symbol = SymbolAdd(funCont, type, name, symbolContext, NULL, false);
if(getError())
return NULL;
funCont->arg[funCont->argCount++] = symbol;
symbol->index = -funCont->argCount;
funCont->locCount--; // Arguments are not included in locCount number
return symbol;
}
void FreeContext(Context *funCont)
{
if (funCont==NULL)
return;
ContextLocTableFree(funCont);
if(funCont->locTable != NULL)
free(funCont->locTable);
funCont->locTable = NULL;
if(funCont->arg != NULL)
free(funCont->arg);
funCont->arg = NULL;
free(funCont);
funCont = NULL;
}