Repository navigation
Expand file tree
/
Copy pathscope_stack.c
More file actions
176 lines (150 loc) · 4.77 KB
/
Copy pathscope_stack.c
File metadata and controls
176 lines (150 loc) · 4.77 KB
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
/**
* @file scope_stack.c
* @author Martin Mezei (xmezeim00)
* @brief Implements Stack and its helper functions to manage working with variable
* scopes according to the memory model of the IFJcode25 programming language.
*
* @version 0.1
* @date 2025-11-20
*
* @copyright Copyright (c) 2025
*
*/
#include "scope_stack.h"
#include "symtable.h"
#include "error.h"
#include <stdlib.h>
#include <stdio.h>
#define DEFAULT_SCOPE_STACK_SIZE 10
/**
* @brief Initializes a new Scope_Stack
*
* @param scope_stack Pointer to uninitialized Scope_Stack.
*/
void scope_stack_init(Scope_Stack *scope_stack)
{
scope_stack->symtable_array = malloc(sizeof(ST_Node *) * DEFAULT_SCOPE_STACK_SIZE);
if (!scope_stack->symtable_array)
error_exit(ERR_INTERNAL);
scope_stack->stack_top_index = -1; // stackTop value for an empty Stack
scope_stack->stack_array_size = DEFAULT_SCOPE_STACK_SIZE;
}
/**
* @brief Handles Scope_Stack clean up.
*
* @param scope_stack Pointer to Scope_Stack.
*/
void scope_stack_dispose(Scope_Stack *scope_stack)
{
if (!scope_stack)
return;
// free all symtables
for (int idx = 0; idx <= scope_stack->stack_top_index; idx++)
{
if (scope_stack->symtable_array[idx])
{
st_dispose_tree(scope_stack->symtable_array[idx]);
}
}
// deallocation of symtable_array and prevention of dangling pointers
free(scope_stack->symtable_array);
scope_stack->symtable_array = NULL;
}
/**
* @brief Adds Symtable pointer on stack top.
*
* @param root_ptr Pointer to the root of a new symtable.
* @param scope_stack Pointer to Scope_Stack.
*/
void scope_stack_push(Scope_Stack *scope_stack, ST_Node *root_ptr)
{
// when trying to push to a full Stack, its size is increased before pushing
if (scope_stack_full(scope_stack))
scope_stack_increase_size(scope_stack); // if scope stack increase fails, scope_stack_increase_size() exits
// new Symtable pointer can be added to the Stack
scope_stack->stack_top_index++;
scope_stack->symtable_array[scope_stack->stack_top_index] = root_ptr;
}
/**
* @brief Increases the size of symtable_array to fit one more element.
* @param scope_stack Pointer to scope_stack.
*/
void scope_stack_increase_size(Scope_Stack *scope_stack)
{
scope_stack->stack_array_size++;
scope_stack->symtable_array = realloc(scope_stack->symtable_array, (sizeof(ST_Node *) * scope_stack->stack_array_size));
if (!scope_stack->symtable_array)
error_exit(ERR_INTERNAL);
}
/**
* @brief Removes Symtable pointer from stack top. Calls Disposte_Tree before popping
*
* @param scope_stack Pointer to the scope_stack.
*/
void scope_stack_pop(Scope_Stack *scope_stack)
{
if (scope_stack_empty(scope_stack)) // cannot pop from an empty stack, nothing happens
return;
st_dispose_tree(*scope_stack_top(scope_stack)); // tree is freed before popping
scope_stack->stack_top_index--;
}
/**
* @brief Checks if Scope_Stack is empty.
* @param scope_stack Pointer to Scope_Stack.
*/
bool scope_stack_empty(Scope_Stack *scope_stack)
{
if (!scope_stack)
error_exit(ERR_INTERNAL);
return scope_stack->stack_top_index == -1;
}
/**
* @brief Checks if Scope_Stack if full.
* @param scope_stack Pointer to Scope_Stack.
*/
bool scope_stack_full(Scope_Stack *scope_stack)
{
if (!scope_stack)
error_exit(ERR_INTERNAL);
return scope_stack->stack_top_index == (int)scope_stack->stack_array_size - 1;
}
/**
* @brief Looks through all the symtables that are currently on stack and tries to find a specific symbol.
*
* @param scope_stack Pointer to a scope_stack.
* @param key Key of a symbol we look for.
* @param block_id Pointer to a helper variable inside the semantic analysis module used for name mangling.
*
* @return True if symbol was found, false otherwise.
*/
ST_Node *scope_stack_var_lookup(Scope_Stack *scope_stack, Key *key, unsigned *block_id)
{
if (scope_stack_empty(scope_stack))
return NULL;
ST_Node *symbol;
for (int idx = scope_stack->stack_top_index; idx >= 0; idx--) // check the whole scope stack for var
{
symbol = st_search(scope_stack->symtable_array[idx], key);
if (symbol)
{
*block_id = symbol->block_id;
return symbol;
}
}
return NULL; // local var not found
}
/**
* @brief Returns an adress of a Symtable that is currently on top of the stack.
* @param scope_stack Pointer to Scope_Stack.
*
* @return Pointer to an existing Symtable root node or NULL if stack is empty.
*/
ST_Node **scope_stack_top(Scope_Stack *scope_stack)
{
// cannot return any Symtables from an empty stack
if (scope_stack_empty(scope_stack))
{
return NULL;
}
return &scope_stack->symtable_array[scope_stack->stack_top_index];
}