-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
57 lines (43 loc) · 922 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include "tokeniser.h"
#include "stack.h"
//#define DEBUG_EXPR_STACK
void push(struct stack *s, struct token *t) {
#ifdef DEBUG_EXPR_STACK
printf("push(%p) %d: ", s, s->sp);
printf("<%d: %s>", t->id, sym_from_id(t->id));
printf("\n");
#endif
s->t[s->sp++] = t;
if (s->sp >= MAX_EXPR_STACK) {
printf("expression stack overflow\n");
exit(1);
}
}
struct token *pop(struct stack *s) {
if(--s->sp < 0) {
printf("expression stack underflow\n");
exit(1);
}
#ifdef DEBUG_EXPR_STACK
do {
struct token *t;
t = s->t[s->sp];
printf("pop (%p) %d: ", s, s->sp);
printf("<%d: %s>", t->id, sym_from_id(t->id));
printf("\n");
} while (0);
#endif
return s->t[s->sp];
}
struct token *pop_nocheck(struct stack *s) {
if(--s->sp >= 0)
return s->t[s->sp];
return NULL;
}
struct token *peek(struct stack *s) {
if(s->sp > 0)
return s->t[s->sp-1];
return NULL;
}