test_as_parser.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. struct result result;
  15. printf("[TEST] assembler parser\n");
  16. // make a memory buffer to FILE*
  17. FILE *fp = fmemopen(input_buffer, strlen(input_buffer), "r");
  18. struct allocator * alct = new_allocator();
  19. struct token_stream * ts = new_token_stream(alct, fp);
  20. result = parse_prog(alct, ts);
  21. if (result.errmsg != NULL) {
  22. printf("error: %s\n", result.errmsg);
  23. }
  24. assert(result.errmsg == NULL);
  25. struct prog* prog = result.value;
  26. // compare output
  27. struct stmt * * stmts = prog->stmts->stmts;
  28. assert(stmts[0]->instr == NULL);
  29. assert(strcmp("start", stmts[0]->label->name) == 0);
  30. assert(stmts[1]->label == NULL);
  31. assert(stmts[1]->instr->op == OP_ADD);
  32. assert(stmts[1]->instr->arg->ival == 1);
  33. assert(strcmp("end", stmts[4]->label->name) == 0);
  34. assert(stmts[4]->instr->op == OP_EQ);
  35. printf("[PASS] assembler parser\n");
  36. fclose(fp);
  37. delete_allocator(alct);
  38. return 0;
  39. }