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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. static int do_print_bytecode = 0;
  12. static int do_print_tokens = 0;
  13. static int do_step = 0;
  14. static int do_serialize_bytecode = 0;
  15. static char *input_filename = "-";
  16. static int parse_text(FILE *inf, struct l2_io_writer *w) {
  17. // Init lexer with its input reader
  18. struct l2_io_file_reader r;
  19. r.r.read = l2_io_file_read;
  20. r.f = inf;
  21. struct l2_lexer lexer;
  22. l2_lexer_init(&lexer, &r.r);
  23. if (do_print_tokens) {
  24. lexer.do_log_tokens = 1;
  25. }
  26. // Init gen with its output writer
  27. struct l2_generator gen;
  28. l2_gen_init(&gen, w);
  29. struct l2_parse_error err;
  30. if (l2_parse_program(&lexer, &gen, &err) < 0) {
  31. fprintf(stderr, "Parse error: %s:%i:%i: %s\n",
  32. input_filename, err.line, err.ch, err.message);
  33. l2_gen_free(&gen);
  34. fclose(inf);
  35. return -1;
  36. }
  37. l2_gen_free(&gen);
  38. return 0;
  39. }
  40. static void step_through(struct l2_vm *vm) {
  41. printf("=====\n\nInitial state:\n");
  42. l2_vm_print_state(vm);
  43. char buf[16];
  44. while (!vm->halted) {
  45. size_t iptr = vm->iptr;
  46. printf("\n======\n\n(%d) Will run instr: ", vm->iptr);
  47. l2_vm_print_op(vm->ops, vm->opcount, &iptr);
  48. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  49. break;
  50. }
  51. l2_vm_step(vm);
  52. l2_vm_print_state(vm);
  53. }
  54. }
  55. static void usage(const char *argv0) {
  56. printf("Usage: %s [options] [input|-]\n", argv0);
  57. printf("\n");
  58. printf("Options:\n");
  59. printf(" --help: Print this help text\n");
  60. printf(" --bytecode: Print the generated bytecode, don't execute\n");
  61. printf(" --tokens: Print the tokens as the program is parsed, don't execute\n");
  62. printf(" --step: Step through the program\n");
  63. printf(" --output,-o <out>: Write bytecode to file\n");
  64. }
  65. int main(int argc, char **argv) {
  66. int was_inf_set = 0;
  67. FILE *inf = stdin;
  68. FILE *outbc = NULL;
  69. int dashes = 0;
  70. for (int i = 1; i < argc; ++i) {
  71. if (!dashes && strcmp(argv[i], "--help") == 0) {
  72. usage(argv[0]);
  73. return 0;
  74. } else if (!dashes && strcmp(argv[i], "--bytecode") == 0) {
  75. do_print_bytecode = 1;
  76. } else if (!dashes && strcmp(argv[i], "--tokens") == 0) {
  77. do_print_tokens = 1;
  78. } else if (!dashes && strcmp(argv[i], "--step") == 0) {
  79. do_step = 1;
  80. } else if (!dashes && (
  81. strcmp(argv[i], "--output") == 0 || strcmp(argv[i], "-o") == 0)) {
  82. if (i == argc - 1) {
  83. fprintf(stderr, "%s expects an argument\n", argv[i]);
  84. return 1;
  85. }
  86. do_serialize_bytecode = 1;
  87. i += 1;
  88. if (strcmp(argv[i], "-") == 0) {
  89. outbc = stdout;
  90. } else {
  91. outbc = fopen(argv[i], "w");
  92. if (outbc == NULL) {
  93. perror(argv[i]);
  94. return 1;
  95. }
  96. }
  97. } else if (strcmp(argv[i], "--") == 0) {
  98. dashes = 1;
  99. } else if (strcmp(argv[i], "-") == 0) {
  100. input_filename = "-";
  101. inf = stdin;
  102. } else if (!was_inf_set) {
  103. input_filename = argv[i];
  104. inf = fopen(argv[i], "r");
  105. was_inf_set = 1;
  106. if (inf == NULL) {
  107. perror(argv[i]);
  108. return 1;
  109. }
  110. } else {
  111. printf("Unexpected argument: %s\n", argv[i]);
  112. usage(argv[0]);
  113. return 1;
  114. }
  115. }
  116. struct l2_io_mem_writer bytecode_writer = {0};
  117. bytecode_writer.w.write = l2_io_mem_write;
  118. //
  119. int headerbyte = fgetc(inf);
  120. if (headerbyte == EOF || ungetc(headerbyte, inf) == EOF) {
  121. fprintf(stderr, "%s: Reading file failed.\n", input_filename);
  122. return 1;
  123. }
  124. // Detect whether input is compiled bytecode or not
  125. // (compile bytecode starts with (ESC) 'l' '2' 'c')
  126. unsigned char header[4];
  127. if (
  128. headerbyte == 0x1b &&
  129. fread(header, 1, 4, inf) >= 4 &&
  130. header[0] == 0x1b && header[1] == 0x6c &&
  131. header[2] == 0x32 && header[3] == 0x63) {
  132. if (l2_bc_load(inf, &bytecode_writer.w) < 0) {
  133. return 1;
  134. }
  135. } else if (headerbyte == 0x1b) {
  136. fprintf(
  137. stderr, "%s: Corrupt file? Start byte is 0x1b (ESC)"
  138. "but the header is wrong\n",
  139. input_filename);
  140. return 1;
  141. } else {
  142. if (parse_text(inf, &bytecode_writer.w) < 0) {
  143. return 1;
  144. }
  145. }
  146. fclose(inf);
  147. if (do_print_bytecode) {
  148. l2_vm_print_bytecode(bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  149. }
  150. if (do_serialize_bytecode) {
  151. l2_bc_serialize(
  152. outbc, bytecode_writer.mem,
  153. bytecode_writer.len / sizeof(l2_word));
  154. }
  155. if (do_print_bytecode || do_print_tokens || do_serialize_bytecode) {
  156. free(bytecode_writer.mem);
  157. return 0;
  158. }
  159. struct l2_vm vm;
  160. l2_vm_init(&vm, bytecode_writer.mem, bytecode_writer.len / sizeof(l2_word));
  161. if (do_step) {
  162. step_through(&vm);
  163. } else {
  164. l2_vm_run(&vm);
  165. }
  166. l2_vm_free(&vm);
  167. free(bytecode_writer.mem);
  168. }