mes_eval.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019 Jeremiah Orians
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "mes.h"
  22. /* Imported functions */
  23. FILE* open_file(char* name, char* mode);
  24. struct cell* macro_apply(struct cell* exp, struct cell* vals);
  25. struct cell* make_cell(int type, struct cell* a, struct cell* b, struct cell* env);
  26. struct cell* make_char(int a);
  27. struct cell* make_eof();
  28. struct cell* make_file(FILE* a);
  29. struct cell* make_int(int a);
  30. struct cell* make_prim(void* fun);
  31. struct cell* make_proc(struct cell* a, struct cell* b, struct cell* env);
  32. struct cell* make_macro(struct cell* a, struct cell* b, struct cell* env);
  33. struct cell* make_string(char* a);
  34. struct cell* make_sym(char* name);
  35. struct cell* make_sym(char* name);
  36. struct cell* prim_display(struct cell* args, FILE* out);
  37. struct cell* prim_write(struct cell* args, FILE* out);
  38. struct cell* reverse_list(struct cell* head);
  39. void garbage_collect();
  40. /* Support functions */
  41. struct cell* findsym(char *name)
  42. {
  43. struct cell* symlist;
  44. for(symlist = all_symbols; nil != symlist; symlist = symlist->cdr)
  45. {
  46. if(match(name, symlist->car->string))
  47. {
  48. return symlist;
  49. }
  50. }
  51. return nil;
  52. }
  53. struct cell* intern(char *name)
  54. {
  55. struct cell* op = findsym(name);
  56. if(nil != op) return op->car;
  57. op = make_sym(name);
  58. all_symbols = make_cons(op, all_symbols);
  59. return op;
  60. }
  61. /*** Environment ***/
  62. struct cell* extend(struct cell* env, struct cell* symbol, struct cell* value)
  63. {
  64. return make_cons(make_cons(symbol, value), env);
  65. }
  66. struct cell* multiple_extend(struct cell* env, struct cell* syms, struct cell* vals)
  67. {
  68. if(nil == syms)
  69. {
  70. return env;
  71. }
  72. if(cell_dot == syms->car)
  73. {
  74. return multiple_extend(extend(env, syms->cdr->car, vals), syms->cdr->cdr, vals->cdr);
  75. }
  76. return multiple_extend(extend(env, syms->car, vals->car), syms->cdr, vals->cdr);
  77. }
  78. struct cell* extend_env(struct cell* sym, struct cell* val, struct cell* env)
  79. {
  80. env->cdr = make_cons(env->car, env->cdr);
  81. env->car = make_cons(sym, val);
  82. return NULL;
  83. }
  84. struct cell* assoc(struct cell* key, struct cell* alist)
  85. {
  86. if(nil == alist) return nil;
  87. for(; nil != alist; alist = alist->cdr)
  88. {
  89. if(alist->car->car->string == key->string) return alist->car;
  90. }
  91. return nil;
  92. }
  93. /*** Stack for passing of arguments ***/
  94. void push_cell(struct cell* a)
  95. {
  96. struct cell* s = g_stack[stack_pointer];
  97. if(NULL == s)
  98. {
  99. s = make_cell(0, NULL, NULL, NULL);
  100. g_stack[stack_pointer] = s;
  101. }
  102. stack_pointer = stack_pointer + 1;
  103. /* Copy Over values */
  104. s->type = a->type;
  105. s->car = a->car;
  106. s->cdr = a->cdr;
  107. s->env = a->env;
  108. }
  109. struct cell* pop_cell()
  110. {
  111. stack_pointer = stack_pointer - 1;
  112. struct cell* r = g_stack[stack_pointer];
  113. g_stack[stack_pointer] = NULL;
  114. return r;
  115. }
  116. /*** Evaluator (Eval/Apply) ***/
  117. void eval(struct cell* exp, struct cell* env);
  118. struct cell* evlis(struct cell* exps, struct cell* env)
  119. {
  120. if(exps == nil) return nil;
  121. eval(exps->car, env);
  122. struct cell* i = R0;
  123. struct cell* j = evlis(exps->cdr, env);
  124. return make_cons(i, j);
  125. }
  126. struct cell* progn(struct cell* exps, struct cell* env)
  127. {
  128. if(exps == nil) return nil;
  129. R0 = exps;
  130. struct cell* result;
  131. progn_reset:
  132. push_cell(R0);
  133. eval(R0->car, env);
  134. result = R0;
  135. R0 = pop_cell();
  136. if(R0->cdr == nil) return result;
  137. R0 = R0->cdr;
  138. goto progn_reset;
  139. }
  140. struct cell* apply(struct cell* proc, struct cell* vals)
  141. {
  142. struct cell* temp;
  143. if(proc->type == PRIMOP)
  144. {
  145. FUNCTION* fp = proc->function;
  146. temp = fp(vals);
  147. }
  148. else if(proc->type == LAMBDA)
  149. {
  150. struct cell* env = make_cons(proc->env->car, proc->env->cdr);
  151. temp = progn(proc->cdr, multiple_extend(env, proc->car, vals));
  152. }
  153. else if(proc->type == MACRO)
  154. {
  155. temp = macro_apply(proc->cdr, vals);
  156. }
  157. else
  158. {
  159. file_print("Bad argument to apply\n", stderr);
  160. exit(EXIT_FAILURE);
  161. }
  162. return temp;
  163. }
  164. struct cell* evcond(struct cell* exp, struct cell* env)
  165. {
  166. if(nil == exp) return cell_unspecified;
  167. eval(exp->car->car, env);
  168. if(cell_t == R0)
  169. {
  170. eval(exp->car->cdr->car, env);
  171. return R0;
  172. }
  173. return evcond(exp->cdr, env);
  174. }
  175. struct cell* evwhile(struct cell* exp, struct cell* env)
  176. {
  177. if(nil == exp) return nil;
  178. eval(exp->cdr->car, env);
  179. while(cell_t == R0)
  180. {
  181. eval(exp->cdr->cdr->car, env);
  182. eval(exp->cdr->car, env);
  183. if((cell_t == exp->cdr->car) && (left_to_take < 1000)) garbage_collect();
  184. }
  185. return R0;
  186. }
  187. struct cell* process_sym(struct cell* exp, struct cell* env);
  188. struct cell* process_cons(struct cell* exp, struct cell* env);
  189. void eval(struct cell* exp, struct cell* env)
  190. {
  191. if(NULL == exp)
  192. {
  193. R0 = NULL;
  194. return;
  195. }
  196. if(exp == nil)
  197. {
  198. R0 = nil;
  199. return;
  200. }
  201. if(SYM == exp->type)
  202. {
  203. R0 = process_sym(exp, env);
  204. return;
  205. }
  206. if(CONS == exp->type)
  207. {
  208. R0 = process_cons(exp, env);
  209. return;
  210. }
  211. R0 = exp;
  212. }
  213. struct cell* process_sym(struct cell* exp, struct cell* env)
  214. {
  215. struct cell* tmp = assoc(exp, env);
  216. if(tmp == nil)
  217. {
  218. file_print("Unbound symbol:", stderr);
  219. file_print(exp->string, stderr);
  220. fputc('\n', stderr);
  221. exit(EXIT_FAILURE);
  222. }
  223. return tmp->cdr;
  224. }
  225. struct cell* process_if(struct cell* exp, struct cell* env)
  226. {
  227. eval(exp->cdr->car, env);
  228. if(R0 != cell_f)
  229. {
  230. eval(exp->cdr->cdr->car, env);
  231. return R0;
  232. }
  233. if(nil == exp->cdr->cdr->cdr) return cell_unspecified;
  234. eval(exp->cdr->cdr->cdr->car, env);
  235. return R0;
  236. }
  237. struct cell* process_setb(struct cell* exp, struct cell* env)
  238. {
  239. eval(exp->cdr->cdr->car, env);
  240. struct cell* newval = R0;
  241. struct cell* pair = assoc(exp->cdr->car, env);
  242. pair->cdr = newval;
  243. return NULL;
  244. }
  245. struct cell* process_let(struct cell* exp, struct cell* env)
  246. {
  247. struct cell* lets;
  248. for(lets = exp->cdr->car; lets != nil; lets = lets->cdr)
  249. {
  250. eval(lets->car->cdr->car, env);
  251. env = make_cons(make_cons(lets->car->car, R0), env);
  252. }
  253. return progn(exp->cdr->cdr, env);
  254. }
  255. struct cell* process_quasiquote(struct cell* exp, struct cell* env)
  256. {
  257. struct cell* i = exp;
  258. struct cell* f = NULL;
  259. struct cell* h;
  260. while(nil != i)
  261. {
  262. h = i->car;
  263. if(CONS == i->car->type)
  264. {
  265. if(unquote == i->car->car)
  266. {
  267. eval(i->car->cdr->car, env);
  268. h = R0;
  269. }
  270. if(unquote_splicing == i->car->car)
  271. {
  272. eval(i->car->cdr->car, env);
  273. while((NULL != R0) && (nil != R0))
  274. {
  275. /* Unsure if correct behavior is to revert to unquote behavior (what guile does) */
  276. /* Or restrict to just proper lists as the spec (r7rs) requires */
  277. /* eg. `(foo bar ,@(+ 4 5)) */
  278. require(CONS == R0->type, "unquote-splicing requires argument of type <proper list>\n");
  279. f = make_cons(R0->car, f);
  280. /* Simply convert require to if and the above */
  281. /* else f = make_cons(R0, f); */
  282. R0 = R0->cdr;
  283. }
  284. goto restart_quasiquote;
  285. }
  286. }
  287. f = make_cons(h, f);
  288. restart_quasiquote:
  289. i = i->cdr;
  290. }
  291. i = f;
  292. f = reverse_list(f);
  293. require(NULL != i, "Impossible quasiquote processed?\n");
  294. i->cdr = nil;
  295. return f;
  296. }
  297. struct cell* process_define(struct cell* exp, struct cell* env)
  298. {
  299. if(CONS == exp->cdr->car->type)
  300. {
  301. struct cell* fun = exp->cdr->cdr;
  302. struct cell* arguments = exp->cdr->car->cdr;
  303. struct cell* name = exp->cdr->car->car;
  304. exp->cdr = make_cons(name, make_cons(make_cons(s_lambda, make_cons(arguments, fun)), nil));
  305. }
  306. eval(exp->cdr->cdr->car, env);
  307. return(extend_env(exp->cdr->car, R0, env));
  308. }
  309. struct cell* process_cons(struct cell* exp, struct cell* env)
  310. {
  311. if(exp->car == s_if) return process_if(exp, env);
  312. if(exp->car == s_cond) return evcond(exp->cdr, env);
  313. if(exp->car == s_begin) return progn(exp->cdr, env);
  314. if(exp->car == s_lambda) return make_proc(exp->cdr->car, exp->cdr->cdr, env);
  315. if(exp->car == s_macro) return make_macro(exp->cdr->car, exp->cdr->cdr, env);
  316. if(exp->car == quote) return exp->cdr->car;
  317. if(exp->car == quasiquote) return process_quasiquote(exp->cdr->car, env);
  318. if(exp->car == s_define) return process_define(exp, env);
  319. if(exp->car == s_setb) return process_setb(exp, env);
  320. if(exp->car == s_let) return process_let(exp, env);
  321. if(exp->car == s_while) return evwhile(exp, env);
  322. eval(exp->car, env);
  323. push_cell(R0);
  324. R1 = evlis(exp->cdr, env);
  325. R0 = pop_cell();
  326. return apply(R0, R1);
  327. }
  328. /* Exposed primitives */
  329. struct cell* builtin_apply(struct cell* args)
  330. {
  331. require(nil != args, "apply requires arguments\n");
  332. require(nil != args->cdr, "apply recieved insufficient arguments\n");
  333. require(CONS == args->cdr->car->type, "apply did not recieve a list\n");
  334. return apply(args->car, args->cdr->car);
  335. }
  336. struct cell* builtin_primitive_eval(struct cell* args)
  337. {
  338. require(nil != args, "primitive-eval requires an argument\n");
  339. require(nil == args->cdr, "primitive-eval received too many arguments\n");
  340. push_cell(R0);
  341. push_cell(R1);
  342. push_cell(g_env);
  343. eval(args->car, primitive_env);
  344. struct cell* r = R0;
  345. g_env = pop_cell();
  346. R1 = pop_cell();
  347. R0 = pop_cell();
  348. return r;
  349. }