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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 int 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 -1;
  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 -1;
  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. return 0;
  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. if (repl() < 0) {
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. struct l2_io_mem_writer bytecode_writer = {
  186. .w.write = l2_io_mem_write,
  187. };
  188. int headerbyte = fgetc(inf);
  189. if (headerbyte == EOF || ungetc(headerbyte, inf) == EOF) {
  190. fprintf(stderr, "%s: Reading file failed.\n", input_filename);
  191. return 1;
  192. }
  193. // Detect whether input is compiled bytecode or not
  194. // (compile bytecode starts with (ESC) 'l' '2' 'c')
  195. unsigned char header[4];
  196. if (
  197. headerbyte == 0x1b &&
  198. fread(header, 1, 4, inf) >= 4 &&
  199. header[0] == 0x1b && header[1] == 0x6c &&
  200. header[2] == 0x32 && header[3] == 0x63) {
  201. if (l2_bc_load(inf, &bytecode_writer.w) < 0) {
  202. return 1;
  203. }
  204. } else if (headerbyte == 0x1b) {
  205. fprintf(
  206. stderr, "%s: Corrupt file? Start byte is 0x1b (ESC)"
  207. "but the header is wrong\n",
  208. input_filename);
  209. return 1;
  210. } else {
  211. if (parse_text(inf, &bytecode_writer.w) < 0) {
  212. return 1;
  213. }
  214. }
  215. fclose(inf);
  216. if (do_print_bytecode) {
  217. l2_vm_print_bytecode(bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  218. }
  219. if (do_serialize_bytecode) {
  220. l2_bc_serialize(
  221. outbc, bytecode_writer.mem,
  222. bytecode_writer.len / sizeof(l2_word));
  223. }
  224. if (do_print_bytecode || do_print_tokens || do_serialize_bytecode) {
  225. free(bytecode_writer.mem);
  226. return 0;
  227. }
  228. struct l2_vm vm;
  229. l2_vm_init(&vm, bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  230. if (do_step) {
  231. step_through(&vm);
  232. } else {
  233. l2_vm_run(&vm);
  234. }
  235. l2_vm_free(&vm);
  236. free(bytecode_writer.mem);
  237. }