-
Notifications
You must be signed in to change notification settings - Fork 0
/
paramstack.c
184 lines (138 loc) · 3.84 KB
/
paramstack.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
/**
* Project: IFJ21 imperative language compiler
*
* Brief: Stack for function call parameters
*
* Author: David Chocholaty <xchoch09>
*
*/
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "paramstack.h"
void param_stack_init (param_stack* stack)
{
stack->top = NULL;
}
bool param_stack_push (param_stack* stack, param_type_t param_type, param_attribute_t param_attr)
{
param_stack_item* new_item = (param_stack_item*) malloc(sizeof(param_stack_item));
if (!new_item)
{
return false;
}
new_item->type = param_type;
switch (param_type)
{
case P_ID:
new_item->attribute.id = (char*) malloc(strlen(param_attr.id) + 1);
if (!(new_item->attribute.id))
{
free(new_item);
new_item = NULL;
return false;
}
strcpy(new_item->attribute.id, param_attr.id);
break;
case P_INT:
new_item->attribute.integer = param_attr.integer;
break;
case P_NUMBER:
new_item->attribute.number = param_attr.number;
break;
case P_STR:
new_item->attribute.id = (char*) malloc(strlen(param_attr.str) + 1);
if (!(new_item->attribute.id))
{
free(new_item);
new_item = NULL;
return false;
}
strcpy(new_item->attribute.id, param_attr.str);
break;
default:
break;
}
new_item->next = stack->top;
stack->top = new_item;
return true;
}
bool param_stack_pop (param_stack* stack, param_type_t* param_type, param_attribute_t* param_attr)
{
param_stack_item* tmp = NULL;
if (param_type == NULL &&
param_attr == NULL &&
stack != NULL &&
stack->top != NULL)
{
tmp = stack->top;
if (tmp->type == P_ID)
{
free(tmp->attribute.id);
tmp->attribute.id = NULL;
}
else if(tmp->type == P_STR)
{
free(tmp->attribute.str);
tmp->attribute.str = NULL;
}
stack->top = tmp->next;
free(tmp);
tmp = NULL;
return true;
}
if (stack != NULL &&
stack->top != NULL)
{
tmp = stack->top;
stack->top = tmp->next;
*param_type = tmp->type;
switch (*param_type)
{
case P_ID:
param_attr->id = (char*) malloc(strlen(tmp->attribute.id) + 1);
if (!(param_attr->id))
{
free(tmp->attribute.id);
tmp->attribute.id = NULL;
free(tmp);
tmp = NULL;
return false;
}
strcpy(param_attr->id , tmp->attribute.id);
free(tmp->attribute.id);
tmp->attribute.id = NULL;
break;
case P_INT:
param_attr->integer = tmp->attribute.integer;
break;
case P_NUMBER:
param_attr->number = tmp->attribute.number;
break;
case P_STR:
param_attr->str = (char*) malloc(strlen(tmp->attribute.str) + 1);
if (!(param_attr->str))
{
free(tmp->attribute.str);
tmp->attribute.str = NULL;
free(tmp);
tmp = NULL;
return false;
}
strcpy(param_attr->str, tmp->attribute.str);
free(tmp->attribute.str);
tmp->attribute.str = NULL;
break;
default:
break;
}
free(tmp);
tmp = NULL;
return true;
}
return false;
}
void param_stack_dispose (param_stack* stack)
{
while (param_stack_pop(stack, NULL, NULL));
}