#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; }