aboutsummaryrefslogtreecommitdiff
path: root/vmrun
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-03-02 20:03:52 -0800
committer3gg <3gg@shellblade.net>2023-03-02 20:03:52 -0800
commit664006b1c42aae84a3c749d9b71c1047e0b8ffcf (patch)
treee08f8af944b132742b3bb1d240d8954328e667e5 /vmrun
Initial commit.HEADmain
Diffstat (limited to 'vmrun')
-rw-r--r--vmrun/CMakeLists.txt11
-rw-r--r--vmrun/src/main.c65
2 files changed, 76 insertions, 0 deletions
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 @@
1cmake_minimum_required(VERSION 3.0)
2
3project(vmrun)
4
5add_executable(vmrun
6 src/main.c)
7
8target_link_libraries(vmrun
9 vm)
10
11target_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 @@
1#include <vm.h>
2
3#include <stdbool.h>
4#include <stdio.h>
5#include <string.h>
6
7typedef enum CommandType {
8 ExitSession,
9 PrintStack,
10} CommandType;
11
12typedef struct Command {
13 CommandType type;
14} Command;
15
16static bool read_command(char line[], Command* pCmd) {
17 static const char* delim = " \n";
18
19 const char* cmd = strtok(line, delim);
20 if (strcmp(cmd, "exit") == 0) {
21 *pCmd = (Command){.type = ExitSession};
22 return true;
23 } else if (strcmp(cmd, "print_stack") == 0) {
24 *pCmd = (Command){.type = PrintStack};
25 return true;
26 }
27 return false;
28}
29
30/// Runs a command.
31///
32/// Returns true unless on ExitSession.
33static bool run_command(Vm* vm, Command cmd) {
34 switch (cmd.type) {
35 case ExitSession:
36 return false;
37 case PrintStack:
38 vm_print_stack(vm);
39 break;
40 }
41 return true;
42}
43
44int main() {
45 Vm* vm = vm_new();
46 if (!vm) {
47 fprintf(stderr, "Failed to create VM\n");
48 return 1;
49 }
50
51 Command cmd;
52 bool should_continue = true;
53 char line[128];
54
55 while (should_continue && fgets(line, sizeof(line), stdin)) {
56 if (read_command(line, &cmd)) {
57 should_continue = run_command(vm, cmd);
58 } else {
59 fprintf(stderr, "Unknown command\n");
60 }
61 }
62
63 vm_del(&vm);
64 return 0;
65}