-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
168 lines (144 loc) · 3.99 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
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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file stack.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Implementation of helper functions for working with stack
*/
#include "stack.h"
#include <stdio.h>
#include "error.h"
#include "token.h"
#define DEFAULT_SIZE 8
stack_t stack_new() {
// Create new struct
stack_t stack = {
.tokens = malloc(DEFAULT_SIZE * sizeof(token_term_t*)),
.size = DEFAULT_SIZE,
.len = 0,
};
// Check if malloc failed
if (stack.tokens == NULL) {
error_exit(ERR_INTERNAL);
}
return stack;
}
void stack_free(stack_t* stack) {
// Free the stack
for (int i = 0; i < stack->len; i++) {
token_term_free(stack->tokens[i]);
stack->tokens[i] = NULL;
}
free(stack->tokens);
// Reset values just to be sure
stack->tokens = NULL;
stack->len = 0;
stack->size = 0;
}
void stack_empty(stack_t* stack) {
// Just set length to 0 (this is correct)
stack->len = 0;
}
void resize_stack(stack_t* stack) {
token_term_t** new_tokens = realloc(stack->tokens, stack->size * 2 * sizeof(token_term_t*));
if (new_tokens == NULL) {
error_exit(ERR_INTERNAL);
}
stack->tokens = new_tokens;
stack->size *= 2;
}
void stack_push(stack_t* stack, token_term_t* token) {
// Enlarge buffer if needed
if (stack->len + 1 >= stack->size) {
resize_stack(stack);
}
// Add token to the stack
stack->tokens[stack->len] = token;
stack->len++;
}
token_term_t* stack_pop(stack_t* stack) {
// Check if stack is empty
if (stack->len == 0) {
error_exit(ERR_SYN);
}
// Get token from the stack
token_term_t* token = stack->tokens[stack->len - 1];
stack->len--;
return token;
}
token_term_t* stack_top(stack_t* stack) {
// Check if stack is empty
if (stack->len == 0) {
error_exit(ERR_SYN);
}
// Get token from the stack
token_term_t* token = stack->tokens[stack->len - 1];
return token;
}
token_term_t* stack_pop_terminal(stack_t* stack) {
// Check if stack is empty
if (stack->len == 0) {
error_exit(ERR_SYN);
}
token_term_t* token = NULL;
for (int i = 1; i <= stack->len; i++) {
token = stack->tokens[stack->len - i];
if (token->terminal) {
stack->len--;
return token;
}
}
error_exit(ERR_SYN);
return token;
}
token_term_t* stack_top_terminal(stack_t* stack) {
// Check if stack is empty
if (stack->len == 0) {
error_exit(ERR_SYN);
}
token_term_t* token = NULL;
for (int i = 1; i <= stack->len; i++) {
token = stack->tokens[stack->len - i];
if (token->terminal) {
return token;
}
}
error_exit(ERR_SYN);
return token;
}
void stack_push_after_terminal(stack_t* stack) {
// Check if stack is empty
if (stack->len == 0) {
error_exit(ERR_SYN);
}
token_term_t* token;
for (int i = 1; i <= stack->len; i++) {
token = stack->tokens[stack->len - i];
if (token->terminal) {
if (stack->len + 1 >= stack->size) {
resize_stack(stack);
}
for (int j = 1; j < i + 1; j++) {
stack->tokens[stack->len - j + 1] = stack->tokens[stack->len - j];
}
stack->tokens[stack->len - i + 1] =
token_term_new(token_new(TOK_HANDLE_START, 0, 0), false);
stack->len++;
return;
}
}
error_exit(ERR_SYN);
}
void stack_pprint(stack_t* stack) {
if (stack->len == 0) {
fprintf(stderr, "empty stack\n");
return;
}
for (int i = 0; i < stack->len; i++) {
fprintf(stderr, "%d[%d]: %s\n", i, stack->tokens[i]->terminal,
token_to_string(stack->tokens[i]->value.type));
}
}