-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
113 lines (91 loc) · 1.83 KB
/
stack.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
/**
* @project IFJ17
* @file stack.c
* @author Jan Bartosek (xbarto92)
* @author Roman Bartl (xbartl06)
* @brief Operations with Dynamic Stack
*/
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"
#include "error_codes.h"
#include "garbage_collector.h"
/**
* @copydoc stackInit
*/
void stackInit(Stack *S) {
S->item = gcmalloc(MAX_VALUE * sizeof(struct stackItem));
S->top = -1;
S->maxTerm = -1;
S->size = MAX_VALUE;
}
/**
* @copydoc stackDestroy
*/
void stackDestroy(Stack *S) {
gcfree(S->item);
S->maxTerm = -1;
S->top = -1;
S->size = 0;
}
/**
* @copydoc stackResize
*/
void stackResize(Stack *S) {
//TODO - maybe here will be a mistake .. but for now it seems it's ok
S->item = gcrealloc(S->item, 2 * S->size * sizeof(struct stackItem));
S->size = 2 * S->size;
}
/**
* @copydoc stackEmpty
*/
bool stackEmpty(Stack *S) {
if (S->top < 0)
return true;
return false;
}
/**
* @copydoc stackFull
*/
bool stackFull(Stack *S) {
if (S->top == S->size - 1)
return true;
return false;
}
/**
* @copydoc stackPush
*/
void stackPush(Stack *S, BinaryTreePtr *ptr, ast_exp *Exp, token *Token, precedStack symbol, ast_stmt* stmt) {
if (stackFull(S)) {
stackResize(S);
}
S->top++;
S->item[S->top].Exp = Exp;
S->item[S->top].ptr = ptr;
S->item[S->top].symbol = symbol;
if(Token != NULL) {
S->item[S->top].Token = *Token;
}
S->item[S->top].stmt = stmt;
}
/**
* @copydoc stackPop
*/
void stackPop(Stack *S) {
if (!stackEmpty(S)) {
S->top--;
}
}
void stackTop(Stack *S, stackItem *item) {
if (stackEmpty(S)) {
return ;
}
*item = S->item[S->top];
}
void stackTopTerminal(Stack *S, stackItem *item) {
if (stackEmpty(S)) {
return ;
}
*item = S->item[S->maxTerm];
}