Infinity Calculator implementation for Assignment 2 in Computer Organization.
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.

46 lines
768 B

2 years ago
2 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "expr.h"
  4. #include "infint.h"
  5. #include "parse.h"
  6. #include "eval.h"
  7. void eval_line(char *line) {
  8. input_str = line;
  9. expr_t *e = parse(0);
  10. if (!e) return;
  11. #ifdef UNIT_TESTS
  12. print_expr_tree(e, 0);
  13. #endif
  14. infint_t *iie = evaluate(e);
  15. char *iistr = infint_to_string(iie);
  16. printf("%s\n", iistr);
  17. free(iistr);
  18. infint_free(iie);
  19. destroy_expr(e);
  20. }
  21. int main() {
  22. #ifdef UNIT_TESTS
  23. test_infint();
  24. test_expr_and_parse();
  25. test_evaluate();
  26. printf("**********************\n| All tests passed |\n**********************\n");
  27. #endif
  28. char *line = NULL;
  29. size_t line_len = 0;
  30. while(getline(&line,&line_len,stdin) != -1){
  31. if (*line != '\n') {
  32. eval_line(line);
  33. }
  34. free(line);
  35. line = NULL;
  36. }
  37. free(line);
  38. }