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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(" --step: Step through the program\n");
  29. }
  30. int main(int argc, char **argv) {
  31. int do_print_bytecode = 0;
  32. int do_step = 0;
  33. int was_inf_set = 0;
  34. FILE *inf = stdin;
  35. int dashes = 0;
  36. for (int i = 1; i < argc; ++i) {
  37. if (!dashes && strcmp(argv[i], "--help") == 0) {
  38. usage(argv[0]);
  39. return 0;
  40. } else if (!dashes && strcmp(argv[i], "--bytecode") == 0) {
  41. do_print_bytecode = 1;
  42. } else if (!dashes && strcmp(argv[i], "--step") == 0) {
  43. do_step = 1;
  44. } else if (strcmp(argv[i], "--") == 0) {
  45. dashes = 1;
  46. } else if (strcmp(argv[i], "-") == 0) {
  47. inf = stdin;
  48. } else if (!was_inf_set) {
  49. inf = fopen(argv[i], "r");
  50. was_inf_set = 1;
  51. if (inf == NULL) {
  52. perror(argv[i]);
  53. return 1;
  54. }
  55. } else {
  56. printf("Unexpected argument: %s\n", argv[i]);
  57. usage(argv[0]);
  58. return 1;
  59. }
  60. }
  61. // Init lexer with its input reader
  62. struct l2_io_file_reader r;
  63. r.r.read = l2_io_file_read;
  64. r.f = inf;
  65. struct l2_lexer lexer;
  66. l2_lexer_init(&lexer, &r.r);
  67. // Init gen with its output writer
  68. struct l2_io_mem_writer w = {0};
  69. w.w.write = l2_io_mem_write;
  70. struct l2_generator gen;
  71. l2_gen_init(&gen, &w.w);
  72. struct l2_parse_error err;
  73. if (l2_parse_program(&lexer, &gen, &err) < 0) {
  74. fprintf(stderr, "Parse error: %s:%i:%i: %s\n",
  75. (argc == 2 ? argv[1] : "-"), err.line, err.ch, err.message);
  76. l2_gen_free(&gen);
  77. fclose(inf);
  78. return 1;
  79. }
  80. l2_gen_free(&gen);
  81. fclose(inf);
  82. if (do_print_bytecode) {
  83. l2_vm_print_bytecode((l2_word *)w.mem, w.len / sizeof(l2_word));
  84. free(w.mem);
  85. return 0;
  86. }
  87. struct l2_vm vm;
  88. l2_vm_init(&vm, (void *)w.mem, w.len / sizeof(l2_word));
  89. if (do_step) {
  90. step_through(&vm);
  91. } else {
  92. l2_vm_run(&vm);
  93. }
  94. l2_vm_free(&vm);
  95. free(w.mem);
  96. }