-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.c
58 lines (43 loc) · 1.04 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
#include "stack.h"
List* stack;
/* stack operations */
void save(Obj reg) {
if (DEBUG) printf("%s\n", "save!");
List* temp = stack;
stack = malloc(sizeof(List)); // &stack?
stack->car = reg;
stack->cdr = temp;
save_count++;
curr_stack_depth++;
max_stack_depth =
curr_stack_depth > max_stack_depth ? curr_stack_depth : max_stack_depth;
return;
}
/* use restore macro instead of _restore function to avoid having to
give the pointer address of the argument (see stack.h) */
/* #define restore(reg) _restore(®); */
void _restore(Obj* reg) {
if (DEBUG) printf("%s\n", "restore!");
*reg = stack->car;
List* temp = stack;
stack = stack->cdr;
free(temp);
curr_stack_depth--;
return;
}
/* stack management */
#define empty_stack NULL
void clear_stack(void) {
List* temp = stack;
while (stack) {
stack = stack->cdr;
free(temp);
temp = stack;
}
return;
}
void initialize_stack(void) {
clear_stack();
stack = empty_stack;
return;
}