From 664006b1c42aae84a3c749d9b71c1047e0b8ffcf Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 2 Mar 2023 20:03:52 -0800 Subject: Initial commit. --- vmrun/CMakeLists.txt | 11 +++++++++ vmrun/src/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 vmrun/CMakeLists.txt create mode 100644 vmrun/src/main.c (limited to 'vmrun') diff --git a/vmrun/CMakeLists.txt b/vmrun/CMakeLists.txt new file mode 100644 index 0000000..1a81995 --- /dev/null +++ b/vmrun/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) + +project(vmrun) + +add_executable(vmrun + src/main.c) + +target_link_libraries(vmrun + vm) + +target_compile_options(vmrun PRIVATE -Wall -Wextra) diff --git a/vmrun/src/main.c b/vmrun/src/main.c new file mode 100644 index 0000000..0bdd16f --- /dev/null +++ b/vmrun/src/main.c @@ -0,0 +1,65 @@ +#include + +#include +#include +#include + +typedef enum CommandType { + ExitSession, + PrintStack, +} CommandType; + +typedef struct Command { + CommandType type; +} Command; + +static bool read_command(char line[], Command* pCmd) { + static const char* delim = " \n"; + + const char* cmd = strtok(line, delim); + if (strcmp(cmd, "exit") == 0) { + *pCmd = (Command){.type = ExitSession}; + return true; + } else if (strcmp(cmd, "print_stack") == 0) { + *pCmd = (Command){.type = PrintStack}; + return true; + } + return false; +} + +/// Runs a command. +/// +/// Returns true unless on ExitSession. +static bool run_command(Vm* vm, Command cmd) { + switch (cmd.type) { + case ExitSession: + return false; + case PrintStack: + vm_print_stack(vm); + break; + } + return true; +} + +int main() { + Vm* vm = vm_new(); + if (!vm) { + fprintf(stderr, "Failed to create VM\n"); + return 1; + } + + Command cmd; + bool should_continue = true; + char line[128]; + + while (should_continue && fgets(line, sizeof(line), stdin)) { + if (read_command(line, &cmd)) { + should_continue = run_command(vm, cmd); + } else { + fprintf(stderr, "Unknown command\n"); + } + } + + vm_del(&vm); + return 0; +} -- cgit v1.2.3