Skip to content

Commit

Permalink
Fix #5: proper swap operation of queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dittos committed May 26, 2014
1 parent 63446f3 commit 6766ed4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions aheui.c
Expand Up @@ -11,6 +11,7 @@
#define FLAG_FETCH_B (1<<1)
#define FLAG_FETCH_AB FLAG_FETCH_B
#define FLAG_FETCH_REMOVE (1<<5)
#define FLAG_FETCH_SWAP (1<<6)

#define FLAG_DIR_SET (1<<2)
#define FLAG_DIR_FLIPX (1<<3)
Expand Down Expand Up @@ -176,10 +177,14 @@ void input(FILE *fp) {
}
cell->op = op;
switch (cell->op) {
case OP_DIV: case OP_ADD: case OP_MUL: case OP_MOD: case OP_CMP: case OP_SUB: case OP_SWAP:
case OP_DIV: case OP_ADD: case OP_MUL: case OP_MOD: case OP_CMP: case OP_SUB:
cell->flags |= FLAG_FETCH_AB;
cell->flags |= FLAG_FETCH_REMOVE;
break;
case OP_SWAP:
cell->flags |= FLAG_FETCH_AB;
cell->flags |= FLAG_FETCH_SWAP;
break;
case OP_PRINT_NUM: case OP_PRINT_CHAR: case OP_POP: case OP_MOVE: case OP_BRANCH:
cell->flags |= FLAG_FETCH_A;
cell->flags |= FLAG_FETCH_REMOVE;
Expand Down Expand Up @@ -229,13 +234,21 @@ int execute() {
if (cell->flags & FLAG_FETCH_B) b = *(current_stack_top - 1);
if (cell->flags & FLAG_FETCH_REMOVE)
current_stack_top -= cell->flags & MASK_REQ_ELEMS;
if (cell->flags & FLAG_FETCH_SWAP) {
*current_stack_top = b;
*(current_stack_top - 1) = a;
}
} else {
if (queuesize() < (cell->flags & MASK_REQ_ELEMS))
goto underflow;
a = *(queue_front + 1);
if (cell->flags & FLAG_FETCH_B) b = *(queue_front + 2);
if (cell->flags & FLAG_FETCH_REMOVE)
queue_front += cell->flags & MASK_REQ_ELEMS;
if (cell->flags & FLAG_FETCH_SWAP) {
*(queue_front + 1) = b;
*(queue_front + 2) = a;
}
}
}
// execute
Expand Down Expand Up @@ -272,7 +285,7 @@ int execute() {
case OP_CMP: push((b>=a) ? 1 : 0); break;
case OP_BRANCH: if (a == 0) { dir.dx = -dir.dx; dir.dy = -dir.dy; } break;
case OP_SUB: push(b-a); break;
case OP_SWAP: push(a); push(b); break;
case OP_SWAP: break;
case OP_EXIT: return step; break;
}
goto next;
Expand Down

0 comments on commit 6766ed4

Please sign in to comment.