This repository has been archived on 2025-11-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ovm/vm/main.c
Aryadev Chavali 5d800d4366 Moved inst module to lib
As it has no dependencies on vm specifically, and it's more necessary
for any vendors who wish to target the virtual machine, it makes more
sense for inst to be a lib module rather than a vm module.
2023-10-31 21:14:14 +00:00

66 lines
1.4 KiB
C

/* Copyright (C) 2023 Aryadev Chavali
* You may distribute and modify this code under the terms of the
* GPLv2 license. You should have received a copy of the GPLv2
* license with this file. If not, please write to:
* aryadev@aryadevchavali.com.
* Created: 2023-10-15
* Author: Aryadev Chavali
* Description: Entrypoint to program
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "./runtime.h"
#include <lib/inst.h>
int interpret_bytecode(const char *filepath)
{
FILE *fp = fopen(filepath, "rb");
size_t number = 0;
inst_t *instructions = insts_read_bytecode_file(fp, &number);
fclose(fp);
byte stack[256];
vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number);
err_t err = vm_execute_all(&vm);
int ret = 0;
if (err)
{
const char *error_str = err_as_cstr(err);
fprintf(stderr, "[ERROR]: %s\n", error_str);
vm_print_all(&vm, stderr);
ret = 255 - err;
}
free(instructions);
return ret;
}
void usage(const char *program_name, FILE *out)
{
fprintf(out,
"Usage: %s [OPTIONS] FILE\n"
"\t FILE: Bytecode file to execute\n"
"\tOptions:\n"
"\t\t To be developed...\n",
program_name);
}
int main(int argc, char *argv[])
{
if (argc == 1)
{
usage(argv[0], stderr);
return 1;
}
const char *filename = argv[1];
return interpret_bytecode(filename);
}