mes_builtins.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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* assoc(struct cell* key, struct cell* alist);
  25. struct cell* extend(struct cell* env, struct cell* symbol, struct cell* value);
  26. struct cell* load_file(char* s);
  27. struct cell* make_cell(int type, struct cell* a, struct cell* b, struct cell* env);
  28. struct cell* make_char(int a);
  29. struct cell* make_eof();
  30. struct cell* make_file(FILE* a, char* name);
  31. struct cell* make_int(int a);
  32. struct cell* make_sym(char* name);
  33. struct cell* string_eq(struct cell* a, struct cell* b);
  34. struct cell* vector_equal(struct cell* a, struct cell* b);
  35. void garbage_collect();
  36. /*** Primitives ***/
  37. struct cell* nullp(struct cell* args)
  38. {
  39. require(nil != args, "null? requires arguments\n");
  40. require(nil == args->cdr, "null? recieved too many arguments\n");
  41. if(nil == args->car) return cell_t;
  42. return cell_f;
  43. }
  44. struct cell* pairp(struct cell* args)
  45. {
  46. require(nil != args, "pair? requires arguments\n");
  47. require(nil == args->cdr, "pair? recieved too many arguments\n");
  48. if(CONS == args->car->type) return cell_t;
  49. return cell_f;
  50. }
  51. struct cell* portp(struct cell* args)
  52. {
  53. require(nil != args, "port? requires arguments\n");
  54. require(nil == args->cdr, "port? recieved too many arguments\n");
  55. if(FILE_PORT == args->car->type) return cell_t;
  56. return cell_f;
  57. }
  58. struct cell* symbolp(struct cell* args)
  59. {
  60. require(nil != args, "symbol? requires arguments\n");
  61. require(nil == args->cdr, "symbol? recieved too many arguments\n");
  62. if(nil == args->car) return cell_f;
  63. if(SYM == args->car->type) return cell_t;
  64. return cell_f;
  65. }
  66. struct cell* builtin_charp(struct cell* args)
  67. {
  68. require(nil != args, "char? requires arguments\n");
  69. require(nil == args->cdr, "char? recieved too many arguments\n");
  70. if(CHAR == args->car->type) return cell_t;
  71. return cell_f;
  72. }
  73. struct cell* builtin_intp(struct cell* args)
  74. {
  75. require(nil != args, "number? requires arguments\n");
  76. require(nil == args->cdr, "number? recieved too many arguments\n");
  77. if(INT == args->car->type) return cell_t;
  78. return cell_f;
  79. }
  80. struct cell* builtin_primitivep(struct cell* args)
  81. {
  82. require(nil != args, "primitive? requires arguments\n");
  83. require(nil == args->cdr, "primitive? recieved too many arguments\n");
  84. if(PRIMOP == args->car->type) return cell_t;
  85. return cell_f;
  86. }
  87. struct cell* builtin_procedurep(struct cell* args)
  88. {
  89. require(nil != args, "procedure? requires arguments\n");
  90. require(nil == args->cdr, "procedure? recieved too many arguments\n");
  91. if(LAMBDA == args->car->type) return cell_t;
  92. return cell_f;
  93. }
  94. struct cell* builtin_eofp (struct cell* args)
  95. {
  96. require(nil != args, "eof? requires arguments\n");
  97. require(nil == args->cdr, "eof? recieved too many arguments\n");
  98. if(EOF_object == args->car->type) return cell_t;
  99. return cell_f;
  100. }
  101. struct cell* builtin_definedp(struct cell* args)
  102. {
  103. require(nil != args, "defined? requires arguments\n");
  104. require(nil == args->cdr, "defined? recieved too many arguments\n");
  105. require(SYM == args->car->type, "defined? did not receive a symbol\n");
  106. struct cell* hold = assoc(args->car, g_env);
  107. if(NULL != hold->cdr) return cell_t;
  108. return cell_f;
  109. }
  110. struct cell* builtin_sum(struct cell* args)
  111. {
  112. if(nil == args) return make_int(0);
  113. int sum;
  114. for(sum = 0; nil != args; args = args->cdr)
  115. {
  116. require(INT == args->car->type, "builtin_sum require integers\n");
  117. sum = sum + args->car->value;
  118. }
  119. return make_int(sum);
  120. }
  121. struct cell* builtin_sub(struct cell* args)
  122. {
  123. require(nil != args, "builtin_sub requires arguments\n");
  124. require(INT == args->car->type, "builtin_sub require integers\n");
  125. int sum = args->car->value;
  126. for(args = args->cdr; nil != args; args = args->cdr)
  127. {
  128. require(INT == args->car->type, "builtin_sub require integers\n");
  129. sum = sum - args->car->value;
  130. }
  131. return make_int(sum);
  132. }
  133. struct cell* builtin_prod(struct cell* args)
  134. {
  135. if(nil == args) make_int(1);
  136. int prod;
  137. for(prod = 1; nil != args; args = args->cdr)
  138. {
  139. require(INT == args->car->type, "builtin_prod require integers\n");
  140. prod = prod * args->car->value;
  141. }
  142. return make_int(prod);
  143. }
  144. struct cell* builtin_div(struct cell* args)
  145. {
  146. require(nil != args, "builtin_div requires arguments\n");
  147. require(INT == args->car->type, "builtin_div require integers\n");
  148. SCM div = args->car->value;
  149. for(args = args->cdr; nil != args; args = args->cdr)
  150. {
  151. require(INT == args->car->type, "builtin_div require integers\n");
  152. div = div / args->car->value;
  153. }
  154. return make_int(div);
  155. }
  156. struct cell* builtin_mod(struct cell* args)
  157. {
  158. require(nil != args, "modulo requires 2 arguments\n");
  159. require(INT == args->car->type, "modulo first argument not an integer\n");
  160. require(nil != args->cdr, "modulo did not recieve a second argument\n");
  161. require(INT == args->cdr->car->type, "modulo second argument not an integer\n");
  162. SCM mod = args->car->value;
  163. mod = mod % args->cdr->car->value;
  164. if((0 > args->car->value) ^ (0 > args->cdr->car->value))
  165. {
  166. mod = mod + args->cdr->car->value;
  167. }
  168. require(nil == args->cdr->cdr, "wrong number of arguments to modulo\n");
  169. return make_int(mod);
  170. }
  171. struct cell* builtin_rem(struct cell* args)
  172. {
  173. require(nil != args, "remainder requires 2 arguments\n");
  174. require(INT == args->car->type, "remainder first argument not an integer\n");
  175. require(nil != args->cdr, "remainder did not recieve a second argument\n");
  176. require(INT == args->cdr->car->type, "remainder second argument not an integer\n");
  177. SCM rem = args->car->value;
  178. rem = rem % args->cdr->car->value;
  179. if(0 > args->cdr->car->value)
  180. {
  181. rem = rem + args->cdr->car->value;
  182. }
  183. require(nil == args->cdr->cdr, "wrong number of arguments to remainder\n");
  184. return make_int(rem);
  185. }
  186. struct cell* builtin_ash(struct cell* args)
  187. {
  188. require(nil != args, "ash requires 2 arguments\n");
  189. require(INT == args->car->type, "ash first argument not an integer\n");
  190. require(nil != args->cdr, "ash did not recieve a second argument\n");
  191. require(INT == args->cdr->car->type, "ash second argument not an integer\n");
  192. require(nil == args->cdr->cdr, "wrong number of arguments to ash\n");
  193. long ash = args->car->value;
  194. int count = args->cdr->car->value;
  195. if(count < 0)
  196. {
  197. count = -count;
  198. ash = ash >> count;
  199. }
  200. else
  201. {
  202. ash = ash << count;
  203. }
  204. return make_int(ash);
  205. }
  206. struct cell* builtin_logand(struct cell* args)
  207. {
  208. long n = -1;
  209. while(nil != args)
  210. {
  211. require(INT == args->car->type, "builtin_logand require integers\n");
  212. n = n & args->car->value;
  213. args = args->cdr;
  214. }
  215. return make_int(n);
  216. }
  217. struct cell* builtin_logor(struct cell* args)
  218. {
  219. long n = 0;
  220. while(nil != args)
  221. {
  222. require(INT == args->car->type, "builtin_logior require integers\n");
  223. n = n | args->car->value;
  224. args = args->cdr;
  225. }
  226. return make_int(n);
  227. }
  228. struct cell* builtin_xor(struct cell* args)
  229. {
  230. long n = 0;
  231. while(nil != args)
  232. {
  233. require(INT == args->car->type, "builtin_logxor require integers\n");
  234. n = n ^ args->car->value;
  235. args = args->cdr;
  236. }
  237. return make_int(n);
  238. }
  239. struct cell* builtin_lognot(struct cell* args)
  240. {
  241. require(nil != args, "lognot requires 1 argument\n");
  242. require(INT == args->car->type, "lognot first argument not an integer\n");
  243. require(nil == args->cdr, "lognot recieved wrong number of arguments\n");
  244. return make_int(~args->car->value);
  245. }
  246. struct cell* builtin_not(struct cell* args)
  247. {
  248. require(nil != args, "not requires 1 argument\n");
  249. require(nil == args->cdr, "not recieved wrong number of arguments\n");
  250. if(cell_f == args->car) return cell_t;
  251. return cell_f;
  252. }
  253. struct cell* builtin_and(struct cell* args)
  254. {
  255. require(nil != args, "and requires arguments\n");
  256. while(nil != args)
  257. {
  258. if(cell_t != args->car) return cell_f;
  259. args = args->cdr;
  260. }
  261. return cell_t;
  262. }
  263. struct cell* builtin_or(struct cell* args)
  264. {
  265. require(nil != args, "or requires arguments\n");
  266. while(nil != args)
  267. {
  268. if(cell_t == args->car) return cell_t;
  269. args = args->cdr;
  270. }
  271. return cell_f;
  272. }
  273. struct cell* builtin_numgt(struct cell* args)
  274. {
  275. require(nil != args, "builtin_numgt requires arguments\n");
  276. require(INT == args->car->type, "builtin_numgt require integers\n");
  277. int temp = args->car->value;
  278. for(args = args->cdr; nil != args; args = args->cdr)
  279. {
  280. require(INT == args->car->type, "builtin_numgt require integers\n");
  281. if(temp <= args->car->value)
  282. {
  283. return cell_f;
  284. }
  285. temp = args->car->value;
  286. }
  287. return cell_t;
  288. }
  289. struct cell* builtin_numge(struct cell* args)
  290. {
  291. require(nil != args, "builtin_numge requires arguments\n");
  292. require(INT == args->car->type, "builtin_numge require integers\n");
  293. int temp = args->car->value;
  294. for(args = args->cdr; nil != args; args = args->cdr)
  295. {
  296. require(INT == args->car->type, "builtin_numge require integers\n");
  297. if(temp < args->car->value)
  298. {
  299. return cell_f;
  300. }
  301. temp = args->car->value;
  302. }
  303. return cell_t;
  304. }
  305. struct cell* builtin_numle(struct cell* args)
  306. {
  307. require(nil != args, "builtin_numle requires arguments\n");
  308. require(INT == args->car->type, "builtin_numle require integers\n");
  309. int temp = args->car->value;
  310. for(args = args->cdr; nil != args; args = args->cdr)
  311. {
  312. require(INT == args->car->type, "builtin_numle require integers\n");
  313. if(temp > args->car->value)
  314. {
  315. return cell_f;
  316. }
  317. temp = args->car->value;
  318. }
  319. return cell_t;
  320. }
  321. struct cell* builtin_numlt(struct cell* args)
  322. {
  323. require(nil != args, "builtin_numlt requires arguments\n");
  324. require(INT == args->car->type, "builtin_numlt require integers\n");
  325. int temp = args->car->value;
  326. for(args = args->cdr; nil != args; args = args->cdr)
  327. {
  328. require(INT == args->car->type, "builtin_numlt require integers\n");
  329. if(temp >= args->car->value)
  330. {
  331. return cell_f;
  332. }
  333. temp = args->car->value;
  334. }
  335. return cell_t;
  336. }
  337. struct cell* builtin_chareq(struct cell* args)
  338. {
  339. require(nil != args, "char=? requires arguments\n");
  340. require(CHAR == args->car->type, "char=? received non-char\n");
  341. int temp = args->car->value;
  342. for(args = args->cdr; nil != args; args = args->cdr)
  343. {
  344. require(CHAR == args->car->type, "char=? received non-char\n");
  345. if(temp != args->car->value) return cell_f;
  346. }
  347. return cell_t;
  348. }
  349. struct cell* builtin_numeq(struct cell* args)
  350. {
  351. require(nil != args, "= requires arguments\n");
  352. require(INT == args->car->type, "= received non-integer\n");
  353. int temp = args->car->value;
  354. for(args = args->cdr; nil != args; args = args->cdr)
  355. {
  356. require(INT == args->car->type, "= received non-integer\n");
  357. if(temp != args->car->value) return cell_f;
  358. }
  359. return cell_t;
  360. }
  361. struct cell* builtin_eq(struct cell* args)
  362. {
  363. if(nil == args) return cell_t;
  364. if(nil == args->cdr) return cell_t;
  365. struct cell* temp = args->car;
  366. for(args = args->cdr; nil != args; args = args->cdr)
  367. {
  368. if(temp == args->car) continue;
  369. else if(temp->type != args->car->type) return cell_f;
  370. else if((INT == temp->type) || (CHAR == temp->type))
  371. {
  372. if(temp->value != args->car->value) return cell_f;
  373. }
  374. else if(STRING == temp->type)
  375. {
  376. if(temp != args->car) return cell_f;
  377. }
  378. else if(CONS == temp->type)
  379. {
  380. if(temp != args->car) return cell_f;
  381. }
  382. else return cell_f;
  383. }
  384. return cell_t;
  385. }
  386. struct cell* equal(struct cell* a, struct cell* b)
  387. {
  388. if(a == b) return cell_t;
  389. if(NULL == a) return cell_f;
  390. if(NULL == b) return cell_f;
  391. if(a->type != b->type) return cell_f;
  392. if((INT == a->type) || (CHAR == a->type))
  393. {
  394. if(a->value != b->value) return cell_f;
  395. return cell_t;
  396. }
  397. else if(STRING == a->type)
  398. {
  399. return string_eq(a, b);
  400. }
  401. else if(VECTOR == a->type)
  402. {
  403. return vector_equal(a, b);
  404. }
  405. else if(CONS == a->type)
  406. {
  407. if(cell_t != equal(a->car, b->car)) return cell_f;
  408. if(cell_t != equal(a->cdr, b->cdr)) return cell_f;
  409. return cell_t;
  410. }
  411. return cell_f;
  412. }
  413. struct cell* builtin_equal(struct cell* args)
  414. {
  415. require(nil != args, "equal? requires arguments\n");
  416. struct cell* temp = args->car;
  417. for(args = args->cdr; nil != args; args = args->cdr)
  418. {
  419. if(cell_t != equal(temp, args->car))
  420. {
  421. return cell_f;
  422. }
  423. }
  424. return cell_t;
  425. }
  426. struct cell* builtin_freecell(struct cell* args)
  427. {
  428. if(nil == args)
  429. {
  430. file_print("Remaining Cells: ", stdout);
  431. file_print(numerate_number(left_to_take), stdout);
  432. return nil;
  433. }
  434. return make_int(left_to_take);
  435. }
  436. struct cell* builtin_number_to_char(struct cell* args)
  437. {
  438. require(nil != args, "integer->char requires an argument\n");
  439. require(nil == args->cdr, "integer->char only supports a single argument\n");
  440. require(INT == args->car->type, "integer->char requires an integer\n");
  441. return make_char(args->car->value);
  442. }
  443. struct cell* builtin_char_to_number(struct cell* args)
  444. {
  445. require(nil != args, "char->integer requires an argument\n");
  446. require(nil == args->cdr, "char->integer only supports a single argument\n");
  447. require(CHAR == args->car->type, "char->integer requires a char\n");
  448. return make_int(args->car->value);
  449. }
  450. struct cell* builtin_primitive_load(struct cell* args)
  451. {
  452. require(nil != args, "primitive-load requires an argument\n");
  453. require(STRING == args->car->type, "primitive-load requires a string\n");
  454. require(nil == args->cdr, "primitive-load only accepts one argument\n");
  455. struct cell* r = load_file(args->car->string);
  456. require(cell_t == r, "primitive-load failed to open file\n");
  457. return cell_unspecified;
  458. }
  459. struct cell* builtin_read_byte(struct cell* args)
  460. {
  461. if(nil == args) return make_char(fgetc(__stdin->file));
  462. else if(FILE_PORT == args->car->type)
  463. {
  464. int c = fgetc(args->car->file);
  465. if(EOF == c) return make_eof();
  466. return make_char(c);
  467. }
  468. return nil;
  469. }
  470. struct cell* builtin_halt(struct cell* args)
  471. {
  472. exit(args->car->value);
  473. }
  474. struct cell* builtin_cons(struct cell* args)
  475. {
  476. require(nil != args, "cons requires arguments\n");
  477. require(nil != args->cdr, "cons requires 2 arguments\n");
  478. require(nil == args->cdr->cdr, "cons recieved too many arguments\n");
  479. return make_cons(args->car, args->cdr->car);
  480. }
  481. struct cell* builtin_car(struct cell* args)
  482. {
  483. require(nil != args, "car requires arguments\n");
  484. require(CONS == args->car->type, "car expects a pair\n");
  485. require(nil == args->cdr, "car expects only a single argument\n");
  486. return args->car->car;
  487. }
  488. struct cell* builtin_cdr(struct cell* args)
  489. {
  490. require(nil != args, "cdr requires arguments\n");
  491. require(CONS == args->car->type, "cdr expects a pair\n");
  492. require(nil == args->cdr, "cdr expects only a single argument\n");
  493. return args->car->cdr;
  494. }
  495. struct cell* builtin_setcar(struct cell* args)
  496. {
  497. require(nil != args, "set-car! requires arguments\n");
  498. require(CONS == args->car->type, "set-car! requires a mutable pair\n");
  499. require(nil != args->cdr, "set-car! requires something to set car to\n");
  500. args->car->car = args->cdr->car;
  501. require(nil == args->cdr->cdr, "set-car! received too many arguements\n");
  502. return NULL;
  503. }
  504. struct cell* builtin_setcdr(struct cell* args)
  505. {
  506. require(nil != args, "set-cdr! requires arguments\n");
  507. require(CONS == args->car->type, "set-cdr! requires a mutable pair\n");
  508. require(nil != args->cdr, "set-cdr! requires something to set cdr to\n");
  509. args->car->cdr = args->cdr->car;
  510. require(nil == args->cdr->cdr, "set-cdr! received too many arguements\n");
  511. return NULL;
  512. }