test_as_analyzer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "as_tokenizer.h"
  5. #include "as_parser.h"
  6. #include "as_analyzer.h"
  7. #include "utils.h"
  8. char *input_buffer =
  9. "start:\n"
  10. " imm 1\n"
  11. "mid: add\n"
  12. " call start\n"
  13. " sub\n"
  14. ".insec: add\n"
  15. " div\n"
  16. "end: eq\n";
  17. int main(int argc, char** argv) {
  18. struct result result;
  19. printf("[TEST] assembler analyzer\n");
  20. // make a memory buffer to FILE*
  21. FILE *fp = fmemopen(input_buffer, strlen(input_buffer), "r");
  22. struct allocator * alct = new_allocator();
  23. struct token_stream * ts = new_token_stream(alct, fp);
  24. result = parse_prog(alct, ts);
  25. assert(result.errmsg == NULL);
  26. struct prog* prog = result.value;
  27. struct sym_table sym_table = analyze_prog(alct, prog);
  28. assert(strcmp(sym_table.buf[0].name, "start") == 0);
  29. assert(strcmp(sym_table.buf[1].name, "mid") == 0);
  30. assert(strcmp(sym_table.buf[2].name, "mid.insec") == 0);
  31. assert(strcmp(sym_table.buf[3].name, "end") == 0);
  32. assert(sym_table.buf[0].offset == 0);
  33. assert(sym_table.buf[1].offset == 9);
  34. assert(sym_table.buf[2].offset == 21);
  35. assert(sym_table.buf[3].offset == 23);
  36. printf("[PASS] assembler analyzer\n");
  37. delete_allocator(alct);
  38. return 0;
  39. }