mes_list.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. struct cell* equal(struct cell* a, struct cell* b);
  24. struct cell* make_char(int a);
  25. struct cell* make_int(int a);
  26. struct cell* make_string(char* a, int length);
  27. struct cell* make_sym(char* name);
  28. struct cell* string_to_list(char* string, int length)
  29. {
  30. if(NULL == string) return nil;
  31. if(0 == length) return nil;
  32. struct cell* result = make_char(string[0]);
  33. struct cell* tail = string_to_list(string + 1, length - 1);
  34. return make_cons(result, tail);
  35. }
  36. int list_length(struct cell* args)
  37. {
  38. require(CONS == args->type, "mes_list.c: length recieved wrong type\n");
  39. if(nil == args) return 0;
  40. int size = 0;
  41. struct cell* i;
  42. for(i = args->car; nil != i; i = i->cdr)
  43. {
  44. require(CONS == i->type, "mes_list.c: list_length recieved non-pure list\n");
  45. size = size + 1;
  46. }
  47. return size;
  48. }
  49. struct cell* list_to_string(struct cell* args)
  50. {
  51. require(CONS == args->type, "mes_list.c: list_to_string recieved wrong type\n");
  52. char* string = calloc(list_length(args) + 1, sizeof(char));
  53. int index = 0;
  54. struct cell* i;
  55. for(i = args->car; nil != i; i = i->cdr)
  56. {
  57. require(CONS == args->type, "mes_list.c: list_to_string recieved non-pure list\n");
  58. if(CHAR == i->car->type)
  59. {
  60. string[index] = i->car->value;
  61. index = index + 1;
  62. }
  63. else
  64. {
  65. file_print("Wrong type recieved\n", stdout);
  66. exit(EXIT_FAILURE);
  67. }
  68. }
  69. return make_string(string, list_length(args));
  70. }
  71. struct cell* append(struct cell* a, struct cell* b)
  72. {
  73. if(nil == a) return b;
  74. require(CONS == a->type, "Did not recieve a proper list in append\n");
  75. return make_cons(a->car, append(a->cdr, b));
  76. }
  77. struct cell* list_equal(struct cell* a, struct cell* b)
  78. {
  79. while((nil != a) && (nil != b))
  80. {
  81. require(CONS == a->type, "mes_list.c: list_equal received non-list\n");
  82. require(CONS == b->type, "mes_list.c: list_equal received non-list\n");
  83. if(cell_t != equal(a->car, b->car)) return cell_f;
  84. a = a->cdr;
  85. b = b->cdr;
  86. }
  87. if(a != b) return cell_f;
  88. return cell_t;
  89. }
  90. struct cell* reverse(struct cell* a, struct cell* b)
  91. {
  92. require(CONS == a->type, "reverse did not receive a list\n");
  93. if(nil == a->cdr) return make_cons(a->car, b);
  94. require(CONS == a->cdr->type, "reverse did not receive a true list\n");
  95. return reverse(a->cdr, make_cons(a->car, b));
  96. }
  97. /* Exposed primitives */
  98. struct cell* builtin_listp(struct cell* args)
  99. {
  100. require(nil != args, "list? requires arguments\n");
  101. require(nil == args->cdr, "list? recieved too many arguments\n");
  102. struct cell* i = args->car;
  103. while(nil != i)
  104. {
  105. if(CONS != i->type) return cell_f;
  106. i = i->cdr;
  107. }
  108. return cell_t;
  109. }
  110. struct cell* builtin_append(struct cell* args)
  111. {
  112. struct cell* i = args;
  113. if(nil == i) return nil;
  114. while (nil != i->cdr)
  115. {
  116. require(((nil == i->car) || (CONS == i->car->type)), "append requires a list\n");
  117. require(((nil == i->cdr->car) || (CONS == i->cdr->car->type)), "append requires a list argument\n");
  118. i->car = append(i->car, i->cdr->car);
  119. i->cdr = i->cdr->cdr;
  120. }
  121. return i->car;
  122. }
  123. struct cell* builtin_listeq(struct cell* args)
  124. {
  125. require(nil != args, "list=? requires arguments\n");
  126. require(CONS == args->car->type, "list=? received non-list\n");
  127. struct cell* temp = args->car;
  128. for(args = args->cdr; nil != args; args = args->cdr)
  129. {
  130. require(CONS == args->car->type, "list=? received non-list\n");
  131. if(cell_t != list_equal(temp, args->car))
  132. {
  133. return cell_f;
  134. }
  135. }
  136. return cell_t;
  137. }
  138. struct cell* builtin_string_to_list(struct cell* args)
  139. {
  140. require(nil != args, "string->list requires arguments\n");
  141. require(STRING == args->car->type, "mes-builtin.c: string->list did not receive a string\n");
  142. struct cell* r = string_to_list(args->car->string, args->car->length);
  143. if(nil == args->cdr)
  144. {
  145. return r;
  146. }
  147. require(INT == args->cdr->car->type, "string->list only accepts integers\n");
  148. int i = args->cdr->car->value;
  149. require(0 <= i, "string->list invalid index\n");
  150. while(0 != i)
  151. {
  152. require(nil != r, "string->list index too large\n");
  153. r = r->cdr;
  154. i = i - 1;
  155. }
  156. return r;
  157. }
  158. struct cell* builtin_list_length(struct cell* args)
  159. {
  160. require(nil != args, "list-length requires arguments\n");
  161. return make_int(list_length(args));
  162. }
  163. struct cell* builtin_list_to_string(struct cell* args)
  164. {
  165. require(nil != args, "list->string requires an argument\n");
  166. require(nil == args->cdr, "list->string only allows a single argument\n");
  167. return list_to_string(args);
  168. }
  169. struct cell* builtin_list_to_symbol(struct cell* args)
  170. {
  171. require(nil != args, "list->symbol requires an argument\n");
  172. require(nil == args->cdr, "list->symbol only allows a single argument\n");
  173. struct cell* r = list_to_string(args);
  174. r->type = SYM;
  175. return r;
  176. }
  177. struct cell* builtin_list(struct cell* args)
  178. {
  179. /* List is stupid, just return */
  180. return args;
  181. }
  182. struct cell* builtin_reverse(struct cell* args)
  183. {
  184. require(nil != args, "reverse requires arguments\n");
  185. require(nil == args->cdr, "reverse recieved too many arguments\n");
  186. return reverse(args->car, nil);
  187. }