Reference


functions.c

  1. #include "mpc.h"
  2. #ifdef _WIN32
  3. static char buffer[2048];
  4. char* readline(char* prompt) {
  5. fputs(prompt, stdout);
  6. fgets(buffer, 2048, stdin);
  7. char* cpy = malloc(strlen(buffer)+1);
  8. strcpy(cpy, buffer);
  9. cpy[strlen(cpy)-1] = '\0';
  10. return cpy;
  11. }
  12. void add_history(char* unused) {}
  13. #else
  14. #include <editline/readline.h>
  15. #include <editline/history.h>
  16. #endif
  17. /* Forward Declarations */
  18. struct lval;
  19. struct lenv;
  20. typedef struct lval lval;
  21. typedef struct lenv lenv;
  22. /* Lisp Value */
  23. enum { LVAL_ERR, LVAL_NUM, LVAL_SYM, LVAL_FUN, LVAL_SEXPR, LVAL_QEXPR };
  24. typedef lval*(*lbuiltin)(lenv*, lval*);
  25. struct lval {
  26. int type;
  27. /* Basic */
  28. long num;
  29. char* err;
  30. char* sym;
  31. /* Function */
  32. lbuiltin builtin;
  33. lenv* env;
  34. lval* formals;
  35. lval* body;
  36. /* Expression */
  37. int count;
  38. lval** cell;
  39. };
  40. lval* lval_num(long x) {
  41. lval* v = malloc(sizeof(lval));
  42. v->type = LVAL_NUM;
  43. v->num = x;
  44. return v;
  45. }
  46. lval* lval_err(char* fmt, ...) {
  47. lval* v = malloc(sizeof(lval));
  48. v->type = LVAL_ERR;
  49. va_list va;
  50. va_start(va, fmt);
  51. v->err = malloc(512);
  52. vsnprintf(v->err, 511, fmt, va);
  53. v->err = realloc(v->err, strlen(v->err)+1);
  54. va_end(va);
  55. return v;
  56. }
  57. lval* lval_sym(char* s) {
  58. lval* v = malloc(sizeof(lval));
  59. v->type = LVAL_SYM;
  60. v->sym = malloc(strlen(s) + 1);
  61. strcpy(v->sym, s);
  62. return v;
  63. }
  64. lval* lval_builtin(lbuiltin func) {
  65. lval* v = malloc(sizeof(lval));
  66. v->type = LVAL_FUN;
  67. v->builtin = func;
  68. return v;
  69. }
  70. lenv* lenv_new(void);
  71. lval* lval_lambda(lval* formals, lval* body) {
  72. lval* v = malloc(sizeof(lval));
  73. v->type = LVAL_FUN;
  74. /* Set Builtin to Null */
  75. v->builtin = NULL;
  76. /* Build new environment */
  77. v->env = lenv_new();
  78. /* Set Formals and Body */
  79. v->formals = formals;
  80. v->body = body;
  81. return v;
  82. }
  83. lval* lval_sexpr(void) {
  84. lval* v = malloc(sizeof(lval));
  85. v->type = LVAL_SEXPR;
  86. v->count = 0;
  87. v->cell = NULL;
  88. return v;
  89. }
  90. lval* lval_qexpr(void) {
  91. lval* v = malloc(sizeof(lval));
  92. v->type = LVAL_QEXPR;
  93. v->count = 0;
  94. v->cell = NULL;
  95. return v;
  96. }
  97. void lenv_del(lenv* e);
  98. void lval_del(lval* v) {
  99. switch (v->type) {
  100. case LVAL_NUM: break;
  101. case LVAL_FUN:
  102. if (!v->builtin) {
  103. lenv_del(v->env);
  104. lval_del(v->formals);
  105. lval_del(v->body);
  106. }
  107. break;
  108. case LVAL_ERR: free(v->err); break;
  109. case LVAL_SYM: free(v->sym); break;
  110. case LVAL_QEXPR:
  111. case LVAL_SEXPR:
  112. for (int i = 0; i < v->count; i++) {
  113. lval_del(v->cell[i]);
  114. }
  115. free(v->cell);
  116. break;
  117. }
  118. free(v);
  119. }
  120. lenv* lenv_copy(lenv* e);
  121. lval* lval_copy(lval* v) {
  122. lval* x = malloc(sizeof(lval));
  123. x->type = v->type;
  124. switch (v->type) {
  125. case LVAL_FUN:
  126. if (v->builtin) {
  127. x->builtin = v->builtin;
  128. } else {
  129. x->builtin = NULL;
  130. x->env = lenv_copy(v->env);
  131. x->formals = lval_copy(v->formals);
  132. x->body = lval_copy(v->body);
  133. }
  134. break;
  135. case LVAL_NUM: x->num = v->num; break;
  136. case LVAL_ERR: x->err = malloc(strlen(v->err) + 1);
  137. strcpy(x->err, v->err);
  138. break;
  139. case LVAL_SYM: x->sym = malloc(strlen(v->sym) + 1);
  140. strcpy(x->sym, v->sym);
  141. break;
  142. case LVAL_SEXPR:
  143. case LVAL_QEXPR:
  144. x->count = v->count;
  145. x->cell = malloc(sizeof(lval*) * x->count);
  146. for (int i = 0; i < x->count; i++) {
  147. x->cell[i] = lval_copy(v->cell[i]);
  148. }
  149. break;
  150. }
  151. return x;
  152. }
  153. lval* lval_add(lval* v, lval* x) {
  154. v->count++;
  155. v->cell = realloc(v->cell, sizeof(lval*) * v->count);
  156. v->cell[v->count-1] = x;
  157. return v;
  158. }
  159. lval* lval_join(lval* x, lval* y) {
  160. for (int i = 0; i < y->count; i++) {
  161. x = lval_add(x, y->cell[i]);
  162. }
  163. free(y->cell);
  164. free(y);
  165. return x;
  166. }
  167. lval* lval_pop(lval* v, int i) {
  168. lval* x = v->cell[i];
  169. memmove(&v->cell[i],
  170. &v->cell[i+1], sizeof(lval*) * (v->count-i-1));
  171. v->count--;
  172. v->cell = realloc(v->cell, sizeof(lval*) * v->count);
  173. return x;
  174. }
  175. lval* lval_take(lval* v, int i) {
  176. lval* x = lval_pop(v, i);
  177. lval_del(v);
  178. return x;
  179. }
  180. void lval_print(lval* v);
  181. void lval_print_expr(lval* v, char open, char close) {
  182. putchar(open);
  183. for (int i = 0; i < v->count; i++) {
  184. lval_print(v->cell[i]);
  185. if (i != (v->count-1)) {
  186. putchar(' ');
  187. }
  188. }
  189. putchar(close);
  190. }
  191. void lval_print(lval* v) {
  192. switch (v->type) {
  193. case LVAL_FUN:
  194. if (v->builtin) {
  195. printf("<builtin>");
  196. } else {
  197. printf("(\\ "); lval_print(v->formals);
  198. putchar(' '); lval_print(v->body); putchar(')');
  199. }
  200. break;
  201. case LVAL_NUM: printf("%li", v->num); break;
  202. case LVAL_ERR: printf("Error: %s", v->err); break;
  203. case LVAL_SYM: printf("%s", v->sym); break;
  204. case LVAL_SEXPR: lval_print_expr(v, '(', ')'); break;
  205. case LVAL_QEXPR: lval_print_expr(v, '{', '}'); break;
  206. }
  207. }
  208. void lval_println(lval* v) { lval_print(v); putchar('\n'); }
  209. char* ltype_name(int t) {
  210. switch(t) {
  211. case LVAL_FUN: return "Function";
  212. case LVAL_NUM: return "Number";
  213. case LVAL_ERR: return "Error";
  214. case LVAL_SYM: return "Symbol";
  215. case LVAL_SEXPR: return "S-Expression";
  216. case LVAL_QEXPR: return "Q-Expression";
  217. default: return "Unknown";
  218. }
  219. }
  220. /* Lisp Environment */
  221. struct lenv {
  222. lenv* par;
  223. int count;
  224. char** syms;
  225. lval** vals;
  226. };
  227. lenv* lenv_new(void) {
  228. lenv* e = malloc(sizeof(lenv));
  229. e->par = NULL;
  230. e->count = 0;
  231. e->syms = NULL;
  232. e->vals = NULL;
  233. return e;
  234. }
  235. void lenv_del(lenv* e) {
  236. for (int i = 0; i < e->count; i++) {
  237. free(e->syms[i]);
  238. lval_del(e->vals[i]);
  239. }
  240. free(e->syms);
  241. free(e->vals);
  242. free(e);
  243. }
  244. lenv* lenv_copy(lenv* e) {
  245. lenv* n = malloc(sizeof(lenv));
  246. n->par = e->par;
  247. n->count = e->count;
  248. n->syms = malloc(sizeof(char*) * n->count);
  249. n->vals = malloc(sizeof(lval*) * n->count);
  250. for (int i = 0; i < e->count; i++) {
  251. n->syms[i] = malloc(strlen(e->syms[i]) + 1);
  252. strcpy(n->syms[i], e->syms[i]);
  253. n->vals[i] = lval_copy(e->vals[i]);
  254. }
  255. return n;
  256. }
  257. lval* lenv_get(lenv* e, lval* k) {
  258. for (int i = 0; i < e->count; i++) {
  259. if (strcmp(e->syms[i], k->sym) == 0) {
  260. return lval_copy(e->vals[i]);
  261. }
  262. }
  263. /* If no symbol check in parent otherwise error */
  264. if (e->par) {
  265. return lenv_get(e->par, k);
  266. } else {
  267. return lval_err("Unbound Symbol '%s'", k->sym);
  268. }
  269. }
  270. void lenv_put(lenv* e, lval* k, lval* v) {
  271. for (int i = 0; i < e->count; i++) {
  272. if (strcmp(e->syms[i], k->sym) == 0) {
  273. lval_del(e->vals[i]);
  274. e->vals[i] = lval_copy(v);
  275. return;
  276. }
  277. }
  278. e->count++;
  279. e->vals = realloc(e->vals, sizeof(lval*) * e->count);
  280. e->syms = realloc(e->syms, sizeof(char*) * e->count);
  281. e->vals[e->count-1] = lval_copy(v);
  282. e->syms[e->count-1] = malloc(strlen(k->sym)+1);
  283. strcpy(e->syms[e->count-1], k->sym);
  284. }
  285. void lenv_def(lenv* e, lval* k, lval* v) {
  286. /* Iterate till e has no parent */
  287. while (e->par) { e = e->par; }
  288. /* Put value in e */
  289. lenv_put(e, k, v);
  290. }
  291. /* Builtins */
  292. #define LASSERT(args, cond, fmt, ...) \
  293. if (!(cond)) { lval* err = lval_err(fmt, ##__VA_ARGS__); lval_del(args); return err; }
  294. #define LASSERT_TYPE(func, args, index, expect) \
  295. LASSERT(args, args->cell[index]->type == expect, \
  296. "Function '%s' passed incorrect type for argument %i. " \
  297. "Got %s, Expected %s.", \
  298. func, index, ltype_name(args->cell[index]->type), ltype_name(expect))
  299. #define LASSERT_NUM(func, args, num) \
  300. LASSERT(args, args->count == num, \
  301. "Function '%s' passed incorrect number of arguments. " \
  302. "Got %i, Expected %i.", \
  303. func, args->count, num)
  304. #define LASSERT_NOT_EMPTY(func, args, index) \
  305. LASSERT(args, args->cell[index]->count != 0, \
  306. "Function '%s' passed {} for argument %i.", func, index);
  307. lval* lval_eval(lenv* e, lval* v);
  308. lval* builtin_lambda(lenv* e, lval* a) {
  309. /* Check Two arguments, each of which are Q-Expressions */
  310. LASSERT_NUM("\\", a, 2);
  311. LASSERT_TYPE("\\", a, 0, LVAL_QEXPR);
  312. LASSERT_TYPE("\\", a, 1, LVAL_QEXPR);
  313. /* Check first Q-Expression contains only Symbols */
  314. for (int i = 0; i < a->cell[0]->count; i++) {
  315. LASSERT(a, (a->cell[0]->cell[i]->type == LVAL_SYM),
  316. "Cannot define non-symbol. Got %s, Expected %s.",
  317. ltype_name(a->cell[0]->cell[i]->type),ltype_name(LVAL_SYM));
  318. }
  319. /* Pop first two arguments and pass them to lval_lambda */
  320. lval* formals = lval_pop(a, 0);
  321. lval* body = lval_pop(a, 0);
  322. lval_del(a);
  323. return lval_lambda(formals, body);
  324. }
  325. lval* builtin_list(lenv* e, lval* a) {
  326. a->type = LVAL_QEXPR;
  327. return a;
  328. }
  329. lval* builtin_head(lenv* e, lval* a) {
  330. LASSERT_NUM("head", a, 1);
  331. LASSERT_TYPE("head", a, 0, LVAL_QEXPR);
  332. LASSERT_NOT_EMPTY("head", a, 0);
  333. lval* v = lval_take(a, 0);
  334. while (v->count > 1) { lval_del(lval_pop(v, 1)); }
  335. return v;
  336. }
  337. lval* builtin_tail(lenv* e, lval* a) {
  338. LASSERT_NUM("tail", a, 1);
  339. LASSERT_TYPE("tail", a, 0, LVAL_QEXPR);
  340. LASSERT_NOT_EMPTY("tail", a, 0);
  341. lval* v = lval_take(a, 0);
  342. lval_del(lval_pop(v, 0));
  343. return v;
  344. }
  345. lval* builtin_eval(lenv* e, lval* a) {
  346. LASSERT_NUM("eval", a, 1);
  347. LASSERT_TYPE("eval", a, 0, LVAL_QEXPR);
  348. lval* x = lval_take(a, 0);
  349. x->type = LVAL_SEXPR;
  350. return lval_eval(e, x);
  351. }
  352. lval* builtin_join(lenv* e, lval* a) {
  353. for (int i = 0; i < a->count; i++) {
  354. LASSERT_TYPE("join", a, i, LVAL_QEXPR);
  355. }
  356. lval* x = lval_pop(a, 0);
  357. while (a->count) {
  358. lval* y = lval_pop(a, 0);
  359. x = lval_join(x, y);
  360. }
  361. lval_del(a);
  362. return x;
  363. }
  364. lval* builtin_op(lenv* e, lval* a, char* op) {
  365. for (int i = 0; i < a->count; i++) {
  366. LASSERT_TYPE(op, a, i, LVAL_NUM);
  367. }
  368. lval* x = lval_pop(a, 0);
  369. if ((strcmp(op, "-") == 0) && a->count == 0) { x->num = -x->num; }
  370. while (a->count > 0) {
  371. lval* y = lval_pop(a, 0);
  372. if (strcmp(op, "+") == 0) { x->num += y->num; }
  373. if (strcmp(op, "-") == 0) { x->num -= y->num; }
  374. if (strcmp(op, "*") == 0) { x->num *= y->num; }
  375. if (strcmp(op, "/") == 0) {
  376. if (y->num == 0) {
  377. lval_del(x); lval_del(y);
  378. x = lval_err("Division By Zero.");
  379. break;
  380. }
  381. x->num /= y->num;
  382. }
  383. lval_del(y);
  384. }
  385. lval_del(a);
  386. return x;
  387. }
  388. lval* builtin_add(lenv* e, lval* a) { return builtin_op(e, a, "+"); }
  389. lval* builtin_sub(lenv* e, lval* a) { return builtin_op(e, a, "-"); }
  390. lval* builtin_mul(lenv* e, lval* a) { return builtin_op(e, a, "*"); }
  391. lval* builtin_div(lenv* e, lval* a) { return builtin_op(e, a, "/"); }
  392. lval* builtin_var(lenv* e, lval* a, char* func) {
  393. LASSERT_TYPE(func, a, 0, LVAL_QEXPR);
  394. lval* syms = a->cell[0];
  395. for (int i = 0; i < syms->count; i++) {
  396. LASSERT(a, (syms->cell[i]->type == LVAL_SYM),
  397. "Function '%s' cannot define non-symbol. "
  398. "Got %s, Expected %s.", func,
  399. ltype_name(syms->cell[i]->type), ltype_name(LVAL_SYM));
  400. }
  401. LASSERT(a, (syms->count == a->count-1),
  402. "Function '%s' passed too many arguments for symbols. "
  403. "Got %i, Expected %i.", func, syms->count, a->count-1);
  404. for (int i = 0; i < syms->count; i++) {
  405. /* If 'def' define in globally. If 'put' define in locally */
  406. if (strcmp(func, "def") == 0) {
  407. lenv_def(e, syms->cell[i], a->cell[i+1]);
  408. }
  409. if (strcmp(func, "=") == 0) {
  410. lenv_put(e, syms->cell[i], a->cell[i+1]);
  411. }
  412. }
  413. lval_del(a);
  414. return lval_sexpr();
  415. }
  416. lval* builtin_def(lenv* e, lval* a) {
  417. return builtin_var(e, a, "def");
  418. }
  419. lval* builtin_put(lenv* e, lval* a) {
  420. return builtin_var(e, a, "=");
  421. }
  422. void lenv_add_builtin(lenv* e, char* name, lbuiltin func) {
  423. lval* k = lval_sym(name);
  424. lval* v = lval_builtin(func);
  425. lenv_put(e, k, v);
  426. lval_del(k); lval_del(v);
  427. }
  428. void lenv_add_builtins(lenv* e) {
  429. /* Variable Functions */
  430. lenv_add_builtin(e, "\\", builtin_lambda);
  431. lenv_add_builtin(e, "def", builtin_def);
  432. lenv_add_builtin(e, "=", builtin_put);
  433. /* List Functions */
  434. lenv_add_builtin(e, "list", builtin_list);
  435. lenv_add_builtin(e, "head", builtin_head);
  436. lenv_add_builtin(e, "tail", builtin_tail);
  437. lenv_add_builtin(e, "eval", builtin_eval);
  438. lenv_add_builtin(e, "join", builtin_join);
  439. /* Mathematical Functions */
  440. lenv_add_builtin(e, "+", builtin_add);
  441. lenv_add_builtin(e, "-", builtin_sub);
  442. lenv_add_builtin(e, "*", builtin_mul);
  443. lenv_add_builtin(e, "/", builtin_div);
  444. }
  445. /* Evaluation */
  446. lval* lval_call(lenv* e, lval* f, lval* a) {
  447. /* If Builtin then simply apply that */
  448. if (f->builtin) { return f->builtin(e, a); }
  449. /* Record Argument Counts */
  450. int given = a->count;
  451. int total = f->formals->count;
  452. /* While arguments still remain to be processed */
  453. while (a->count) {
  454. /* If we've ran out of formal arguments to bind */
  455. if (f->formals->count == 0) {
  456. lval_del(a);
  457. return lval_err("Function passed too many arguments. "
  458. "Got %i, Expected %i.", given, total);
  459. }
  460. /* Pop the first symbol from the formals */
  461. lval* sym = lval_pop(f->formals, 0);
  462. /* Special Case to deal with '&' */
  463. if (strcmp(sym->sym, "&") == 0) {
  464. /* Ensure '&' is followed by another symbol */
  465. if (f->formals->count != 1) {
  466. lval_del(a);
  467. return lval_err("Function format invalid. "
  468. "Symbol '&' not followed by single symbol.");
  469. }
  470. /* Next formal should be bound to remaining arguments */
  471. lval* nsym = lval_pop(f->formals, 0);
  472. lenv_put(f->env, nsym, builtin_list(e, a));
  473. lval_del(sym); lval_del(nsym);
  474. break;
  475. }
  476. /* Pop the next argument from the list */
  477. lval* val = lval_pop(a, 0);
  478. /* Bind a copy into the function's environment */
  479. lenv_put(f->env, sym, val);
  480. /* Delete symbol and value */
  481. lval_del(sym); lval_del(val);
  482. }
  483. /* Argument list is now bound so can be cleaned up */
  484. lval_del(a);
  485. /* If '&' remains in formal list bind to empty list */
  486. if (f->formals->count > 0 &&
  487. strcmp(f->formals->cell[0]->sym, "&") == 0) {
  488. /* Check to ensure that & is not passed invalidly. */
  489. if (f->formals->count != 2) {
  490. return lval_err("Function format invalid. "
  491. "Symbol '&' not followed by single symbol.");
  492. }
  493. /* Pop and delete '&' symbol */
  494. lval_del(lval_pop(f->formals, 0));
  495. /* Pop next symbol and create empty list */
  496. lval* sym = lval_pop(f->formals, 0);
  497. lval* val = lval_qexpr();
  498. /* Bind to environment and delete */
  499. lenv_put(f->env, sym, val);
  500. lval_del(sym); lval_del(val);
  501. }
  502. /* If all formals have been bound evaluate */
  503. if (f->formals->count == 0) {
  504. /* Set environment parent to evaluation environment */
  505. f->env->par = e;
  506. /* Evaluate and return */
  507. return builtin_eval(f->env,
  508. lval_add(lval_sexpr(), lval_copy(f->body)));
  509. } else {
  510. /* Otherwise return partially evaluated function */
  511. return lval_copy(f);
  512. }
  513. }
  514. lval* lval_eval_sexpr(lenv* e, lval* v) {
  515. for (int i = 0; i < v->count; i++) {
  516. v->cell[i] = lval_eval(e, v->cell[i]);
  517. }
  518. for (int i = 0; i < v->count; i++) {
  519. if (v->cell[i]->type == LVAL_ERR) { return lval_take(v, i); }
  520. }
  521. if (v->count == 0) { return v; }
  522. if (v->count == 1) { return lval_eval(e, lval_take(v, 0)); }
  523. lval* f = lval_pop(v, 0);
  524. if (f->type != LVAL_FUN) {
  525. lval* err = lval_err(
  526. "S-Expression starts with incorrect type. "
  527. "Got %s, Expected %s.",
  528. ltype_name(f->type), ltype_name(LVAL_FUN));
  529. lval_del(f); lval_del(v);
  530. return err;
  531. }
  532. lval* result = lval_call(e, f, v);
  533. lval_del(f);
  534. return result;
  535. }
  536. lval* lval_eval(lenv* e, lval* v) {
  537. if (v->type == LVAL_SYM) {
  538. lval* x = lenv_get(e, v);
  539. lval_del(v);
  540. return x;
  541. }
  542. if (v->type == LVAL_SEXPR) { return lval_eval_sexpr(e, v); }
  543. return v;
  544. }
  545. /* Reading */
  546. lval* lval_read_num(mpc_ast_t* t) {
  547. errno = 0;
  548. long x = strtol(t->contents, NULL, 10);
  549. return errno != ERANGE ? lval_num(x) : lval_err("Invalid Number.");
  550. }
  551. lval* lval_read(mpc_ast_t* t) {
  552. if (strstr(t->tag, "number")) { return lval_read_num(t); }
  553. if (strstr(t->tag, "symbol")) { return lval_sym(t->contents); }
  554. lval* x = NULL;
  555. if (strcmp(t->tag, ">") == 0) { x = lval_sexpr(); }
  556. if (strstr(t->tag, "sexpr")) { x = lval_sexpr(); }
  557. if (strstr(t->tag, "qexpr")) { x = lval_qexpr(); }
  558. for (int i = 0; i < t->children_num; i++) {
  559. if (strcmp(t->children[i]->contents, "(") == 0) { continue; }
  560. if (strcmp(t->children[i]->contents, ")") == 0) { continue; }
  561. if (strcmp(t->children[i]->contents, "}") == 0) { continue; }
  562. if (strcmp(t->children[i]->contents, "{") == 0) { continue; }
  563. if (strcmp(t->children[i]->tag, "regex") == 0) { continue; }
  564. x = lval_add(x, lval_read(t->children[i]));
  565. }
  566. return x;
  567. }
  568. /* Main */
  569. int main(int argc, char** argv) {
  570. mpc_parser_t* Number = mpc_new("number");
  571. mpc_parser_t* Symbol = mpc_new("symbol");
  572. mpc_parser_t* Sexpr = mpc_new("sexpr");
  573. mpc_parser_t* Qexpr = mpc_new("qexpr");
  574. mpc_parser_t* Expr = mpc_new("expr");
  575. mpc_parser_t* Lispy = mpc_new("lispy");
  576. mpca_lang(MPCA_LANG_DEFAULT,
  577. " \
  578. number : /-?[0-9]+/ ; \
  579. symbol : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; \
  580. sexpr : '(' <expr>* ')' ; \
  581. qexpr : '{' <expr>* '}' ; \
  582. expr : <number> | <symbol> | <sexpr> | <qexpr> ; \
  583. lispy : /^/ <expr>* /$/ ; \
  584. ",
  585. Number, Symbol, Sexpr, Qexpr, Expr, Lispy);
  586. puts("Lispy Version 0.0.0.0.8");
  587. puts("Press Ctrl+c to Exit\n");
  588. lenv* e = lenv_new();
  589. lenv_add_builtins(e);
  590. while (1) {
  591. char* input = readline("lispy> ");
  592. add_history(input);
  593. mpc_result_t r;
  594. if (mpc_parse("<stdin>", input, Lispy, &r)) {
  595. lval* x = lval_eval(e, lval_read(r.output));
  596. lval_println(x);
  597. lval_del(x);
  598. mpc_ast_delete(r.output);
  599. } else {
  600. mpc_err_print(r.error);
  601. mpc_err_delete(r.error);
  602. }
  603. free(input);
  604. }
  605. lenv_del(e);
  606. mpc_cleanup(6, Number, Symbol, Sexpr, Qexpr, Expr, Lispy);
  607. return 0;
  608. }