-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
121 lines (103 loc) · 3.07 KB
/
Copy pathstack.c
File metadata and controls
121 lines (103 loc) · 3.07 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
/*
* Project: Compiler for imperative programing language IFJ20
*
* File: stack.c
* Brief: Implementation of token stack.
*
* Authors: Hladký Tomáš xhladk15@stud.fit.vutbr.cz
* Kostolányi Adam xkosto04@stud.fit.vutbr.cz
* Makiš Jozef xmakis00@stud.fit.vutbr.cz
* Bartko Jakub xbartk07@stud.fit.vutbr.cz
*/
#include <stdlib.h>
#include "stack.h"
#include "error.h"
void stack_init(tokenStack **stack)
{
*stack = malloc(sizeof(tokenStack));
// Insterting the empty flag token
stackElemPtr newElem = (stackElemPtr)malloc(sizeof(struct stackElem));
if (!newElem)
{
// instert error call
error(99, "stack", "stack_init", "Allocation error");
}
newElem->nextTok = NULL;
newElem->token.token_type = T_EMPTY;
newElem->data = NULL;
newElem->expr = false;
//newElem->token.attr.int_lit = 0; Not important ??
(*stack)->topToken = newElem;
//printf("STACK INIT\t%p\t%p\t%d\n", newElem, stack->topToken, newElem->token.token_type);
}
void stack_push(tokenStack *stack, tToken tokPush)
{
stackElemPtr newElem = (stackElemPtr)malloc(sizeof(struct stackElem));
// Allocation error
if (!newElem)
{
// instert error call
error(99, "stack", "stack_push", "Allocation error");
printf("\nSTACK PUSH ERROR\n");
}
newElem->token.token_type = tokPush.token_type;
newElem->token.attr = tokPush.attr;
newElem->originalType = tokPush.token_type;
newElem->data = NULL;
newElem->expr = false;
// Push the token
// printf("STACK TOP\t%p\t%d\t%d\n", stack->topToken, stack->topToken->token.token_type);
newElem->nextTok = stack->topToken;
stack->topToken = newElem;
// printf("STACK PUSH\t%p\t%p\t%d\n", newElem, stack->topToken,newElem->token.token_type);
// printf("%d\n\n", tokPush.token_type);
// exit(0);
}
void stack_pop(tokenStack *stack)
{
// If element on the top is not empty flag
if (stack->topToken->token.token_type != T_EMPTY)
{
stackElemPtr tmp = stack->topToken;
stack->topToken = stack->topToken->nextTok;
tmp->data = NULL;
free(tmp);
}
else
{
//insert STACK_POP Error, trying to pop non-existent element
error(99, "stack", "stack_pop", "Trying to access non-existent element"); // or "trying to pop non-existent element"
}
}
int stack_count(tokenStack **stack)
{
int i = 0;
stackElemPtr tmp = (*stack)->topToken;
// While only empty flag token is left on the stack
while (tmp->token.token_type != T_EMPTY)
{
tmp = tmp->nextTok;
i++;
}
return i;
}
void stack_free(tokenStack **stack)
{
if (stack == NULL || *stack == NULL)
return;
if ((*stack)->topToken == NULL)
{
// MAYBE ERROR ? TRYING TO FREE UNALLOCATED
return;
}
stackElemPtr tmp;
while ((*stack)->topToken != NULL)
{
tmp = (*stack)->topToken;
(*stack)->topToken = (*stack)->topToken->nextTok;
tmp->data = NULL;
free(tmp);
}
free(*stack);
*stack = NULL;
}