test_as_parser.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include "as_tokenizer.h"
  5. #include "as_parser.h"
  6. #include "utils.h"
  7. char *input_buffer =
  8. "start:\n"
  9. " add 1\n"
  10. " sub start\n"
  11. " div\n"
  12. "end: eq\n";
  13. int main(int argc, char** argv) {
  14. printf("[TEST] assembler parser\n");
  15. // make a memory buffer to FILE*
  16. FILE *fp = fmemopen(input_buffer, strlen(input_buffer), "r");
  17. struct allocator * alct = new_allocator();
  18. struct token_stream * ts = new_token_stream(alct, fp);
  19. struct prog * prog = parse_prog(alct, ts);
  20. // compare output
  21. struct stmt * * stmts = prog->stmts->stmts;
  22. assert(stmts[0]->instr == NULL);
  23. assert(strcmp("start", stmts[0]->label->name) == 0);
  24. assert(stmts[1]->label == NULL);
  25. assert(stmts[1]->instr->op == OP_ADD);
  26. assert(stmts[1]->instr->arg->ival == 1);
  27. assert(strcmp("end", stmts[4]->label->name) == 0);
  28. assert(stmts[4]->instr->op == OP_EQ);
  29. printf("[PASS] assembler parser\n");
  30. delete_allocator(alct);
  31. return 0;
  32. }