Lines Matching refs:stack
1 /* $NetBSD: stack.c,v 1.2 2017/04/10 16:37:48 christos Exp $ */
2 /* $OpenBSD: stack.c,v 1.14 2016/03/27 15:55:13 otto Exp $ */
20 __RCSID("$NetBSD: stack.c,v 1.2 2017/04/10 16:37:48 christos Exp $");
28 static __inline bool stack_empty(const struct stack *);
29 static void stack_grow(struct stack *);
38 stack_init(struct stack *stack)
40 stack->size = 0;
41 stack->sp = -1;
42 stack->stack = NULL;
46 stack_empty(const struct stack *stack)
48 bool empty = stack->sp == -1;
50 warnx("stack empty");
97 stack_size(const struct stack *stack)
99 return (size_t)(stack->sp + 1);
103 stack_dup(struct stack *stack)
108 value = stack_tos(stack);
110 warnx("stack empty");
113 stack_push(stack, stack_dup_value(value, ©));
117 stack_swap(struct stack *stack)
121 if (stack->sp < 1) {
122 warnx("stack empty");
125 copy = stack->stack[stack->sp];
126 stack->stack[stack->sp] = stack->stack[stack->sp-1];
127 stack->stack[stack->sp-1] = copy;
131 stack_grow(struct stack *stack)
135 if ((size_t)++stack->sp == stack->size) {
136 new_size = stack->size * 2 + 1;
137 stack->stack = breallocarray(stack->stack,
138 new_size, sizeof(*stack->stack));
139 stack->size = new_size;
144 stack_pushnumber(struct stack *stack, struct number *b)
146 stack_grow(stack);
147 stack->stack[stack->sp].type = BCODE_NUMBER;
148 stack->stack[stack->sp].u.num = b;
149 stack->stack[stack->sp].array = NULL;
153 stack_pushstring(struct stack *stack, char *string)
155 stack_grow(stack);
156 stack->stack[stack->sp].type = BCODE_STRING;
157 stack->stack[stack->sp].u.string = string;
158 stack->stack[stack->sp].array = NULL;
162 stack_push(struct stack *stack, struct value *v)
166 stack_grow(stack);
167 stack->stack[stack->sp].type = BCODE_NONE;
170 stack_pushnumber(stack, v->u.num);
173 stack_pushstring(stack, v->u.string);
176 stack->stack[stack->sp].array = v->array == NULL ?
181 stack_tos(const struct stack *stack)
183 if (stack->sp == -1)
185 return &stack->stack[stack->sp];
189 stack_set_tos(struct stack *stack, struct value *v)
191 if (stack->sp == -1)
192 stack_push(stack, v);
194 stack_free_value(&stack->stack[stack->sp]);
195 stack->stack[stack->sp] = *v;
196 stack->stack[stack->sp].array = v->array == NULL ?
202 stack_pop(struct stack *stack)
204 if (stack_empty(stack))
206 return &stack->stack[stack->sp--];
210 stack_popnumber(struct stack *stack)
212 if (stack_empty(stack))
214 array_free(stack->stack[stack->sp].array);
215 stack->stack[stack->sp].array = NULL;
216 if (stack->stack[stack->sp].type != BCODE_NUMBER) {
220 return stack->stack[stack->sp--].u.num;
224 stack_popstring(struct stack *stack)
226 if (stack_empty(stack))
228 array_free(stack->stack[stack->sp].array);
229 stack->stack[stack->sp].array = NULL;
230 if (stack->stack[stack->sp].type != BCODE_STRING) {
234 return stack->stack[stack->sp--].u.string;
238 stack_clear(struct stack *stack)
240 while (stack->sp >= 0)
241 stack_free_value(&stack->stack[stack->sp--]);
242 free(stack->stack);
243 stack_init(stack);
247 stack_print(FILE *f, const struct stack *stack, const char *prefix, u_int base)
251 for (i = stack->sp; i >= 0; i--) {
252 print_value(f, &stack->stack[i], prefix, base);
328 frame_assign(struct stack *stack, size_t index, const struct value *v)
333 if (stack->sp == -1) {
336 stack_push(stack, &n);
339 a = stack->stack[stack->sp].array;
341 a = stack->stack[stack->sp].array = array_new();
346 frame_retrieve(const struct stack *stack, size_t index)
350 if (stack->sp == -1)
352 a = stack->stack[stack->sp].array;
354 a = stack->stack[stack->sp].array = array_new();