#include #include #include //Comment to disable debug #define DEBUG #define IMMEDIATE(x) ((x) & 0x00FFFFFF) #define HALT 0 #define PUSHC 1 #define ADD 2 #define SUB 3 #define MUL 4 #define DIV 5 #define MOD 6 #define RDINT 7 #define WRINT 8 #define RDCHR 9 #define WRCHR 10 unsigned int programmSpeicher[1000]; // Stack #define maxValues 1000 unsigned int stack[maxValues]; unsigned int current = 0; void stackPush(unsigned int value) { if (current > (maxValues-1)) { fprintf(stderr, "stack overflow\n"); exit(EXIT_FAILURE); } stack[current] = value; current++; } unsigned int stackPop(void) { if (current < 1) { fprintf(stderr, "stack underflow\n"); exit(EXIT_FAILURE); } return stack[--current]; } void version(void) { printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); } void help(void) { printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n"); } void useption(int argc, char *argv[]) { if (argc > 1) { if (strcmp(argv[1], "--version") == 0) { version(); } else if (strcmp(argv[1], "--help") == 0) { help(); } else { printf("unknown command line argument '%s', try './njvm --help'", argv[1]); } } } void execute(void) { int i = 0; while (programmSpeicher[i] != 0) { switch (programmSpeicher[i] >> 24) { case HALT: break; case PUSHC: break; case ADD: stackPush(stackPop()+stackPop()); break; case SUB: stackPush(stackPop()-stackPop()); break; case MUL: stackPush(stackPop()*stackPop()); break; case DIV: stackPush(stackPop()/stackPop()); break; case MOD: stackPush(stackPop()%stackPop()); break; case RDINT: break; case WRINT: break; case RDCHR: break; case WRCHR: break; } i++; } } #ifdef DEBUG void tests(){ printf("test\n"); } #endif /* ifdef DEBUG */ int main(int argc, char *argv[]) { #ifdef DEBUG tests(); #endif /* ifdef DEBUG */ if (argc > 1) useption(argc, argv); else { // Started printf("Ninja Virtual Machine started\n"); // Stopped printf("Ninja Virtual Machine stopped\n"); return 0; } }