Created
November 21, 2014 16:38
-
-
Save Arachnid/5d73b82938cf51f98429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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