lisp_eval.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "lisp.h"
  18. /* Support functions */
  19. struct cell* findsym(char *name)
  20. {
  21. struct cell* symlist;
  22. for(symlist = all_symbols; nil != symlist; symlist = symlist->cdr)
  23. {
  24. if(match(name, symlist->car->string))
  25. {
  26. return symlist;
  27. }
  28. }
  29. return nil;
  30. }
  31. struct cell* make_sym(char* name);
  32. struct cell* intern(char *name)
  33. {
  34. struct cell* op = findsym(name);
  35. if(nil != op) return op->car;
  36. op = make_sym(name);
  37. all_symbols = make_cons(op, all_symbols);
  38. return op;
  39. }
  40. /*** Environment ***/
  41. struct cell* extend(struct cell* env, struct cell* symbol, struct cell* value)
  42. {
  43. return make_cons(make_cons(symbol, value), env);
  44. }
  45. struct cell* multiple_extend(struct cell* env, struct cell* syms, struct cell* vals)
  46. {
  47. if(nil == syms)
  48. {
  49. return env;
  50. }
  51. return multiple_extend(extend(env, syms->car, vals->car), syms->cdr, vals->cdr);
  52. }
  53. struct cell* extend_env(struct cell* sym, struct cell* val, struct cell* env)
  54. {
  55. env->cdr = make_cons(env->car, env->cdr);
  56. env->car = make_cons(sym, val);
  57. return val;
  58. }
  59. struct cell* assoc(struct cell* key, struct cell* alist)
  60. {
  61. if(nil == alist) return nil;
  62. for(; nil != alist; alist = alist->cdr)
  63. {
  64. if(alist->car->car->string == key->string) return alist->car;
  65. }
  66. return nil;
  67. }
  68. /*** Evaluator (Eval/Apply) ***/
  69. struct cell* eval(struct cell* exp, struct cell* env);
  70. struct cell* make_proc(struct cell* a, struct cell* b, struct cell* env);
  71. struct cell* evlis(struct cell* exps, struct cell* env)
  72. {
  73. if(exps == nil) return nil;
  74. return make_cons(eval(exps->car, env), evlis(exps->cdr, env));
  75. }
  76. struct cell* progn(struct cell* exps, struct cell* env)
  77. {
  78. if(exps == nil) return nil;
  79. struct cell* result;
  80. progn_reset:
  81. result = eval(exps->car, env);
  82. if(exps->cdr == nil) return result;
  83. exps = exps->cdr;
  84. goto progn_reset;
  85. }
  86. struct cell* exec_func(FUNCTION * func, struct cell* vals)
  87. {
  88. return func(vals);
  89. }
  90. struct cell* apply(struct cell* proc, struct cell* vals)
  91. {
  92. struct cell* temp = nil;
  93. if(proc->type == PRIMOP)
  94. {
  95. temp = exec_func(proc->function, vals);
  96. }
  97. else if(proc->type == PROC)
  98. {
  99. struct cell* env = make_cons(proc->env->car, proc->env->cdr);
  100. temp = progn(proc->cdr, multiple_extend(env, proc->car, vals));
  101. }
  102. else
  103. {
  104. file_print("Bad argument to apply\n", stderr);
  105. exit(EXIT_FAILURE);
  106. }
  107. return temp;
  108. }
  109. struct cell* evcond(struct cell* exp, struct cell* env)
  110. {
  111. /* Return nil but the result is technically undefined per the standard */
  112. if(nil == exp)
  113. {
  114. return nil;
  115. }
  116. if(tee == eval(exp->car->car, env))
  117. {
  118. return eval(exp->car->cdr->car, env);
  119. }
  120. return evcond(exp->cdr, env);
  121. }
  122. void garbage_collect();
  123. struct cell* evwhile(struct cell* exp, struct cell* env)
  124. {
  125. if(nil == exp) return nil;
  126. struct cell* conditional = eval(exp->cdr->car, env);
  127. while(tee == conditional)
  128. {
  129. eval(exp->cdr->cdr->car, env);
  130. conditional = eval(exp->cdr->car, env);
  131. if((tee == exp->cdr->car) && (left_to_take < 1000)) garbage_collect();
  132. }
  133. return conditional;
  134. }
  135. struct cell* process_sym(struct cell* exp, struct cell* env);
  136. struct cell* process_cons(struct cell* exp, struct cell* env);
  137. struct cell* eval(struct cell* exp, struct cell* env)
  138. {
  139. if(exp == nil) return nil;
  140. if(SYM == exp->type) return process_sym(exp, env);
  141. if(CONS == exp->type) return process_cons(exp, env);
  142. return exp;
  143. }
  144. struct cell* process_sym(struct cell* exp, struct cell* env)
  145. {
  146. struct cell* tmp = assoc(exp, env);
  147. if(tmp == nil)
  148. {
  149. file_print("Unbound symbol:", stderr);
  150. file_print(exp->string, stderr);
  151. fputc('\n', stderr);
  152. exit(EXIT_FAILURE);
  153. }
  154. return tmp->cdr;
  155. }
  156. struct cell* process_if(struct cell* exp, struct cell* env)
  157. {
  158. if(eval(exp->cdr->car, env) != nil)
  159. {
  160. return eval(exp->cdr->cdr->car, env);
  161. }
  162. return eval(exp->cdr->cdr->cdr->car, env);
  163. }
  164. struct cell* process_setb(struct cell* exp, struct cell* env)
  165. {
  166. struct cell* newval = eval(exp->cdr->cdr->car, env);
  167. struct cell* pair = assoc(exp->cdr->car, env);
  168. pair->cdr = newval;
  169. return newval;
  170. }
  171. struct cell* process_let(struct cell* exp, struct cell* env)
  172. {
  173. struct cell* lets;
  174. for(lets = exp->cdr->car; lets != nil; lets = lets->cdr)
  175. {
  176. env = make_cons(make_cons(lets->car->car, eval(lets->car->cdr->car, env)), env);
  177. }
  178. return progn(exp->cdr->cdr, env);
  179. }
  180. struct cell* process_cons(struct cell* exp, struct cell* env)
  181. {
  182. if(exp->car == s_if) return process_if(exp, env);
  183. if(exp->car == s_cond) return evcond(exp->cdr, env);
  184. if(exp->car == s_begin) return progn(exp->cdr, env);
  185. if(exp->car == s_lambda) return make_proc(exp->cdr->car, exp->cdr->cdr, env);
  186. if(exp->car == quote) return exp->cdr->car;
  187. if(exp->car == s_define) return(extend_env(exp->cdr->car, eval(exp->cdr->cdr->car, env), env));
  188. if(exp->car == s_setb) return process_setb(exp, env);
  189. if(exp->car == s_let) return process_let(exp, env);
  190. if(exp->car == s_while) return evwhile(exp, env);
  191. return apply(eval(exp->car, env), evlis(exp->cdr, env));
  192. }
  193. /*** Primitives ***/
  194. struct cell* prim_apply(struct cell* args)
  195. {
  196. return apply(args->car, args->cdr->car);
  197. }
  198. struct cell* nullp(struct cell* args)
  199. {
  200. if(nil == args->car) return tee;
  201. return nil;
  202. }
  203. struct cell* make_int(int a);
  204. struct cell* prim_sum(struct cell* args)
  205. {
  206. if(nil == args) return nil;
  207. int sum;
  208. for(sum = 0; nil != args; args = args->cdr)
  209. {
  210. sum = sum + args->car->value;
  211. }
  212. return make_int(sum);
  213. }
  214. struct cell* prim_sub(struct cell* args)
  215. {
  216. if(nil == args) return nil;
  217. int sum = args->car->value;
  218. for(args = args->cdr; nil != args; args = args->cdr)
  219. {
  220. sum = sum - args->car->value;
  221. }
  222. return make_int(sum);
  223. }
  224. struct cell* prim_prod(struct cell* args)
  225. {
  226. if(nil == args) return nil;
  227. int prod;
  228. for(prod = 1; nil != args; args = args->cdr)
  229. {
  230. prod = prod * args->car->value;
  231. }
  232. return make_int(prod);
  233. }
  234. struct cell* prim_div(struct cell* args)
  235. {
  236. if(nil == args) return make_int(1);
  237. int div = args->car->value;
  238. for(args = args->cdr; nil != args; args = args->cdr)
  239. {
  240. div = div / args->car->value;
  241. }
  242. return make_int(div);
  243. }
  244. struct cell* prim_mod(struct cell* args)
  245. {
  246. if(nil == args) return nil;
  247. int mod = args->car->value % args->cdr->car->value;
  248. if(nil != args->cdr->cdr)
  249. {
  250. file_print("wrong number of arguments to mod\n", stderr);
  251. exit(EXIT_FAILURE);
  252. }
  253. return make_int(mod);
  254. }
  255. struct cell* prim_and(struct cell* args)
  256. {
  257. if(nil == args) return nil;
  258. for(; nil != args; args = args->cdr)
  259. {
  260. if(tee != args->car) return nil;
  261. }
  262. return tee;
  263. }
  264. struct cell* prim_or(struct cell* args)
  265. {
  266. if(nil == args) return nil;
  267. for(; nil != args; args = args->cdr)
  268. {
  269. if(tee == args->car) return tee;
  270. }
  271. return nil;
  272. }
  273. struct cell* prim_not(struct cell* args)
  274. {
  275. if(nil == args) return nil;
  276. if(tee != args->car) return tee;
  277. return nil;
  278. }
  279. struct cell* prim_numgt(struct cell* args)
  280. {
  281. if(nil == args) return nil;
  282. int temp = args->car->value;
  283. for(args = args->cdr; nil != args; args = args->cdr)
  284. {
  285. if(temp <= args->car->value)
  286. {
  287. return nil;
  288. }
  289. temp = args->car->value;
  290. }
  291. return tee;
  292. }
  293. struct cell* prim_numge(struct cell* args)
  294. {
  295. if(nil == args) return nil;
  296. int temp = args->car->value;
  297. for(args = args->cdr; nil != args; args = args->cdr)
  298. {
  299. if(temp < args->car->value)
  300. {
  301. return nil;
  302. }
  303. temp = args->car->value;
  304. }
  305. return tee;
  306. }
  307. struct cell* prim_numeq(struct cell* args)
  308. {
  309. if(nil == args) return nil;
  310. int temp = args->car->value;
  311. for(args = args->cdr; nil != args; args = args->cdr)
  312. {
  313. if(temp != args->car->value)
  314. {
  315. return nil;
  316. }
  317. }
  318. return tee;
  319. }
  320. struct cell* prim_numle(struct cell* args)
  321. {
  322. if(nil == args) return nil;
  323. int temp = args->car->value;
  324. for(args = args->cdr; nil != args; args = args->cdr)
  325. {
  326. if(temp > args->car->value)
  327. {
  328. return nil;
  329. }
  330. temp = args->car->value;
  331. }
  332. return tee;
  333. }
  334. struct cell* prim_numlt(struct cell* args)
  335. {
  336. if(nil == args) return nil;
  337. int temp = args->car->value;
  338. for(args = args->cdr; nil != args; args = args->cdr)
  339. {
  340. if(temp >= args->car->value)
  341. {
  342. return nil;
  343. }
  344. temp = args->car->value;
  345. }
  346. return tee;
  347. }
  348. struct cell* prim_listp(struct cell* args)
  349. {
  350. if(nil == args) return nil;
  351. if(CONS == args->car->type)
  352. {
  353. return tee;
  354. }
  355. return nil;
  356. }
  357. struct cell* prim_get_type(struct cell* args)
  358. {
  359. if(nil == args) return nil;
  360. return make_int(args->car->type);
  361. }
  362. struct cell* make_cell(int type, struct cell* a, struct cell* b, struct cell* env);
  363. struct cell* prim_set_type(struct cell* args)
  364. {
  365. if(nil == args) return nil;
  366. return make_cell(args->cdr->car->value, args->car->car, args->car->cdr, args->car->env);
  367. }
  368. struct cell* prim_output(struct cell* args, FILE* out)
  369. {
  370. for(; nil != args; args = args->cdr)
  371. {
  372. if(INT == args->car->type)
  373. {
  374. file_print(numerate_number(args->car->value), out);
  375. }
  376. else if(CHAR == args->car->type)
  377. {
  378. fputc(args->car->value, out);
  379. }
  380. else if(CONS == args->car->type)
  381. {
  382. prim_output(args->car, out);
  383. }
  384. else
  385. {
  386. file_print(args->car->string, out);
  387. }
  388. }
  389. return tee;
  390. }
  391. struct cell* prim_stringeq(struct cell* args)
  392. {
  393. if(nil == args) return nil;
  394. char* temp = args->car->string;
  395. for(args = args->cdr; nil != args; args = args->cdr)
  396. {
  397. if(!match(temp, args->car->string))
  398. {
  399. return nil;
  400. }
  401. }
  402. return tee;
  403. }
  404. struct cell* prim_display(struct cell* args)
  405. {
  406. return prim_output(args, console_output);
  407. }
  408. struct cell* prim_write(struct cell* args)
  409. {
  410. return prim_output(args, file_output);
  411. }
  412. struct cell* prim_freecell(struct cell* args)
  413. {
  414. if(nil == args)
  415. {
  416. file_print("Remaining Cells: ", stdout);
  417. file_print(numerate_number(left_to_take), stdout);
  418. return nil;
  419. }
  420. return make_int(left_to_take);
  421. }
  422. struct cell* make_char(int a);
  423. struct cell* string_to_list(char* string)
  424. {
  425. if(NULL == string) return nil;
  426. if(0 == string[0]) return nil;
  427. struct cell* result = make_char(string[0]);
  428. struct cell* tail = string_to_list(string + 1);
  429. return make_cons(result, tail);
  430. }
  431. struct cell* prim_string_to_list(struct cell* args)
  432. {
  433. if(nil == args) return nil;
  434. if(STRING == args->car->type)
  435. {
  436. return string_to_list(args->car->string);
  437. }
  438. return nil;
  439. }
  440. struct cell* make_string(char* a);
  441. int list_to_string(int index, char* string, struct cell* args)
  442. {
  443. struct cell* i;
  444. for(i = args; nil != i; i = i->cdr)
  445. {
  446. if(CHAR == i->car->type)
  447. {
  448. string[index] = i->car->value;
  449. index = index + 1;
  450. }
  451. if(CONS == i->car->type)
  452. {
  453. index = list_to_string(index, string, i->car);
  454. }
  455. }
  456. return index;
  457. }
  458. struct cell* prim_list_to_string(struct cell* args)
  459. {
  460. if(nil == args) return nil;
  461. char* string = calloc(MAX_STRING + 2, sizeof(char));
  462. list_to_string(0, string, args);
  463. return make_string(string);
  464. }
  465. struct cell* prim_echo(struct cell* args)
  466. {
  467. if(nil == args) return nil;
  468. if(nil == args->car) echo = FALSE;
  469. if(tee == args->car)
  470. {
  471. echo = TRUE;
  472. return make_string("");
  473. }
  474. return args->car;
  475. }
  476. struct cell* prim_read_byte(struct cell* args)
  477. {
  478. if(nil == args) return make_char(fgetc(input));
  479. return nil;
  480. }
  481. struct cell* prim_halt(struct cell* args)
  482. {
  483. /* Cleanup */
  484. free(args);
  485. fclose(file_output);
  486. /* Actual important part */
  487. exit(EXIT_SUCCESS);
  488. }
  489. struct cell* prim_list(struct cell* args) {return args;}
  490. struct cell* prim_cons(struct cell* args) { return make_cons(args->car, args->cdr->car); }
  491. struct cell* prim_car(struct cell* args) { return args->car->car; }
  492. struct cell* prim_cdr(struct cell* args) { return args->car->cdr; }
  493. void spinup(struct cell* sym, struct cell* prim)
  494. {
  495. all_symbols = make_cons(sym, all_symbols);
  496. top_env = extend(top_env, sym, prim);
  497. }
  498. /*** Initialization ***/
  499. struct cell* intern(char *name);
  500. struct cell* make_prim(void* fun);
  501. struct cell* make_sym(char* name);
  502. void init_sl3()
  503. {
  504. /* Special symbols */
  505. nil = make_sym("nil");
  506. tee = make_sym("#t");
  507. quote = make_sym("quote");
  508. s_if = make_sym("if");
  509. s_cond = make_sym("cond");
  510. s_lambda = make_sym("lambda");
  511. s_define = make_sym("define");
  512. s_setb = make_sym("set!");
  513. s_begin = make_sym("begin");
  514. s_let = make_sym("let");
  515. s_while = make_sym("while");
  516. /* Globals of interest */
  517. all_symbols = make_cons(nil, nil);
  518. top_env = extend(nil, nil, nil);
  519. /* Add Eval Specials */
  520. spinup(tee, tee);
  521. spinup(quote, quote);
  522. spinup(s_if, s_if);
  523. spinup(s_cond, s_cond);
  524. spinup(s_lambda, s_lambda);
  525. spinup(s_define, s_define);
  526. spinup(s_setb, s_setb);
  527. spinup(s_begin, s_begin);
  528. spinup(s_let, s_let);
  529. spinup(s_while, s_while);
  530. /* Add Primitive Specials */
  531. spinup(make_sym("apply"), make_prim(prim_apply));
  532. spinup(make_sym("null?"), make_prim(nullp));
  533. spinup(make_sym("+"), make_prim(prim_sum));
  534. spinup(make_sym("-"), make_prim(prim_sub));
  535. spinup(make_sym("*"), make_prim(prim_prod));
  536. spinup(make_sym("/"), make_prim(prim_div));
  537. spinup(make_sym("mod"), make_prim(prim_mod));
  538. spinup(make_sym("and"), make_prim(prim_and));
  539. spinup(make_sym("or"), make_prim(prim_or));
  540. spinup(make_sym("not"), make_prim(prim_not));
  541. spinup(make_sym(">"), make_prim(prim_numgt));
  542. spinup(make_sym(">="), make_prim(prim_numge));
  543. spinup(make_sym("="), make_prim(prim_numeq));
  544. spinup(make_sym("<="), make_prim(prim_numle));
  545. spinup(make_sym("<"), make_prim(prim_numlt));
  546. spinup(make_sym("display"), make_prim(prim_display));
  547. spinup(make_sym("write"), make_prim(prim_write));
  548. spinup(make_sym("free_mem"), make_prim(prim_freecell));
  549. spinup(make_sym("get-type"), make_prim(prim_get_type));
  550. spinup(make_sym("set-type!"), make_prim(prim_set_type));
  551. spinup(make_sym("list?"), make_prim(prim_listp));
  552. spinup(make_sym("list"), make_prim(prim_list));
  553. spinup(make_sym("list->string"), make_prim(prim_list_to_string));
  554. spinup(make_sym("string->list"), make_prim(prim_string_to_list));
  555. spinup(make_sym("string=?"), make_prim(prim_stringeq));
  556. spinup(make_sym("cons"), make_prim(prim_cons));
  557. spinup(make_sym("car"), make_prim(prim_car));
  558. spinup(make_sym("cdr"), make_prim(prim_cdr));
  559. spinup(make_sym("echo"), make_prim(prim_echo));
  560. spinup(make_sym("read-byte"), make_prim(prim_read_byte));
  561. spinup(make_sym("HALT"), make_prim(prim_halt));
  562. }