(Somewhat adapted) code and solutions from the book "Build Your Own Lisp" http://www.buildyourownlisp.com
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.

71 lines
2.0 KiB

  1. static char* lispy_version = "Lispy version 0.0.0.0.6";
  2. /* Parser language defintion */
  3. static char* parser =
  4. " \
  5. number : /-?[0-9]+([.][0-9]*|[0-9]*)/ ; \
  6. symbol : \"list\" | \"head\" | \"tail\" | \"join\" | \
  7. \"eval\" | '+' | '-' | '*' | '/' | '%' | \
  8. '^' | \"min\" | \"max\" ; \
  9. sexpr : '(' <expr>* ')' ; \
  10. qexpr : '{' <expr>* '}' ; \
  11. expr : <number> | <symbol> | <sexpr> | <qexpr>; \
  12. lispy : /^/ <expr>* /$/ ; \
  13. ";
  14. /* Declare new lval struct */
  15. typedef struct lval {
  16. int type;
  17. /* only one of the two number/decimal is valid in a
  18. lval instance, so we can use an anonymous union */
  19. union {
  20. long num;
  21. double dec;
  22. };
  23. /* Error & symbol types have some string data */
  24. char* err;
  25. char* sym;
  26. /* Counter & pointer to a list of lval */
  27. size_t count;
  28. struct lval** cell;
  29. } lval;
  30. /* Create enumeration of possible lval types */
  31. enum lval_types {LVAL_NUM, LVAL_DEC, LVAL_SYM, LVAL_SEXPR,
  32. LVAL_QEXPR, LVAL_ERR};
  33. /* lval constructor functions */
  34. lval* lval_num(long x);
  35. lval* lval_dec(double x);
  36. lval* lval_err(char* m);
  37. lval* lval_sym(char* s);
  38. lval* lval_sexpr(void);
  39. lval* lval_qexpr(void);
  40. /* lval destructor function */
  41. void lval_del(lval* v);
  42. /* lval manipulating functions */
  43. lval* lval_add(lval* v, lval* x);
  44. lval* lval_pop(lval* v, size_t i);
  45. lval* lval_take(lval* v, size_t i);
  46. lval* lval_read_num(mpc_ast_t* t);
  47. lval* lval_read(mpc_ast_t* t);
  48. lval* lval_eval_sexpr(lval* v);
  49. lval* lval_eval(lval* v);
  50. /* lval print functions */
  51. void lval_expr_print(lval* v, char open, char close);
  52. void lval_print(lval* v);
  53. void lval_println(lval* v);
  54. /* language built-in operators and functions */
  55. long min(long x, long y);
  56. long max(long x, long y);
  57. lval* builtin_op(lval* a, char* op);
  58. lval* builtin_head(lval* a);
  59. lval* builtin_tail(lval* a);