diff options
Diffstat (limited to 'vmrun/src')
-rw-r--r-- | vmrun/src/main.c | 65 |
1 files changed, 65 insertions, 0 deletions
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 | } | ||