You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "vm/vm.h"
  2. #include "vm/print.h"
  3. #include "parse/parse.h"
  4. #include "parse/lex.h"
  5. #include "io.h"
  6. #include "bitset.h"
  7. #include "bytecode.h"
  8. #include "loader.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #ifdef __unix__
  12. #define USE_READLINE
  13. #include <unistd.h>
  14. #include <readline/readline.h>
  15. #include <readline/history.h>
  16. #endif
  17. static int do_print_bytecode = 0;
  18. static int do_print_tokens = 0;
  19. static int do_step = 0;
  20. static int do_serialize_bytecode = 0;
  21. static int do_repl = 0;
  22. static char *input_filename = "-";
  23. static int parse_text(FILE *inf, struct l2_io_writer *w) {
  24. // Init lexer with its input reader
  25. struct l2_io_file_reader r;
  26. r.r.read = l2_io_file_read;
  27. r.f = inf;
  28. struct l2_lexer lexer;
  29. l2_lexer_init(&lexer, &r.r);
  30. if (do_print_tokens) {
  31. lexer.do_log_tokens = 1;
  32. }
  33. // Init gen with its output writer
  34. struct l2_generator gen;
  35. l2_gen_init(&gen, w);
  36. struct l2_parse_error err;
  37. if (l2_parse_program(&lexer, &gen, &err) < 0) {
  38. fprintf(stderr, "Parse error: %s:%i:%i: %s\n",
  39. input_filename, err.line, err.ch, err.message);
  40. l2_parse_error_free(&err);
  41. l2_gen_free(&gen);
  42. fclose(inf);
  43. return -1;
  44. }
  45. l2_gen_free(&gen);
  46. return 0;
  47. }
  48. static void step_through(struct l2_vm *vm) {
  49. printf("=====\n\nInitial state:\n");
  50. l2_vm_print_state(vm);
  51. char buf[16];
  52. while (!vm->halted) {
  53. size_t iptr = vm->iptr;
  54. printf("\n======\n\n(%d) Will run instr: ", vm->iptr);
  55. l2_vm_print_op(vm->ops, vm->opcount, &iptr);
  56. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  57. break;
  58. }
  59. l2_vm_step(vm);
  60. l2_vm_print_state(vm);
  61. }
  62. }
  63. static void repl() {
  64. struct l2_io_mem_writer w = {
  65. .w.write = l2_io_mem_write,
  66. };
  67. struct l2_io_mem_reader r = {
  68. .r.read = l2_io_mem_read,
  69. };
  70. struct l2_lexer lexer;
  71. struct l2_generator gen;
  72. l2_gen_init(&gen, &w.w);
  73. struct l2_vm vm;
  74. l2_vm_init(&vm, NULL, 0);
  75. while (1) {
  76. char line[4096];
  77. #ifdef USE_READLINE
  78. char *rline = readline("> ");
  79. if (rline == NULL) return;
  80. if (rline[0] == '\0') continue;
  81. snprintf(line, sizeof(line), "print (%s)", rline);
  82. free(rline);
  83. #else
  84. char rline[4096];
  85. if (fgets(rline, sizeof(rline), stdin) == NULL) return;
  86. if (rline[0] == '\n' && rline[1] == '\0') continue;
  87. snprintf(line, sizeof(line), "print (%s)", rline);
  88. #endif
  89. r.idx = 0;
  90. r.len = strlen(line);
  91. r.mem = line;
  92. l2_lexer_init(&lexer, &r.r);
  93. size_t prev_len = w.len;
  94. struct l2_parse_error err;
  95. if (l2_parse_program(&lexer, &gen, &err) < 0) {
  96. fprintf(stderr, "Parse error: %s\n -- %s\n", err.message, line);
  97. l2_parse_error_free(&err);
  98. w.len = prev_len;
  99. } else if (w.len > 0) {
  100. vm.ops = w.mem;
  101. vm.opcount = w.len / sizeof(l2_word);
  102. while (vm.iptr < vm.opcount) {
  103. l2_vm_step(&vm);
  104. }
  105. l2_vm_gc(&vm);
  106. }
  107. }
  108. }
  109. static void usage(const char *argv0) {
  110. printf("Usage: %s [options] [input|-]\n", argv0);
  111. printf("\n");
  112. printf("Options:\n");
  113. printf(" --help: Print this help text\n");
  114. printf(" --bytecode: Print the generated bytecode, don't execute\n");
  115. printf(" --tokens: Print the tokens as the program is parsed, don't execute\n");
  116. printf(" --step: Step through the program\n");
  117. printf(" --repl: Start a repl\n");
  118. printf(" --output,-o <out>: Write bytecode to file\n");
  119. }
  120. int main(int argc, char **argv) {
  121. int was_inf_set = 0;
  122. FILE *inf = stdin;
  123. FILE *outbc = NULL;
  124. #ifdef __unix__
  125. do_repl = isatty(0);
  126. #endif
  127. int dashes = 0;
  128. for (int i = 1; i < argc; ++i) {
  129. if (!dashes && strcmp(argv[i], "--help") == 0) {
  130. usage(argv[0]);
  131. return 0;
  132. } else if (!dashes && strcmp(argv[i], "--bytecode") == 0) {
  133. do_print_bytecode = 1;
  134. } else if (!dashes && strcmp(argv[i], "--tokens") == 0) {
  135. do_print_tokens = 1;
  136. } else if (!dashes && strcmp(argv[i], "--step") == 0) {
  137. do_step = 1;
  138. } else if (!dashes && strcmp(argv[i], "--repl") == 0) {
  139. do_repl = 1;
  140. } else if (!dashes && (
  141. strcmp(argv[i], "--output") == 0 || strcmp(argv[i], "-o") == 0)) {
  142. if (i == argc - 1) {
  143. fprintf(stderr, "%s expects an argument\n", argv[i]);
  144. return 1;
  145. }
  146. do_serialize_bytecode = 1;
  147. i += 1;
  148. if (strcmp(argv[i], "-") == 0) {
  149. outbc = stdout;
  150. } else {
  151. outbc = fopen(argv[i], "w");
  152. if (outbc == NULL) {
  153. perror(argv[i]);
  154. return 1;
  155. }
  156. }
  157. } else if (strcmp(argv[i], "--") == 0) {
  158. dashes = 1;
  159. } else if (strcmp(argv[i], "-") == 0) {
  160. do_repl = 0;
  161. input_filename = "-";
  162. inf = stdin;
  163. } else if (!was_inf_set) {
  164. do_repl = 0;
  165. input_filename = argv[i];
  166. inf = fopen(argv[i], "r");
  167. was_inf_set = 1;
  168. if (inf == NULL) {
  169. perror(argv[i]);
  170. return 1;
  171. }
  172. } else {
  173. printf("Unexpected argument: %s\n", argv[i]);
  174. usage(argv[0]);
  175. return 1;
  176. }
  177. }
  178. if (do_repl) {
  179. repl();
  180. printf("\n");
  181. return 0;
  182. }
  183. struct l2_io_mem_writer bytecode_writer = {
  184. .w.write = l2_io_mem_write,
  185. };
  186. int headerbyte = fgetc(inf);
  187. if (headerbyte == EOF || ungetc(headerbyte, inf) == EOF) {
  188. fprintf(stderr, "%s: Reading file failed.\n", input_filename);
  189. return 1;
  190. }
  191. // Detect whether input is compiled bytecode or not
  192. // (compile bytecode starts with (ESC) 'l' '2' 'c')
  193. unsigned char header[4];
  194. if (
  195. headerbyte == 0x1b &&
  196. fread(header, 1, 4, inf) >= 4 &&
  197. header[0] == 0x1b && header[1] == 0x6c &&
  198. header[2] == 0x32 && header[3] == 0x63) {
  199. if (l2_bc_load(inf, &bytecode_writer.w) < 0) {
  200. return 1;
  201. }
  202. } else if (headerbyte == 0x1b) {
  203. fprintf(
  204. stderr, "%s: Corrupt file? Start byte is 0x1b (ESC)"
  205. "but the header is wrong\n",
  206. input_filename);
  207. return 1;
  208. } else {
  209. if (parse_text(inf, &bytecode_writer.w) < 0) {
  210. return 1;
  211. }
  212. }
  213. fclose(inf);
  214. if (do_print_bytecode) {
  215. l2_vm_print_bytecode(bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  216. }
  217. if (do_serialize_bytecode) {
  218. l2_bc_serialize(
  219. outbc, bytecode_writer.mem,
  220. bytecode_writer.len / sizeof(l2_word));
  221. }
  222. if (do_print_bytecode || do_print_tokens || do_serialize_bytecode) {
  223. free(bytecode_writer.mem);
  224. return 0;
  225. }
  226. struct l2_vm vm;
  227. l2_vm_init(&vm, bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  228. if (do_step) {
  229. step_through(&vm);
  230. } else {
  231. l2_vm_run(&vm);
  232. }
  233. l2_vm_free(&vm);
  234. free(bytecode_writer.mem);
  235. }