Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created November 21, 2014 16:38
Embed
What would you like to do?
#define NEXT goto *opcodes[program[pc++]]
int interpret(char *stack, int sp, char *program) {
int pc = 0;
static const void *opcodes[] = {&&push, &&pop, &&add, &&sub, &&ret};
NEXT;
push:
stack[sp++] = program[pc++];
NEXT;
pop:
sp--;
NEXT;
add:
sp--;
stack[sp - 1] = stack[sp - 1] + stack[sp];
NEXT;
sub:
sp--;
stack[sp - 1] = stack[sp - 1] - stack[sp];
NEXT;
ret:
return stack[sp - 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment