Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created November 21, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arachnid/5d73b82938cf51f98429 to your computer and use it in GitHub Desktop.
Save Arachnid/5d73b82938cf51f98429 to your computer and use it in GitHub Desktop.
#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