Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

main.c 6.1KB

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