(Somewhat adapted) code and solutions from the book "Build Your Own Lisp" http://www.buildyourownlisp.com
lisp
c

lispy.h 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. static char* lispy_version = "Lispy version 0.0.0.0.5";
  2. /* Parser language defintion */
  3. static char* parser =
  4. " \
  5. number : /-?[0-9]+([.][0-9]*|[0-9]*)/ ; \
  6. symbol : '+' | '-' | '*' | '/' | '%' | '^' | \
  7. \"min\" | \"max\" ; \
  8. sexpr : '(' <expr>* ')' ; \
  9. expr : <number> | <symbol> | <sexpr> ; \
  10. lispy : /^/ <expr>* /$/ ; \
  11. ";
  12. /* Declare new lval struct */
  13. typedef struct lval {
  14. int type;
  15. long num;
  16. double dec;
  17. /* Error & symbol types have some string data */
  18. char* err;
  19. char* sym;
  20. /* Counter & pointer to a list of lval */
  21. size_t count;
  22. struct lval** cell;
  23. } lval;
  24. /* Create enumeration of possible lval types */
  25. enum {LVAL_NUM, LVAL_DEC, LVAL_SYM, LVAL_SEXPR, LVAL_ERR};
  26. /* lval constructor functions */
  27. lval* lval_num(long x);
  28. lval* lval_dec(double x);
  29. lval* lval_err(char* m);
  30. lval* lval_sym(char* s);
  31. lval* lval_sexpr(void);
  32. /* lval destructor function */
  33. void lval_del(lval* v);
  34. /* lval manipulating functions */
  35. lval* lval_add(lval* v, lval* x);
  36. lval* lval_pop(lval* v, size_t i);
  37. lval* lval_take(lval* v, size_t i);
  38. lval* lval_read_num(mpc_ast_t* t);
  39. lval* lval_read(mpc_ast_t* t);
  40. lval* lval_eval_sexpr(lval* v);
  41. lval* lval_eval(lval* v);
  42. void lval_expr_print(lval* v, char open, char close);
  43. void lval_print(lval* v);
  44. void lval_println(lval* v);
  45. long min(long x, long y);
  46. long max(long x, long y);
  47. lval* builtin_op(lval* a, char* op);