Browse Source

finish fvm

Mistivia 4 months ago
parent
commit
fb4dca6c42
2 changed files with 27 additions and 2 deletions
  1. 1 0
      src/fvm.c
  2. 26 2
      src/main.c

+ 1 - 0
src/fvm.c

@@ -424,6 +424,7 @@ int64_t fvm_execute(struct fvm *vm) {
       return vm->rv;
     default:
       fprintf(stderr, "unknown opcode.\n");
+      exit(-1);
       break; 
     }
   }

+ 26 - 2
src/main.c

@@ -1,5 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+
 #include "fvm.h"
 
-int main() {
-    return 0;
+int main(int argc, char **argv) {
+  struct fvm vm;
+  if (argc < 2) {
+    fprintf(stderr, "Usage: fvm <program-file>\n");
+    return -1;
+  }
+  FILE* fp = fopen(argv[1], "rb");
+  if (fp == NULL) {
+    fprintf(stderr, "Failed to open file.\n");
+    return -1;
+  }
+  fseek(fp, 0, SEEK_END);
+  int64_t flen = ftell(fp);
+  char *code = malloc(flen);
+  fseek(fp, 0, SEEK_SET);
+  int n = fread(code, 1, flen, fp);
+  if (n != flen) {
+    fprintf(stderr, "Failed to read from file.\n");
+    exit(-1);
+  }
+  fvm_init(&vm, code, 2*1024*1024);
+  fvm_execute(&vm);
+  return vm.rv;
 }