lisp but crispier
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.

54 lines
1.2KB

  1. //
  2. // Created by red on 03/12/19.
  3. //
  4. #include <stdio.h>
  5. #include <editline/readline.h>
  6. #include <editline/history.h>
  7. #include <stdlib.h>
  8. #include "mpc.h"
  9. #include "eval.h"
  10. #pragma clang diagnostic ignored "-Wmissing-noreturn"
  11. int main(int argc, char** argv) {
  12. mpc_parser_t* Number = mpc_new("number");
  13. mpc_parser_t* Operator = mpc_new("operator");
  14. mpc_parser_t* Expr = mpc_new("expr");
  15. mpc_parser_t* Crisp = mpc_new("crisp");
  16. mpca_lang(MPCA_LANG_DEFAULT,
  17. " number : /-?[0-9]+/ ;"
  18. " operator : '+' | '-' | '*' | '/' ;"
  19. " expr : <number> | '(' <operator> <expr>+ ')' ;"
  20. " crisp : /^/ <operator> <expr>+ /$/ ;",
  21. Number, Operator, Expr, Crisp);
  22. puts("Crisp v.0.0.1");
  23. puts("Press Ctrl-C to exit\n");
  24. for (;;) {
  25. char* input = readline("crisp> ");
  26. add_history(input);
  27. // attempt to parse input
  28. mpc_result_t r;
  29. if (mpc_parse("<stdin>", input, Crisp, &r)) {
  30. cval result = eval(r.output);
  31. cval_println(result);
  32. mpc_ast_delete(r.output);
  33. } else {
  34. mpc_err_print(r.error);
  35. mpc_err_delete(r.error);
  36. }
  37. free(input);
  38. }
  39. mpc_cleanup(4, Number, Operator, Expr, Crisp);
  40. return EXIT_SUCCESS;
  41. }