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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <stdio.h>
  8. #include <string.h>
  9. void step_through(struct l2_vm *vm) {
  10. printf("=====\n\nInitial state:\n");
  11. l2_vm_print_state(vm);
  12. char buf[16];
  13. while ((enum l2_opcode)vm->ops[vm->iptr] != L2_OP_HALT) {
  14. size_t iptr = vm->iptr;
  15. printf("\n======\n\n(%d) Will run instr: ", vm->iptr);
  16. l2_vm_print_op(vm->ops, vm->opcount, &iptr);
  17. fgets(buf, sizeof(buf), stdin);
  18. l2_vm_step(vm);
  19. l2_vm_print_state(vm);
  20. }
  21. }
  22. static void usage(const char *argv0) {
  23. printf("Usage: %s [options] [input|-]\n", argv0);
  24. printf("\n");
  25. printf("Options:\n");
  26. printf(" --help: Print this help text\n");
  27. printf(" --bytecode: Print the generated bytecode, don't execute\n");
  28. printf(" --tokens: Print the tokens as the program is parsed, don't execute\n");
  29. printf(" --step: Step through the program\n");
  30. }
  31. int main(int argc, char **argv) {
  32. int do_print_bytecode = 0;
  33. int do_print_tokens = 0;
  34. int do_step = 0;
  35. int was_inf_set = 0;
  36. FILE *inf = stdin;
  37. int dashes = 0;
  38. for (int i = 1; i < argc; ++i) {
  39. if (!dashes && strcmp(argv[i], "--help") == 0) {
  40. usage(argv[0]);
  41. return 0;
  42. } else if (!dashes && strcmp(argv[i], "--bytecode") == 0) {
  43. do_print_bytecode = 1;
  44. } else if (!dashes && strcmp(argv[i], "--tokens") == 0) {
  45. do_print_tokens = 1;
  46. } else if (!dashes && strcmp(argv[i], "--step") == 0) {
  47. do_step = 1;
  48. } else if (strcmp(argv[i], "--") == 0) {
  49. dashes = 1;
  50. } else if (strcmp(argv[i], "-") == 0) {
  51. inf = stdin;
  52. } else if (!was_inf_set) {
  53. inf = fopen(argv[i], "r");
  54. was_inf_set = 1;
  55. if (inf == NULL) {
  56. perror(argv[i]);
  57. return 1;
  58. }
  59. } else {
  60. printf("Unexpected argument: %s\n", argv[i]);
  61. usage(argv[0]);
  62. return 1;
  63. }
  64. }
  65. // Init lexer with its input reader
  66. struct l2_io_file_reader r;
  67. r.r.read = l2_io_file_read;
  68. r.f = inf;
  69. struct l2_lexer lexer;
  70. l2_lexer_init(&lexer, &r.r);
  71. if (do_print_tokens) {
  72. lexer.do_log_tokens = 1;
  73. }
  74. // Init gen with its output writer
  75. struct l2_io_mem_writer w = {0};
  76. w.w.write = l2_io_mem_write;
  77. struct l2_generator gen;
  78. l2_gen_init(&gen, &w.w);
  79. struct l2_parse_error err;
  80. if (l2_parse_program(&lexer, &gen, &err) < 0) {
  81. fprintf(stderr, "Parse error: %s:%i:%i: %s\n",
  82. (argc == 2 ? argv[1] : "-"), err.line, err.ch, err.message);
  83. l2_gen_free(&gen);
  84. fclose(inf);
  85. return 1;
  86. }
  87. l2_gen_free(&gen);
  88. fclose(inf);
  89. if (do_print_bytecode) {
  90. l2_vm_print_bytecode(w.mem, w.len / sizeof(l2_word));
  91. }
  92. if (do_print_bytecode || do_print_tokens) {
  93. free(w.mem);
  94. return 0;
  95. }
  96. struct l2_vm vm;
  97. l2_vm_init(&vm, w.mem, w.len / sizeof(l2_word));
  98. if (do_step) {
  99. step_through(&vm);
  100. } else {
  101. l2_vm_run(&vm);
  102. }
  103. l2_vm_free(&vm);
  104. free(w.mem);
  105. }