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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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) goto out;
  80. if (rline[0] == '\0') continue;
  81. add_history(rline);
  82. snprintf(line, sizeof(line), "print (%s)", rline);
  83. free(rline);
  84. #else
  85. char rline[4096];
  86. if (fgets(rline, sizeof(rline), stdin) == NULL) goto out;
  87. if (rline[0] == '\n' && rline[1] == '\0') continue;
  88. snprintf(line, sizeof(line), "print (%s)", rline);
  89. #endif
  90. r.idx = 0;
  91. r.len = strlen(line);
  92. r.mem = line;
  93. l2_lexer_init(&lexer, &r.r);
  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. l2_vm_free(&vm);
  99. l2_gen_free(&gen);
  100. w.len = 0;
  101. l2_gen_init(&gen, &w.w);
  102. l2_vm_init(&vm, NULL, 0);
  103. } else if (w.len > 0) {
  104. vm.ops = w.mem;
  105. vm.opcount = w.len / sizeof(l2_word);
  106. while (vm.iptr < vm.opcount) {
  107. l2_vm_step(&vm);
  108. }
  109. l2_vm_gc(&vm);
  110. }
  111. }
  112. out:
  113. l2_gen_free(&gen);
  114. l2_vm_free(&vm);
  115. }
  116. static void usage(const char *argv0) {
  117. printf("Usage: %s [options] [input|-]\n", argv0);
  118. printf("\n");
  119. printf("Options:\n");
  120. printf(" --help: Print this help text\n");
  121. printf(" --bytecode: Print the generated bytecode, don't execute\n");
  122. printf(" --tokens: Print the tokens as the program is parsed, don't execute\n");
  123. printf(" --step: Step through the program\n");
  124. printf(" --repl: Start a repl\n");
  125. printf(" --output,-o <out>: Write bytecode to file\n");
  126. }
  127. int main(int argc, char **argv) {
  128. int was_inf_set = 0;
  129. FILE *inf = stdin;
  130. FILE *outbc = NULL;
  131. #ifdef __unix__
  132. do_repl = isatty(0);
  133. #endif
  134. int dashes = 0;
  135. for (int i = 1; i < argc; ++i) {
  136. if (!dashes && strcmp(argv[i], "--help") == 0) {
  137. usage(argv[0]);
  138. return 0;
  139. } else if (!dashes && strcmp(argv[i], "--bytecode") == 0) {
  140. do_print_bytecode = 1;
  141. } else if (!dashes && strcmp(argv[i], "--tokens") == 0) {
  142. do_print_tokens = 1;
  143. } else if (!dashes && strcmp(argv[i], "--step") == 0) {
  144. do_step = 1;
  145. } else if (!dashes && strcmp(argv[i], "--repl") == 0) {
  146. do_repl = 1;
  147. } else if (!dashes && (
  148. strcmp(argv[i], "--output") == 0 || strcmp(argv[i], "-o") == 0)) {
  149. if (i == argc - 1) {
  150. fprintf(stderr, "%s expects an argument\n", argv[i]);
  151. return 1;
  152. }
  153. do_serialize_bytecode = 1;
  154. i += 1;
  155. if (strcmp(argv[i], "-") == 0) {
  156. outbc = stdout;
  157. } else {
  158. outbc = fopen(argv[i], "w");
  159. if (outbc == NULL) {
  160. perror(argv[i]);
  161. return 1;
  162. }
  163. }
  164. } else if (strcmp(argv[i], "--") == 0) {
  165. dashes = 1;
  166. } else if (strcmp(argv[i], "-") == 0) {
  167. do_repl = 0;
  168. input_filename = "-";
  169. inf = stdin;
  170. } else if (!was_inf_set) {
  171. do_repl = 0;
  172. input_filename = argv[i];
  173. inf = fopen(argv[i], "r");
  174. was_inf_set = 1;
  175. if (inf == NULL) {
  176. perror(argv[i]);
  177. return 1;
  178. }
  179. } else {
  180. printf("Unexpected argument: %s\n", argv[i]);
  181. usage(argv[0]);
  182. return 1;
  183. }
  184. }
  185. if (do_repl) {
  186. repl();
  187. printf("\n");
  188. return 0;
  189. }
  190. struct l2_io_mem_writer bytecode_writer = {
  191. .w.write = l2_io_mem_write,
  192. };
  193. int headerbyte = fgetc(inf);
  194. if (headerbyte == EOF || ungetc(headerbyte, inf) == EOF) {
  195. fprintf(stderr, "%s: Reading file failed.\n", input_filename);
  196. return 1;
  197. }
  198. // Detect whether input is compiled bytecode or not
  199. // (compile bytecode starts with (ESC) 'l' '2' 'c')
  200. unsigned char header[4];
  201. if (
  202. headerbyte == 0x1b &&
  203. fread(header, 1, 4, inf) >= 4 &&
  204. header[0] == 0x1b && header[1] == 0x6c &&
  205. header[2] == 0x32 && header[3] == 0x63) {
  206. if (l2_bc_load(inf, &bytecode_writer.w) < 0) {
  207. return 1;
  208. }
  209. } else if (headerbyte == 0x1b) {
  210. fprintf(
  211. stderr, "%s: Corrupt file? Start byte is 0x1b (ESC)"
  212. "but the header is wrong\n",
  213. input_filename);
  214. return 1;
  215. } else {
  216. if (parse_text(inf, &bytecode_writer.w) < 0) {
  217. return 1;
  218. }
  219. }
  220. fclose(inf);
  221. if (do_print_bytecode) {
  222. l2_vm_print_bytecode(bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  223. }
  224. if (do_serialize_bytecode) {
  225. l2_bc_serialize(
  226. outbc, bytecode_writer.mem,
  227. bytecode_writer.len / sizeof(l2_word));
  228. }
  229. if (do_print_bytecode || do_print_tokens || do_serialize_bytecode) {
  230. free(bytecode_writer.mem);
  231. return 0;
  232. }
  233. struct l2_vm vm;
  234. l2_vm_init(&vm, bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  235. if (do_step) {
  236. step_through(&vm);
  237. } else {
  238. l2_vm_run(&vm);
  239. }
  240. l2_vm_free(&vm);
  241. free(bytecode_writer.mem);
  242. }