-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe_stack.c
117 lines (99 loc) · 2.48 KB
/
frame_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
#include <stdlib.h>
#include <stdio.h>
#include "frame_stack.h"
#include "jvm.h"
extern frame_stack_t *jvm_stack;
/** \addtogroup Operacoes com as frames
* @{
*/
// --------------- Frame stack Stuff -----------------
/**
* @brief Pega um frame da pilha retirando ele do topo.
*
* @param stack para buscar o frame
* @return o frame da pilha
*/
frame_t* pop_frame_stack(frame_stack_t **stack) {
frame_stack_t *oldStackHead = (*stack);
frame_t *frame = (*stack)->frame;
if ((*stack) != NULL) {
*stack = (*stack)->previous;
(*stack)->next = NULL;
free(oldStackHead);
return frame;
} else {
return NULL;
}
}
/**
* @brief Coloca um frame na pilha
*
* @param pilha
* @param frame
*
*/
void push_frame_stack(frame_stack_t **stack, frame_t *frame) {
frame_stack_t *newStackHead = (frame_stack_t*) malloc(sizeof(frame_stack_t));
newStackHead->frame = frame;
newStackHead->next = NULL;
newStackHead->previous = (*stack);
if ((*stack) != NULL) {
(*stack)->next = newStackHead;
(*stack) = newStackHead;
} else {
(*stack) = newStackHead;
}
return;
}
/**
* @brief pega um frame da pilha mas nao retira da pilha
*
* @param pilha
*
*/
frame_t* peek_frame_stack(frame_stack_t *stack) {
if (stack != NULL) {
return stack->frame;
} else {
return NULL;
}
}
// --------------- Operand stack Stuff -----------------
/**
* @brief Pega um operando da pilha retirando ele do topo.
*
* @param stack para buscar o frame
* @return o operando da pilha
*/
any_type_t *pop_operand_stack(operand_stack_t *stack) {
if (stack->head == -1) {
printf("ERRO: Pop operand stack sem elementos\n");
exit(1);
}
any_type_t *operand = stack->operand[stack->head];
(stack->head)--;
DEBUG_PRINT("poped at %d: ", stack->head + 1);
print_any_type(operand);
return operand;
}
/**
* @brief Coloca um operando na pilha
*
* @param pilha
* @param oprerando
*
*/
void push_operand_stack(operand_stack_t *stack, any_type_t *operand) {
if (stack->head == stack->size - 1) {
printf("ERRO: Push operand stack overflow - max_stack = %d\n", stack->size);
u1 i = 0;
for(i = 0; i < stack->size; i++) {
print_any_type(pop_operand_stack(stack));
}
exit(1);
}
(stack->head)++;
stack->operand[stack->head] = operand;
DEBUG_PRINT("pushed at %d: ", stack->head);
print_any_type(operand);
}