Reference


hand_rolled_parser.c

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <errno.h>
  6. #ifdef _WIN32
  7. static char buffer[2048];
  8. char* readline(char* prompt) {
  9. fputs(prompt, stdout);
  10. fgets(buffer, 2048, stdin);
  11. char* cpy = malloc(strlen(buffer)+1);
  12. strcpy(cpy, buffer);
  13. cpy[strlen(cpy)-1] = '\0';
  14. return cpy;
  15. }
  16. void add_history(char* unused) {}
  17. #else
  18. #include <editline/readline.h>
  19. #include <editline/history.h>
  20. #endif
  21. /* Forward Declarations */
  22. struct lval;
  23. struct lenv;
  24. typedef struct lval lval;
  25. typedef struct lenv lenv;
  26. /* Lisp Value */
  27. enum { LVAL_ERR, LVAL_NUM, LVAL_SYM, LVAL_STR,
  28. LVAL_FUN, LVAL_SEXPR, LVAL_QEXPR };
  29. typedef lval*(*lbuiltin)(lenv*, lval*);
  30. struct lval {
  31. int type;
  32. /* Basic */
  33. long num;
  34. char* err;
  35. char* sym;
  36. char* str;
  37. /* Function */
  38. lbuiltin builtin;
  39. lenv* env;
  40. lval* formals;
  41. lval* body;
  42. /* Expression */
  43. int count;
  44. lval** cell;
  45. };
  46. lval* lval_num(long x) {
  47. lval* v = malloc(sizeof(lval));
  48. v->type = LVAL_NUM;
  49. v->num = x;
  50. return v;
  51. }
  52. lval* lval_err(char* fmt, ...) {
  53. lval* v = malloc(sizeof(lval));
  54. v->type = LVAL_ERR;
  55. va_list va;
  56. va_start(va, fmt);
  57. v->err = malloc(512);
  58. vsnprintf(v->err, 511, fmt, va);
  59. v->err = realloc(v->err, strlen(v->err)+1);
  60. va_end(va);
  61. return v;
  62. }
  63. lval* lval_sym(char* s) {
  64. lval* v = malloc(sizeof(lval));
  65. v->type = LVAL_SYM;
  66. v->sym = malloc(strlen(s) + 1);
  67. strcpy(v->sym, s);
  68. return v;
  69. }
  70. lval* lval_str(char* s) {
  71. lval* v = malloc(sizeof(lval));
  72. v->type = LVAL_STR;
  73. v->str = malloc(strlen(s) + 1);
  74. strcpy(v->str, s);
  75. return v;
  76. }
  77. lval* lval_builtin(lbuiltin func) {
  78. lval* v = malloc(sizeof(lval));
  79. v->type = LVAL_FUN;
  80. v->builtin = func;
  81. return v;
  82. }
  83. lenv* lenv_new(void);
  84. lval* lval_lambda(lval* formals, lval* body) {
  85. lval* v = malloc(sizeof(lval));
  86. v->type = LVAL_FUN;
  87. v->builtin = NULL;
  88. v->env = lenv_new();
  89. v->formals = formals;
  90. v->body = body;
  91. return v;
  92. }
  93. lval* lval_sexpr(void) {
  94. lval* v = malloc(sizeof(lval));
  95. v->type = LVAL_SEXPR;
  96. v->count = 0;
  97. v->cell = NULL;
  98. return v;
  99. }
  100. lval* lval_qexpr(void) {
  101. lval* v = malloc(sizeof(lval));
  102. v->type = LVAL_QEXPR;
  103. v->count = 0;
  104. v->cell = NULL;
  105. return v;
  106. }
  107. void lenv_del(lenv* e);
  108. void lval_del(lval* v) {
  109. switch (v->type) {
  110. case LVAL_NUM: break;
  111. case LVAL_FUN:
  112. if (!v->builtin) {
  113. lenv_del(v->env);
  114. lval_del(v->formals);
  115. lval_del(v->body);
  116. }
  117. break;
  118. case LVAL_ERR: free(v->err); break;
  119. case LVAL_SYM: free(v->sym); break;
  120. case LVAL_STR: free(v->str); break;
  121. case LVAL_QEXPR:
  122. case LVAL_SEXPR:
  123. for (int i = 0; i < v->count; i++) {
  124. lval_del(v->cell[i]);
  125. }
  126. free(v->cell);
  127. break;
  128. }
  129. free(v);
  130. }
  131. lenv* lenv_copy(lenv* e);
  132. lval* lval_copy(lval* v) {
  133. lval* x = malloc(sizeof(lval));
  134. x->type = v->type;
  135. switch (v->type) {
  136. case LVAL_FUN:
  137. if (v->builtin) {
  138. x->builtin = v->builtin;
  139. } else {
  140. x->builtin = NULL;
  141. x->env = lenv_copy(v->env);
  142. x->formals = lval_copy(v->formals);
  143. x->body = lval_copy(v->body);
  144. }
  145. break;
  146. case LVAL_NUM: x->num = v->num; break;
  147. case LVAL_ERR: x->err = malloc(strlen(v->err) + 1);
  148. strcpy(x->err, v->err);
  149. break;
  150. case LVAL_SYM: x->sym = malloc(strlen(v->sym) + 1);
  151. strcpy(x->sym, v->sym);
  152. break;
  153. case LVAL_STR: x->str = malloc(strlen(v->str) + 1);
  154. strcpy(x->str, v->str);
  155. break;
  156. case LVAL_SEXPR:
  157. case LVAL_QEXPR:
  158. x->count = v->count;
  159. x->cell = malloc(sizeof(lval*) * x->count);
  160. for (int i = 0; i < x->count; i++) {
  161. x->cell[i] = lval_copy(v->cell[i]);
  162. }
  163. break;
  164. }
  165. return x;
  166. }
  167. lval* lval_add(lval* v, lval* x) {
  168. v->count++;
  169. v->cell = realloc(v->cell, sizeof(lval*) * v->count);
  170. v->cell[v->count-1] = x;
  171. return v;
  172. }
  173. lval* lval_join(lval* x, lval* y) {
  174. for (int i = 0; i < y->count; i++) {
  175. x = lval_add(x, y->cell[i]);
  176. }
  177. free(y->cell);
  178. free(y);
  179. return x;
  180. }
  181. lval* lval_pop(lval* v, int i) {
  182. lval* x = v->cell[i];
  183. memmove(&v->cell[i],
  184. &v->cell[i+1], sizeof(lval*) * (v->count-i-1));
  185. v->count--;
  186. v->cell = realloc(v->cell, sizeof(lval*) * v->count);
  187. return x;
  188. }
  189. lval* lval_take(lval* v, int i) {
  190. lval* x = lval_pop(v, i);
  191. lval_del(v);
  192. return x;
  193. }
  194. void lval_print(lval* v);
  195. void lval_print_expr(lval* v, char open, char close) {
  196. putchar(open);
  197. for (int i = 0; i < v->count; i++) {
  198. lval_print(v->cell[i]);
  199. if (i != (v->count-1)) {
  200. putchar(' ');
  201. }
  202. }
  203. putchar(close);
  204. }
  205. /* Possible unescapable characters */
  206. char* lval_str_unescapable = "abfnrtv\\\'\"";
  207. /* Function to unescape characters */
  208. char lval_str_unescape(char x) {
  209. switch (x) {
  210. case 'a': return '\a';
  211. case 'b': return '\b';
  212. case 'f': return '\f';
  213. case 'n': return '\n';
  214. case 'r': return '\r';
  215. case 't': return '\t';
  216. case 'v': return '\v';
  217. case '\\': return '\\';
  218. case '\'': return '\'';
  219. case '\"': return '\"';
  220. }
  221. return '\0';
  222. }
  223. /* List of possible escapable characters */
  224. char* lval_str_escapable = "\a\b\f\n\r\t\v\\\'\"";
  225. /* Function to escape characters */
  226. char* lval_str_escape(char x) {
  227. switch (x) {
  228. case '\a': return "\\a";
  229. case '\b': return "\\b";
  230. case '\f': return "\\f";
  231. case '\n': return "\\n";
  232. case '\r': return "\\r";
  233. case '\t': return "\\t";
  234. case '\v': return "\\v";
  235. case '\\': return "\\\\";
  236. case '\'': return "\\\'";
  237. case '\"': return "\\\"";
  238. }
  239. return "";
  240. }
  241. void lval_print_str(lval* v) {
  242. putchar('"');
  243. /* Loop over the characters in the string */
  244. for (int i = 0; i < strlen(v->str); i++) {
  245. if (strchr(lval_str_escapable, v->str[i])) {
  246. /* If the character is escapable then escape it */
  247. printf("%s", lval_str_escape(v->str[i]));
  248. } else {
  249. /* Otherwise print character as it is */
  250. putchar(v->str[i]);
  251. }
  252. }
  253. putchar('"');
  254. }
  255. void lval_print(lval* v) {
  256. switch (v->type) {
  257. case LVAL_FUN:
  258. if (v->builtin) {
  259. printf("<builtin>");
  260. } else {
  261. printf("(\\ ");
  262. lval_print(v->formals);
  263. putchar(' ');
  264. lval_print(v->body);
  265. putchar(')');
  266. }
  267. break;
  268. case LVAL_NUM: printf("%li", v->num); break;
  269. case LVAL_ERR: printf("Error: %s", v->err); break;
  270. case LVAL_SYM: printf("%s", v->sym); break;
  271. case LVAL_STR: lval_print_str(v); break;
  272. case LVAL_SEXPR: lval_print_expr(v, '(', ')'); break;
  273. case LVAL_QEXPR: lval_print_expr(v, '{', '}'); break;
  274. }
  275. }
  276. void lval_println(lval* v) { lval_print(v); putchar('\n'); }
  277. int lval_eq(lval* x, lval* y) {
  278. if (x->type != y->type) { return 0; }
  279. switch (x->type) {
  280. case LVAL_NUM: return (x->num == y->num);
  281. case LVAL_ERR: return (strcmp(x->err, y->err) == 0);
  282. case LVAL_SYM: return (strcmp(x->sym, y->sym) == 0);
  283. case LVAL_STR: return (strcmp(x->str, y->str) == 0);
  284. case LVAL_FUN:
  285. if (x->builtin || y->builtin) {
  286. return x->builtin == y->builtin;
  287. } else {
  288. return lval_eq(x->formals, y->formals) && lval_eq(x->body, y->body);
  289. }
  290. case LVAL_QEXPR:
  291. case LVAL_SEXPR:
  292. if (x->count != y->count) { return 0; }
  293. for (int i = 0; i < x->count; i++) {
  294. if (!lval_eq(x->cell[i], y->cell[i])) { return 0; }
  295. }
  296. return 1;
  297. break;
  298. }
  299. return 0;
  300. }
  301. char* ltype_name(int t) {
  302. switch(t) {
  303. case LVAL_FUN: return "Function";
  304. case LVAL_NUM: return "Number";
  305. case LVAL_ERR: return "Error";
  306. case LVAL_SYM: return "Symbol";
  307. case LVAL_STR: return "String";
  308. case LVAL_SEXPR: return "S-Expression";
  309. case LVAL_QEXPR: return "Q-Expression";
  310. default: return "Unknown";
  311. }
  312. }
  313. /* Lisp Environment */
  314. struct lenv {
  315. lenv* par;
  316. int count;
  317. char** syms;
  318. lval** vals;
  319. };
  320. lenv* lenv_new(void) {
  321. lenv* e = malloc(sizeof(lenv));
  322. e->par = NULL;
  323. e->count = 0;
  324. e->syms = NULL;
  325. e->vals = NULL;
  326. return e;
  327. }
  328. void lenv_del(lenv* e) {
  329. for (int i = 0; i < e->count; i++) {
  330. free(e->syms[i]);
  331. lval_del(e->vals[i]);
  332. }
  333. free(e->syms);
  334. free(e->vals);
  335. free(e);
  336. }
  337. lenv* lenv_copy(lenv* e) {
  338. lenv* n = malloc(sizeof(lenv));
  339. n->par = e->par;
  340. n->count = e->count;
  341. n->syms = malloc(sizeof(char*) * n->count);
  342. n->vals = malloc(sizeof(lval*) * n->count);
  343. for (int i = 0; i < e->count; i++) {
  344. n->syms[i] = malloc(strlen(e->syms[i]) + 1);
  345. strcpy(n->syms[i], e->syms[i]);
  346. n->vals[i] = lval_copy(e->vals[i]);
  347. }
  348. return n;
  349. }
  350. lval* lenv_get(lenv* e, lval* k) {
  351. for (int i = 0; i < e->count; i++) {
  352. if (strcmp(e->syms[i], k->sym) == 0) { return lval_copy(e->vals[i]); }
  353. }
  354. if (e->par) {
  355. return lenv_get(e->par, k);
  356. } else {
  357. return lval_err("Unbound Symbol '%s'", k->sym);
  358. }
  359. }
  360. void lenv_put(lenv* e, lval* k, lval* v) {
  361. for (int i = 0; i < e->count; i++) {
  362. if (strcmp(e->syms[i], k->sym) == 0) {
  363. lval_del(e->vals[i]);
  364. e->vals[i] = lval_copy(v);
  365. return;
  366. }
  367. }
  368. e->count++;
  369. e->vals = realloc(e->vals, sizeof(lval*) * e->count);
  370. e->syms = realloc(e->syms, sizeof(char*) * e->count);
  371. e->vals[e->count-1] = lval_copy(v);
  372. e->syms[e->count-1] = malloc(strlen(k->sym)+1);
  373. strcpy(e->syms[e->count-1], k->sym);
  374. }
  375. void lenv_def(lenv* e, lval* k, lval* v) {
  376. while (e->par) { e = e->par; }
  377. lenv_put(e, k, v);
  378. }
  379. /* Builtins */
  380. #define LASSERT(args, cond, fmt, ...) \
  381. if (!(cond)) { lval* err = lval_err(fmt, ##__VA_ARGS__); lval_del(args); return err; }
  382. #define LASSERT_TYPE(func, args, index, expect) \
  383. LASSERT(args, args->cell[index]->type == expect, \
  384. "Function '%s' passed incorrect type for argument %i. Got %s, Expected %s.", \
  385. func, index, ltype_name(args->cell[index]->type), ltype_name(expect))
  386. #define LASSERT_NUM(func, args, num) \
  387. LASSERT(args, args->count == num, \
  388. "Function '%s' passed incorrect number of arguments. Got %i, Expected %i.", \
  389. func, args->count, num)
  390. #define LASSERT_NOT_EMPTY(func, args, index) \
  391. LASSERT(args, args->cell[index]->count != 0, \
  392. "Function '%s' passed {} for argument %i.", func, index);
  393. lval* lval_eval(lenv* e, lval* v);
  394. lval* builtin_lambda(lenv* e, lval* a) {
  395. LASSERT_NUM("\\", a, 2);
  396. LASSERT_TYPE("\\", a, 0, LVAL_QEXPR);
  397. LASSERT_TYPE("\\", a, 1, LVAL_QEXPR);
  398. for (int i = 0; i < a->cell[0]->count; i++) {
  399. LASSERT(a, (a->cell[0]->cell[i]->type == LVAL_SYM),
  400. "Cannot define non-symbol. Got %s, Expected %s.",
  401. ltype_name(a->cell[0]->cell[i]->type), ltype_name(LVAL_SYM));
  402. }
  403. lval* formals = lval_pop(a, 0);
  404. lval* body = lval_pop(a, 0);
  405. lval_del(a);
  406. return lval_lambda(formals, body);
  407. }
  408. lval* builtin_list(lenv* e, lval* a) {
  409. a->type = LVAL_QEXPR;
  410. return a;
  411. }
  412. lval* builtin_head(lenv* e, lval* a) {
  413. LASSERT_NUM("head", a, 1);
  414. LASSERT_TYPE("head", a, 0, LVAL_QEXPR);
  415. LASSERT_NOT_EMPTY("head", a, 0);
  416. lval* v = lval_take(a, 0);
  417. while (v->count > 1) { lval_del(lval_pop(v, 1)); }
  418. return v;
  419. }
  420. lval* builtin_tail(lenv* e, lval* a) {
  421. LASSERT_NUM("tail", a, 1);
  422. LASSERT_TYPE("tail", a, 0, LVAL_QEXPR);
  423. LASSERT_NOT_EMPTY("tail", a, 0);
  424. lval* v = lval_take(a, 0);
  425. lval_del(lval_pop(v, 0));
  426. return v;
  427. }
  428. lval* builtin_eval(lenv* e, lval* a) {
  429. LASSERT_NUM("eval", a, 1);
  430. LASSERT_TYPE("eval", a, 0, LVAL_QEXPR);
  431. lval* x = lval_take(a, 0);
  432. x->type = LVAL_SEXPR;
  433. return lval_eval(e, x);
  434. }
  435. lval* builtin_join(lenv* e, lval* a) {
  436. for (int i = 0; i < a->count; i++) {
  437. LASSERT_TYPE("join", a, i, LVAL_QEXPR);
  438. }
  439. lval* x = lval_pop(a, 0);
  440. while (a->count) {
  441. lval* y = lval_pop(a, 0);
  442. x = lval_join(x, y);
  443. }
  444. lval_del(a);
  445. return x;
  446. }
  447. lval* builtin_op(lenv* e, lval* a, char* op) {
  448. for (int i = 0; i < a->count; i++) {
  449. LASSERT_TYPE(op, a, i, LVAL_NUM);
  450. }
  451. lval* x = lval_pop(a, 0);
  452. if ((strcmp(op, "-") == 0) && a->count == 0) { x->num = -x->num; }
  453. while (a->count > 0) {
  454. lval* y = lval_pop(a, 0);
  455. if (strcmp(op, "+") == 0) { x->num += y->num; }
  456. if (strcmp(op, "-") == 0) { x->num -= y->num; }
  457. if (strcmp(op, "*") == 0) { x->num *= y->num; }
  458. if (strcmp(op, "/") == 0) {
  459. if (y->num == 0) {
  460. lval_del(x); lval_del(y);
  461. x = lval_err("Division By Zero.");
  462. break;
  463. }
  464. x->num /= y->num;
  465. }
  466. lval_del(y);
  467. }
  468. lval_del(a);
  469. return x;
  470. }
  471. lval* builtin_add(lenv* e, lval* a) { return builtin_op(e, a, "+"); }
  472. lval* builtin_sub(lenv* e, lval* a) { return builtin_op(e, a, "-"); }
  473. lval* builtin_mul(lenv* e, lval* a) { return builtin_op(e, a, "*"); }
  474. lval* builtin_div(lenv* e, lval* a) { return builtin_op(e, a, "/"); }
  475. lval* builtin_var(lenv* e, lval* a, char* func) {
  476. LASSERT_TYPE(func, a, 0, LVAL_QEXPR);
  477. lval* syms = a->cell[0];
  478. for (int i = 0; i < syms->count; i++) {
  479. LASSERT(a, (syms->cell[i]->type == LVAL_SYM),
  480. "Function '%s' cannot define non-symbol. "
  481. "Got %s, Expected %s.",
  482. func, ltype_name(syms->cell[i]->type), ltype_name(LVAL_SYM));
  483. }
  484. LASSERT(a, (syms->count == a->count-1),
  485. "Function '%s' passed too many arguments for symbols. "
  486. "Got %i, Expected %i.",
  487. func, syms->count, a->count-1);
  488. for (int i = 0; i < syms->count; i++) {
  489. if (strcmp(func, "def") == 0) { lenv_def(e, syms->cell[i], a->cell[i+1]); }
  490. if (strcmp(func, "=") == 0) { lenv_put(e, syms->cell[i], a->cell[i+1]); }
  491. }
  492. lval_del(a);
  493. return lval_sexpr();
  494. }
  495. lval* builtin_def(lenv* e, lval* a) { return builtin_var(e, a, "def"); }
  496. lval* builtin_put(lenv* e, lval* a) { return builtin_var(e, a, "="); }
  497. lval* builtin_ord(lenv* e, lval* a, char* op) {
  498. LASSERT_NUM(op, a, 2);
  499. LASSERT_TYPE(op, a, 0, LVAL_NUM);
  500. LASSERT_TYPE(op, a, 1, LVAL_NUM);
  501. int r;
  502. if (strcmp(op, ">") == 0) { r = (a->cell[0]->num > a->cell[1]->num); }
  503. if (strcmp(op, "<") == 0) { r = (a->cell[0]->num < a->cell[1]->num); }
  504. if (strcmp(op, ">=") == 0) { r = (a->cell[0]->num >= a->cell[1]->num); }
  505. if (strcmp(op, "<=") == 0) { r = (a->cell[0]->num <= a->cell[1]->num); }
  506. lval_del(a);
  507. return lval_num(r);
  508. }
  509. lval* builtin_gt(lenv* e, lval* a) { return builtin_ord(e, a, ">"); }
  510. lval* builtin_lt(lenv* e, lval* a) { return builtin_ord(e, a, "<"); }
  511. lval* builtin_ge(lenv* e, lval* a) { return builtin_ord(e, a, ">="); }
  512. lval* builtin_le(lenv* e, lval* a) { return builtin_ord(e, a, "<="); }
  513. lval* builtin_cmp(lenv* e, lval* a, char* op) {
  514. LASSERT_NUM(op, a, 2);
  515. int r;
  516. if (strcmp(op, "==") == 0) { r = lval_eq(a->cell[0], a->cell[1]); }
  517. if (strcmp(op, "!=") == 0) { r = !lval_eq(a->cell[0], a->cell[1]); }
  518. lval_del(a);
  519. return lval_num(r);
  520. }
  521. lval* builtin_eq(lenv* e, lval* a) { return builtin_cmp(e, a, "=="); }
  522. lval* builtin_ne(lenv* e, lval* a) { return builtin_cmp(e, a, "!="); }
  523. lval* builtin_if(lenv* e, lval* a) {
  524. LASSERT_NUM("if", a, 3);
  525. LASSERT_TYPE("if", a, 0, LVAL_NUM);
  526. LASSERT_TYPE("if", a, 1, LVAL_QEXPR);
  527. LASSERT_TYPE("if", a, 2, LVAL_QEXPR);
  528. lval* x;
  529. a->cell[1]->type = LVAL_SEXPR;
  530. a->cell[2]->type = LVAL_SEXPR;
  531. if (a->cell[0]->num) {
  532. x = lval_eval(e, lval_pop(a, 1));
  533. } else {
  534. x = lval_eval(e, lval_pop(a, 2));
  535. }
  536. lval_del(a);
  537. return x;
  538. }
  539. /* Change forward declaration */
  540. lval* lval_read_expr(char* s, int* i, char end);
  541. lval* builtin_load(lenv* e, lval* a) {
  542. LASSERT_NUM("load", a, 1);
  543. LASSERT_TYPE("load", a, 0, LVAL_STR);
  544. /* Open file and check it exists */
  545. FILE* f = fopen(a->cell[0]->str, "rb");
  546. if (f == NULL) {
  547. lval* err = lval_err("Could not load Library %s", a->cell[0]->str);
  548. lval_del(a);
  549. return err;
  550. }
  551. /* Read File Contents */
  552. fseek(f, 0, SEEK_END);
  553. long length = ftell(f);
  554. fseek(f, 0, SEEK_SET);
  555. char* input = calloc(length+1, 1);
  556. fread(input, 1, length, f);
  557. fclose(f);
  558. /* Read from input to create an S-Expr */
  559. int pos = 0;
  560. lval* expr = lval_read_expr(input, &pos, '\0');
  561. free(input);
  562. /* Evaluate all expressions contained in S-Expr */
  563. if (expr->type != LVAL_ERR) {
  564. while (expr->count) {
  565. lval* x = lval_eval(e, lval_pop(expr, 0));
  566. if (x->type == LVAL_ERR) { lval_println(x); }
  567. lval_del(x);
  568. }
  569. } else {
  570. lval_println(expr);
  571. }
  572. lval_del(expr);
  573. lval_del(a);
  574. return lval_sexpr();
  575. }
  576. lval* builtin_print(lenv* e, lval* a) {
  577. for (int i = 0; i < a->count; i++) {
  578. lval_print(a->cell[i]); putchar(' ');
  579. }
  580. putchar('\n');
  581. lval_del(a);
  582. return lval_sexpr();
  583. }
  584. lval* builtin_error(lenv* e, lval* a) {
  585. LASSERT_NUM("error", a, 1);
  586. LASSERT_TYPE("error", a, 0, LVAL_STR);
  587. lval* err = lval_err(a->cell[0]->str);
  588. lval_del(a);
  589. return err;
  590. }
  591. void lenv_add_builtin(lenv* e, char* name, lbuiltin func) {
  592. lval* k = lval_sym(name);
  593. lval* v = lval_builtin(func);
  594. lenv_put(e, k, v);
  595. lval_del(k); lval_del(v);
  596. }
  597. void lenv_add_builtins(lenv* e) {
  598. /* Variable Functions */
  599. lenv_add_builtin(e, "\\", builtin_lambda);
  600. lenv_add_builtin(e, "def", builtin_def);
  601. lenv_add_builtin(e, "=", builtin_put);
  602. /* List Functions */
  603. lenv_add_builtin(e, "list", builtin_list);
  604. lenv_add_builtin(e, "head", builtin_head);
  605. lenv_add_builtin(e, "tail", builtin_tail);
  606. lenv_add_builtin(e, "eval", builtin_eval);
  607. lenv_add_builtin(e, "join", builtin_join);
  608. /* Mathematical Functions */
  609. lenv_add_builtin(e, "+", builtin_add);
  610. lenv_add_builtin(e, "-", builtin_sub);
  611. lenv_add_builtin(e, "*", builtin_mul);
  612. lenv_add_builtin(e, "/", builtin_div);
  613. /* Comparison Functions */
  614. lenv_add_builtin(e, "if", builtin_if);
  615. lenv_add_builtin(e, "==", builtin_eq);
  616. lenv_add_builtin(e, "!=", builtin_ne);
  617. lenv_add_builtin(e, ">", builtin_gt);
  618. lenv_add_builtin(e, "<", builtin_lt);
  619. lenv_add_builtin(e, ">=", builtin_ge);
  620. lenv_add_builtin(e, "<=", builtin_le);
  621. /* String Functions */
  622. lenv_add_builtin(e, "load", builtin_load);
  623. lenv_add_builtin(e, "error", builtin_error);
  624. lenv_add_builtin(e, "print", builtin_print);
  625. }
  626. /* Evaluation */
  627. lval* lval_call(lenv* e, lval* f, lval* a) {
  628. if (f->builtin) { return f->builtin(e, a); }
  629. int given = a->count;
  630. int total = f->formals->count;
  631. while (a->count) {
  632. if (f->formals->count == 0) {
  633. lval_del(a);
  634. return lval_err("Function passed too many arguments. "
  635. "Got %i, Expected %i.", given, total);
  636. }
  637. lval* sym = lval_pop(f->formals, 0);
  638. if (strcmp(sym->sym, "&") == 0) {
  639. if (f->formals->count != 1) {
  640. lval_del(a);
  641. return lval_err("Function format invalid. "
  642. "Symbol '&' not followed by single symbol.");
  643. }
  644. lval* nsym = lval_pop(f->formals, 0);
  645. lenv_put(f->env, nsym, builtin_list(e, a));
  646. lval_del(sym); lval_del(nsym);
  647. break;
  648. }
  649. lval* val = lval_pop(a, 0);
  650. lenv_put(f->env, sym, val);
  651. lval_del(sym); lval_del(val);
  652. }
  653. lval_del(a);
  654. if (f->formals->count > 0 &&
  655. strcmp(f->formals->cell[0]->sym, "&") == 0) {
  656. if (f->formals->count != 2) {
  657. return lval_err("Function format invalid. "
  658. "Symbol '&' not followed by single symbol.");
  659. }
  660. lval_del(lval_pop(f->formals, 0));
  661. lval* sym = lval_pop(f->formals, 0);
  662. lval* val = lval_qexpr();
  663. lenv_put(f->env, sym, val);
  664. lval_del(sym); lval_del(val);
  665. }
  666. if (f->formals->count == 0) {
  667. f->env->par = e;
  668. return builtin_eval(f->env, lval_add(lval_sexpr(), lval_copy(f->body)));
  669. } else {
  670. return lval_copy(f);
  671. }
  672. }
  673. lval* lval_eval_sexpr(lenv* e, lval* v) {
  674. for (int i = 0; i < v->count; i++) { v->cell[i] = lval_eval(e, v->cell[i]); }
  675. for (int i = 0; i < v->count; i++) { if (v->cell[i]->type == LVAL_ERR) { return lval_take(v, i); } }
  676. if (v->count == 0) { return v; }
  677. if (v->count == 1) { return lval_eval(e, lval_take(v, 0)); }
  678. lval* f = lval_pop(v, 0);
  679. if (f->type != LVAL_FUN) {
  680. lval* err = lval_err(
  681. "S-Expression starts with incorrect type. "
  682. "Got %s, Expected %s.",
  683. ltype_name(f->type), ltype_name(LVAL_FUN));
  684. lval_del(f); lval_del(v);
  685. return err;
  686. }
  687. lval* result = lval_call(e, f, v);
  688. lval_del(f);
  689. return result;
  690. }
  691. lval* lval_eval(lenv* e, lval* v) {
  692. if (v->type == LVAL_SYM) {
  693. lval* x = lenv_get(e, v);
  694. lval_del(v);
  695. return x;
  696. }
  697. if (v->type == LVAL_SEXPR) { return lval_eval_sexpr(e, v); }
  698. return v;
  699. }
  700. /* Reading */
  701. lval* lval_read_sym(char* s, int* i) {
  702. /* Allocate Empty String */
  703. char* part = calloc(1,1);
  704. /* While valid identifier characters */
  705. while (strchr(
  706. "abcdefghijklmnopqrstuvwxyz"
  707. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  708. "0123456789_+-*\\/=<>!&", s[*i]) && s[*i] != '\0') {
  709. /* Append character to end of string */
  710. part = realloc(part, strlen(part)+2);
  711. part[strlen(part)+1] = '\0';
  712. part[strlen(part)+0] = s[*i];
  713. (*i)++;
  714. }
  715. /* Check if Identifier looks like number */
  716. int is_num = strchr("-0123456789", part[0]) != NULL;
  717. for (int j = 1; j < strlen(part); j++) {
  718. if (strchr("0123456789", part[j]) == NULL) { is_num = 0; break; }
  719. }
  720. if (strlen(part) == 1 && part[0] == '-') { is_num = 0; }
  721. /* Add Symbol or Number as lval */
  722. lval* x = NULL;
  723. if (is_num) {
  724. errno = 0;
  725. long v = strtol(part, NULL, 10);
  726. x = (errno != ERANGE) ? lval_num(v) : lval_err("Invalid Number %s", part);
  727. } else {
  728. x = lval_sym(part);
  729. }
  730. /* Free temp string */
  731. free(part);
  732. /* Return lval */
  733. return x;
  734. }
  735. lval* lval_read_str(char* s, int* i) {
  736. /* Allocate empty string */
  737. char* part = calloc(1,1);
  738. /* More forward one step past initial " character */
  739. (*i)++;
  740. while (s[*i] != '"') {
  741. char c = s[*i];
  742. /* If end of input then there is an unterminated string literal */
  743. if (c == '\0') {
  744. free(part);
  745. return lval_err("Unexpected end of input");
  746. }
  747. /* If backslash then unescape character after it */
  748. if (c == '\\') {
  749. (*i)++;
  750. /* Check next character is escapable */
  751. if (strchr(lval_str_unescapable, s[*i])) {
  752. c = lval_str_unescape(s[*i]);
  753. } else {
  754. free(part);
  755. return lval_err("Invalid escape sequence \\%c", s[*i]);
  756. }
  757. }
  758. /* Append character to string */
  759. part = realloc(part, strlen(part)+2);
  760. part[strlen(part)+1] = '\0';
  761. part[strlen(part)+0] = c;
  762. (*i)++;
  763. }
  764. /* Move forward past final " character */
  765. (*i)++;
  766. lval* x = lval_str(part);
  767. /* free temp string */
  768. free(part);
  769. return x;
  770. }
  771. lval* lval_read(char* s, int* i);
  772. lval* lval_read_expr(char* s, int* i, char end) {
  773. /* Either create new qexpr or sexpr */
  774. lval* x = (end == '}') ? lval_qexpr() : lval_sexpr();
  775. /* While not at end character keep reading lvals */
  776. while (s[*i] != end) {
  777. lval* y = lval_read(s, i);
  778. /* If an error then return this and stop */
  779. if (y->type == LVAL_ERR) {
  780. lval_del(x);
  781. return y;
  782. } else {
  783. lval_add(x, y);
  784. }
  785. }
  786. /* Move past end character */
  787. (*i)++;
  788. return x;
  789. }
  790. lval* lval_read(char* s, int* i) {
  791. /* Skip all trailing whitespace and comments */
  792. while (strchr(" \t\v\r\n;", s[*i]) && s[*i] != '\0') {
  793. if (s[*i] == ';') {
  794. while (s[*i] != '\n' && s[*i] != '\0') { (*i)++; }
  795. }
  796. (*i)++;
  797. }
  798. lval* x = NULL;
  799. /* If we reach end of input then we're missing something */
  800. if (s[*i] == '\0') {
  801. return lval_err("Unexpected end of input");
  802. }
  803. /* If next character is ( then read S-Expr */
  804. else if (s[*i] == '(') {
  805. (*i)++;
  806. x = lval_read_expr(s, i, ')');
  807. }
  808. /* If next character is { then read Q-Expr */
  809. else if (s[*i] == '{') {
  810. (*i)++;
  811. x = lval_read_expr(s, i, '}');
  812. }
  813. /* If next character is part of a symbol then read symbol */
  814. else if (strchr(
  815. "abcdefghijklmnopqrstuvwxyz"
  816. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  817. "0123456789_+-*\\/=<>!&", s[*i])) {
  818. x = lval_read_sym(s, i);
  819. }
  820. /* If next character is " then read string */
  821. else if (strchr("\"", s[*i])) {
  822. x = lval_read_str(s, i);
  823. }
  824. /* Encountered some unexpected character */
  825. else {
  826. x = lval_err("Unexpected character %c", s[*i]);
  827. }
  828. /* Skip all trailing whitespace and comments */
  829. while (strchr(" \t\v\r\n;", s[*i]) && s[*i] != '\0') {
  830. if (s[*i] == ';') {
  831. while (s[*i] != '\n' && s[*i] != '\0') { (*i)++; }
  832. }
  833. (*i)++;
  834. }
  835. return x;
  836. }
  837. /* Main */
  838. int main(int argc, char** argv) {
  839. lenv* e = lenv_new();
  840. lenv_add_builtins(e);
  841. /* Interactive Prompt */
  842. if (argc == 1) {
  843. puts("Lispy Version 0.0.0.1.1");
  844. puts("Press Ctrl+c to Exit\n");
  845. while (1) {
  846. char* input = readline("lispy> ");
  847. add_history(input);
  848. /* Read from input to create an S-Expr */
  849. int pos = 0;
  850. lval* expr = lval_read_expr(input, &pos, '\0');
  851. /* Evaluate and print input */
  852. lval* x = lval_eval(e, expr);
  853. lval_println(x);
  854. lval_del(x);
  855. free(input);
  856. }
  857. }
  858. /* Supplied with list of files */
  859. if (argc >= 2) {
  860. for (int i = 1; i < argc; i++) {
  861. lval* args = lval_add(lval_sexpr(), lval_str(argv[i]));
  862. lval* x = builtin_load(e, args);
  863. if (x->type == LVAL_ERR) { lval_println(x); }
  864. lval_del(x);
  865. }
  866. }
  867. lenv_del(e);
  868. return 0;
  869. }