forked from xjuric29/ifj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokstack.c
123 lines (99 loc) · 2.2 KB
/
tokstack.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
/**
* @file stacktok.c
* @author Jiri Furda (xfurda00)
* @brief Source file for token stack
* @todo
*/
//#define TOKSTACKDEBUG
#include "tokstack.h"
int tokStack_Init(tokStack_t *stack)
{
if(stack == NULL) // If the stack is not allocated
{
tokStack_Error("tokStackInit: Stack is not allocated");
return FAIL;
}
stack->top = NO_STACK; // Initialize top of the stack
return SUCCESS;
}
int tokStack_Push(tokStack_t *stack, tokenType_t tokenType)
{
// Check if stack is full
if(tokStack_Full(stack) == TRUE)
{
tokStack_Error("tokStackInit: Stack is not allocated");
return FAIL;
}
// Increase top of the stack
(stack->top)++;
// Push token to the stack
stack->tokArr[stack->top] = tokenType;
// Debug
#ifdef TOKSTACKDEBUG
tokStack_Info(stack);
#endif
// Return success
return SUCCESS;
}
tokenType_t tokStack_Pop(tokStack_t *stack)
{
// Get pointer to token on top of the stack
tokenType_t topType = tokStack_Top(stack);
if(topType == TOK_FAIL)
return TOK_FAIL;
// Decrease top of the stack
(stack->top)--;
// Debug
#ifdef TOKSTACKDEBUG
tokStack_Info(stack);
#endif
// Return pointer to the token on top of the stack
return topType;
}
tokenType_t tokStack_Top(tokStack_t *stack)
{
// Check if stack is empty
if(tokStack_Empty(stack) == TRUE)
{
tokStack_Error("tokStackTop: Tring to pop/top empty stack");
return TOK_FAIL; // @todo checking whne called this function!!!!!!
}
// Return pointer to token on top of the stack
return stack->tokArr[stack->top];
}
int tokStack_Empty(tokStack_t *stack)
{
if(stack->top < 0)
return TRUE;
else
return FALSE;
}
int tokStack_Full(tokStack_t *stack)
{
if(stack->top >= TOKSTACK_MAX)
return TRUE;
else
return FALSE;
}
void tokStack_Error(char* msg)
{
fprintf(stderr, "[ERROR] %s\n", msg);
}
#ifdef TOKSTACKDEBUG
void tokStack_Info(tokStack_t *stack)
{
printf("[DBG] tokStack=");
for(int i = 0; i <= stack->top; i++)
{
switch(stack->tokArr[i])
{
case TOK_integer: printf("int "); break;
case TOK_decimal: printf("dec "); break;
case TOK_string: printf("str "); break;
case TOK_BOOLEAN: printf("bool "); break;
default: printf("WTF "); break;
}
}
printf("\n");
}
#endif