Finished cleaning up the code

This commit is contained in:
nilsplk 2023-10-30 12:51:42 +01:00
parent dfedf2e717
commit 1363db7c11
2 changed files with 66 additions and 70 deletions

30
njvm.c
View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "instruktion.c" #include "instruktion.c"
#include "code.c" #include "code.c"
#include "stack.c" #include "stack.c"
@ -11,16 +10,13 @@
#define DEBUG #define DEBUG
// Stack // Stack
struct stack stack; struct stack stack;
#define SIZE 1000 #define SIZE 1000
// ProgrammSpeicher // Program
struct program program; struct program program;
unsigned int* programmSpeicher;
void version(void) { void version(void) {
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
} }
@ -29,19 +25,18 @@ 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"); printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
} }
void execute(void) { void execute(struct program program) {
int i = 0; int i;
int intInput; int intInput;
int temp; unsigned int temp;
char charInput; char charInput;
while (1) { for (i = 0; i < *program.size; ++i) {
switch (program.program[i] >> 24) {
switch (programmSpeicher[i] >> 24) {
case HALT: case HALT:
goto end; goto end;
break; break;
case PUSHC: case PUSHC:
push(stack,IMMEDIATE(programmSpeicher[i])); push(stack,IMMEDIATE(program.program[i]));
break; break;
case ADD: case ADD:
push(stack,pop(stack)+pop(stack)); push(stack,pop(stack)+pop(stack));
@ -76,10 +71,9 @@ void execute(void) {
printf("%c",pop(stack)); printf("%c",pop(stack));
break; break;
} }
i++;
} }
end: end:
return; return;
} }
#ifdef DEBUG #ifdef DEBUG
@ -101,8 +95,8 @@ int main(int argc, char *argv[]) {
stack.stack = s; stack.stack = s;
// Initialize ProgrammSpeicher // Initialize ProgrammSpeicher
int psize = 1; int psize = 1000;
unsigned int p[1]; unsigned int p[1000];
program.size = &psize; program.size = &psize;
program.program = p; program.program = p;
@ -132,7 +126,7 @@ int main(int argc, char *argv[]) {
// Started // Started
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
printProgram(program); printProgram(program);
execute(); execute(program);
// Stopped // Stopped
printf("Ninja Virtual Machine stopped\n"); printf("Ninja Virtual Machine stopped\n");
return 0; return 0;

106
program.c
View File

@ -3,65 +3,67 @@
*/ */
#ifndef PROGRAM #ifndef PROGRAM
#define PROGRAM #define PROGRAM
#include <string.h> #include <string.h>
#include "instruktion.c" #include "instruktion.c"
#include <stdio.h> #include <stdio.h>
struct program { struct program {
int* size; int *size;
unsigned int* program; unsigned int *program;
}; };
void copyToProgram(unsigned int codeToCopy[], int size, struct program program){ void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
program.program = codeToCopy; for (int i = 0; i < size; ++i) {
*program.size = size; program.program[i] = codeToCopy[i];
}
void printProgram(struct program program){
printf("%i\n",*program.size);
char c[10];
for (int i = 0; i > *program.size; i++) {
switch (program.program[i] >> 24) {
case PUSHC:
strcpy(c,"pushc");
break;
case ADD:
strcpy(c,"add");
break;
case SUB:
strcpy(c,"sub");
break;
case MUL:
strcpy(c,"mul");
break;
case DIV:
strcpy(c,"div");
break;
case MOD:
strcpy(c,"mod");
break;
case RDINT:
strcpy(c,"rdint");
break;
case WRINT:
strcpy(c,"wrint");
break;
case RDCHR:
strcpy(c,"rdchr");
break;
case WRCHR:
strcpy(c,"wrchr");
break;
case HALT:
strcpy(c,"halt");
break;
default:
strcpy(c,"ERROR");
break;
} }
IMMEDIATE(program.program[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(program.program[i])) : printf("%03i:\t%s\n",i,c); *program.size = size;
printf("afslaf");
}
} }
void printProgram(struct program program) {
char c[10];
for (int i = 0; i < *program.size; i++) {
switch (program.program[i] >> 24) {
case PUSHC:
strcpy(c, "pushc");
break;
case ADD:
strcpy(c, "add");
break;
case SUB:
strcpy(c, "sub");
break;
case MUL:
strcpy(c, "mul");
break;
case DIV:
strcpy(c, "div");
break;
case MOD:
strcpy(c, "mod");
break;
case RDINT:
strcpy(c, "rdint");
break;
case WRINT:
strcpy(c, "wrint");
break;
case RDCHR:
strcpy(c, "rdchr");
break;
case WRCHR:
strcpy(c, "wrchr");
break;
case HALT:
strcpy(c, "halt");
break;
default:
strcpy(c, "ERROR");
break;
}
IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, IMMEDIATE(program.program[i])) : printf(
"%03i:\t%s\n", i, c);
}
}
#endif /* ifndef PROGRAMM */ #endif /* ifndef PROGRAMM */