diff options
Diffstat (limited to 'vmrun')
-rw-r--r-- | vmrun/CMakeLists.txt | 11 | ||||
-rw-r--r-- | vmrun/src/main.c | 65 |
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 @@ | |||
1 | cmake_minimum_required(VERSION 3.0) | ||
2 | |||
3 | project(vmrun) | ||
4 | |||
5 | add_executable(vmrun | ||
6 | src/main.c) | ||
7 | |||
8 | target_link_libraries(vmrun | ||
9 | vm) | ||
10 | |||
11 | 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 @@ | |||
1 | #include <vm.h> | ||
2 | |||
3 | #include <stdbool.h> | ||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | typedef enum CommandType { | ||
8 | ExitSession, | ||
9 | PrintStack, | ||
10 | } CommandType; | ||
11 | |||
12 | typedef struct Command { | ||
13 | CommandType type; | ||
14 | } Command; | ||
15 | |||
16 | static 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. | ||
33 | static 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 | |||
44 | int 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 | } | ||