main.c (4301B)
1 #include "parser.h" 2 #include "interpreter.h" 3 4 void print_help() { 5 printf("command line options:\n"); 6 printf("\t'--quiet' : disables printing inbetween operations\n"); 7 printf("\t'--step' : steps through the program [press enter]\n"); 8 printf("\t\n"); 9 printf("\t'--verbose-parser' : prints opcodes and parameters of the awatalk\n"); 10 printf("\t'--verbose-interpreter' : prints current instruction and stack while executing\n"); 11 printf("\t'--verbose' : enables both of the above\n"); 12 printf("\t'--allow-syscall' : enables syscalls\n"); 13 printf("\t\n"); 14 printf("\t'--help' : prints this message\n"); 15 printf("\t\n"); 16 printf("\t'-string' : the next provided argument will be awatalk to be parsed\n"); 17 printf("\t'-file' : the next provided argument will be awatalk file to be parsed\n"); 18 printf("example usage:\n"); 19 printf("\tawaparser --verbose -string 'awa awa awawa awawa awa awa awa awa awa awawa awa awa awawa awawa awa awa awa awa awa awawawa awa awawa awawa awa awa awa awa awa awawa awa awawa awa awawa awa awa awawawa awa awa awa awawa'\n"); 20 printf("\tawaparser --quiet -file hello_world.awa\n"); 21 printf("\tawaparser --verbose-parser -file hello_world.awa\n"); 22 } 23 24 int main(int argc, char** argv) 25 { 26 bool verbose_parser = false; 27 bool verbose_interpreter = false; 28 bool quiet = false; 29 bool step_through = false; 30 bool allow_syscall = false; 31 char* file = NULL; 32 char* string = NULL; 33 bool file_next = false; 34 bool string_next = false; 35 for (int i = 1; i < argc; i++) { 36 if (file_next) { 37 file_next = false; 38 file = argv[i]; 39 if (*argv[i] == '-') 40 printf("WARNING: -file argument might not be a propper filepath '%s'\n", argv[i]); 41 continue; 42 } 43 if (string_next) { 44 string_next = false; 45 string = argv[i]; 46 if (*argv[i] == '-') 47 printf("WARNING: -string argument might not be a propper awatalk '%s'\n", argv[i]); 48 continue; 49 } 50 if (strcmp(argv[i], "--help") == 0) { 51 print_help(); 52 return 0; 53 } 54 if (strcmp(argv[i], "--quiet") == 0) 55 quiet = true; 56 else if (strcmp(argv[i], "--verbose") == 0) 57 verbose_parser = verbose_interpreter = true; 58 else if (strcmp(argv[i], "--verbose-parser") == 0) 59 verbose_parser = true; 60 else if (strcmp(argv[i], "--verbose-interpreter") == 0) 61 verbose_interpreter = true; 62 else if (strcmp(argv[i], "--step") == 0) 63 step_through = true; 64 else if (strcmp(argv[i], "--allow-syscall") == 0) 65 allow_syscall = true; 66 else if (strcmp(argv[i], "-file") == 0) 67 file_next = true; 68 else if (strcmp(argv[i], "-string") == 0) 69 string_next = true; 70 else 71 printf("WARNING: unknown command line argument '%s'\n", argv[i]); 72 } 73 if (string_next) { 74 puts("ERROR: '-string' was provided but the next argument didn't exist"); 75 return 2; 76 } 77 if (file_next) { 78 puts("ERROR: '-file' was provided but the next argument didn't exist"); 79 return 2; 80 } 81 if (file && string) { 82 puts("ERROR: '-file' and '-string' were provided at the same time"); 83 return 2; 84 } 85 if (!file && !string) { 86 puts("ERROR: neither '-file' nor '-string' were provided"); 87 print_help(); 88 return 0; 89 } 90 awa_program_t prog = {0}; 91 if (file) { 92 prog = awatalk_file_to_program(file, verbose_parser); 93 } 94 if (string) { 95 int l = strlen(string); 96 char* prog_source = malloc(l+1); 97 memcpy(prog_source, string, l+1); 98 prog = awatalk_to_program(prog_source, verbose_parser); 99 } 100 if (prog.failed_parse) { 101 puts("error during parsing, exiting."); 102 awa_program_free(&prog); 103 return 1; 104 } 105 if (!quiet) puts("parsed file succesfully"); 106 if (!quiet) puts("running program:\n-"); 107 awa_program_run(prog, verbose_interpreter, step_through, allow_syscall); 108 if (!quiet) puts("\n-\nprogram done!"); 109 awa_program_free(&prog); 110 }