forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.c
139 lines (121 loc) · 2.8 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
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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#include "symbol.h"
#include "nierr.h"
#include <stdlib.h>
#include <string.h>
void copyValue(const Value *src, Value *dest)
{
dest->type = src->type;
switch(src->type) {
case VT_Null:
case VT_Undefined:
return;
case VT_String:
copyString(&(src->data.s), &(dest->data.s));
break;
default:
dest->data = src->data;
}
}
void tokenToValue(const Token *src, Value *dest)
{
switch (src->type) {
case STT_Number:
dest->type = VT_Integer;
dest->data.i = src->n;
break;
case STT_Double:
dest->type = VT_Double;
dest->data.d = src->d;
break;
case STT_String:
dest->type = VT_String;
initString(&(dest->data.s));
copyString(&(src->str), &(dest->data.s));
break;
case STT_Bool:
dest->type = VT_Bool;
dest->data.b = src->n;
break;
case STT_Null:
dest->type = VT_Null;
break;
default:
setError(ERR_Convert);
return;
}
}
void initSymbol(Symbol *symbol)
{
memset(symbol, 0, sizeof(Symbol));
}
void deleteSymbol(Symbol *symbol)
{
if (symbol->type == ST_Function)
freeFunction((Function**)&symbol->data);
else
freeVariable((Variable**)&symbol->data);
}
void copySymbol(const Symbol *src, Symbol *dest)
{
copySymbolData(src->data, dest->data);
dest->type = src->type;
// Association with string in Token
dest->key = src->key;
}
void copySymbolData(const SymbolData *src, SymbolData *dest)
{
if (src != NULL && dest != NULL) {
// Unsupported operation
setError(ERR_Internal);
}
}
Variable* newVariable()
{
Variable *tmp = malloc(sizeof(Variable));
memset(tmp, 0, sizeof(Variable));
return tmp;
}
void freeVariable(Variable **ppt)
{
if (ppt != NULL) {
free(*ppt);
*ppt = NULL;
}
}
Function* newFunction()
{
Function *tmp = malloc(sizeof(Function));
memset(tmp, 0, sizeof(Function));
initContext(&tmp->context);
return tmp;
}
void freeFunction(Function **ppt)
{
if (ppt != NULL) {
if (*ppt != NULL) {
deleteContext(&(*ppt)->context);
}
free(*ppt);
*ppt = NULL;
}
}