-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
137 lines (118 loc) · 3.2 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "h/sporth.h"
#ifdef DEBUG_MODE
static void print_guts(sporth_stack *stack)
{
int i;
fprintf(stderr, "Dying stack contents:\n");
for(i = 0; i < stack->pos; i++) {
fprintf(stderr, "\t %d: ", i);
switch(stack->stack[i].type) {
case SPORTH_FLOAT:
fprintf(stderr, "%g\n", stack->stack[i].fval);
break;
case SPORTH_STRING:
fprintf(stderr, "%s\n", stack->stack[i].sval);
break;
default:
fprintf(stderr, "General type of %d\n", stack->stack[i].type);
break;
}
}
}
#endif
int sporth_stack_push_float(sporth_stack *stack, float val)
{
if(stack->error > 0) return SPORTH_NOTOK;
if(stack->pos < SPORTH_STACK_SIZE) {
stack->pos++;
stack->stack[stack->pos - 1].fval = val;
stack->stack[stack->pos - 1].type = SPORTH_FLOAT;
return SPORTH_OK;
} else {
fprintf(stderr, "Stack limit of %d reached, cannot push float value.\n", stack->pos);
stack->error++;
#ifdef DEBUG_MODE
print_guts(stack);
#endif
return SPORTH_NOTOK;
}
return SPORTH_OK;
}
int sporth_stack_push_string(sporth_stack *stack, char **str)
{
if(stack->error > 0) return SPORTH_NOTOK;
sporth_stack_val *pstack;
if(stack->pos < SPORTH_STACK_SIZE) {
stack->pos++;
pstack = &stack->stack[stack->pos - 1];
/* TODO: can const strings work here? */
pstack->sval = (char *)*str;
pstack->type = SPORTH_STRING;
return SPORTH_OK;
} else {
fprintf(stderr, "Stack limit of %d reached, cannot push float value.\n", stack->pos);
stack->error++;
#ifdef DEBUG_MODE
print_guts(stack);
#endif
return SPORTH_NOTOK;
}
return SPORTH_OK;
}
float sporth_stack_pop_float(sporth_stack *stack)
{
if(stack->error > 0) return 0;
sporth_stack_val *pstack;
if(stack->pos == 0) {
fprintf(stderr, "Stack is empty.\n");
stack->error++;
return SPORTH_NOTOK;
}
pstack = &stack->stack[stack->pos - 1];
if(pstack->type != SPORTH_FLOAT) {
fprintf(stderr, "Value is not a float.\n");
stack->error++;
return SPORTH_NOTOK;
}
stack->pos--;
return pstack->fval;
}
/*TODO use this function inside sporth stack functions*/
sporth_stack_val * sporth_stack_get_last(sporth_stack *stack)
{
return &stack->stack[stack->pos - 1];
}
const char * sporth_stack_pop_string(sporth_stack *stack)
{
const char *str;
sporth_stack_val *pstack;
if(stack->error > 0) return NULL;
if(stack->pos == 0) {
fprintf(stderr, "Stack is empty.\n");
stack->error++;
return NULL;
}
pstack = &stack->stack[stack->pos - 1];
if(pstack->type != SPORTH_STRING) {
fprintf(stderr, "Value is not a string.\n");
stack->error++;
return NULL;
}
str = pstack->sval;
stack->pos--;
return str;
}
int sporth_stack_init(sporth_stack *stack)
{
stack->pos = 0;
stack->error = 0;
return SPORTH_OK;
}
int sporth_stack_pos(sporth_stack *stack)
{
return stack->pos;
}