commit 60d748d3e15ba89806c77207e69e039ae03facc3
parent 9765eb6f6a720c7c1c59415f4a5f580bc7ca5106
Author: William Casarin <jb55@jb55.com>
Date: Wed, 27 Dec 2017 20:36:48 -0800
script: implement roll
closes #26
Diffstat:
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/script.c b/script.c
@@ -436,7 +436,7 @@ script_eval(const u8 *script, size_t script_size, struct stack *stack,
SCRIPTERR("INVALID_STACK_OPERATION");
struct val val = stack_top_val(stack, (-(n->val))-1);
if (opcode == OP_ROLL)
- assert(!"Finish OP_ROLL");
+ stack_erase(stack, -(n->val) - 1);
/* stack.erase(stack.end()-n-1); */
stack_push_val(stack, val);
}
diff --git a/stack.c b/stack.c
@@ -82,6 +82,14 @@ stack_push(struct stack *stack, void *val) {
*stack->top++ = val;
}
+void
+stack_erase(struct stack *stack, int pos) {
+ if (stack_size(stack) == 0) return;
+ size_t len = stack->top - (stack->top + pos);
+ memcpy(stack->top + pos, stack->top + pos + 1, sizeof(stack->top) * len);
+ stack->top--;
+}
+
void *
stack_pop(struct stack *stack) {
assert(stack);
diff --git a/stack.h b/stack.h
@@ -25,7 +25,7 @@ int stack_init_size(struct stack *, int size);
void stack_free(struct stack *);
void stack_clear(struct stack *);
void* stack_pop(struct stack *);
-void* stack_erase(struct stack *, int ind);
+void stack_erase(struct stack *, int ind);
void stack_push(struct stack *, void *val);
static inline void *