mes_list.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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);
  27. struct cell* make_sym(char* name);
  28. struct cell* string_to_list(char* string)
  29. {
  30. if(NULL == string) return nil;
  31. if(0 == string[0]) return nil;
  32. struct cell* result = make_char(string[0]);
  33. struct cell* tail = string_to_list(string + 1);
  34. return make_cons(result, tail);
  35. }
  36. int list_length(struct cell* args)
  37. {
  38. require(CONS == args->type, "mes_list.c: list_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. char* 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 string;
  70. }
  71. struct cell* append(struct cell* a, struct cell* b)
  72. {
  73. if(nil == a) return b;
  74. return make_cons(a->car, append(a->cdr, b));
  75. }
  76. struct cell* list_equal(struct cell* a, struct cell* b)
  77. {
  78. while((nil != a) && (nil != b))
  79. {
  80. require(CONS == a->type, "mes_list.c: list_equal received non-list\n");
  81. require(CONS == b->type, "mes_list.c: list_equal received non-list\n");
  82. if(cell_t != equal(a->car, b->car)) return cell_f;
  83. a = a->cdr;
  84. b = b->cdr;
  85. }
  86. if(a != b) return cell_f;
  87. return cell_t;
  88. }
  89. struct cell* reverse(struct cell* a, struct cell* b)
  90. {
  91. require(CONS == a->type, "reverse did not receive a list\n");
  92. if(nil == a->cdr) return make_cons(a->car, b);
  93. require(CONS == a->cdr->type, "reverse did not receive a true list\n");
  94. return reverse(a->cdr, make_cons(a->car, b));
  95. }
  96. /* Exposed primitives */
  97. struct cell* builtin_listp(struct cell* args)
  98. {
  99. require(nil != args, "list? requires arguments\n");
  100. require(nil == args->cdr, "list? recieved too many arguments\n");
  101. struct cell* i = args->car;
  102. while(nil != i)
  103. {
  104. if(CONS != i->type) return cell_f;
  105. i = i->cdr;
  106. }
  107. return cell_t;
  108. }
  109. struct cell* builtin_append(struct cell* args)
  110. {
  111. if(nil == args) return nil;
  112. require(((nil == args->car) || (CONS == args->car->type)), "append requires a list\n");
  113. if(nil == args->cdr) return args->car;
  114. require(((nil == args->car) || (CONS == args->car->type)), "append requires a list argument\n");
  115. return append(args->car, args->cdr->car);
  116. }
  117. struct cell* builtin_listeq(struct cell* args)
  118. {
  119. require(nil != args, "list=? requires arguments\n");
  120. require(CONS == args->car->type, "list=? received non-list\n");
  121. struct cell* temp = args->car;
  122. for(args = args->cdr; nil != args; args = args->cdr)
  123. {
  124. require(CONS == args->car->type, "list=? received non-list\n");
  125. if(cell_t != list_equal(temp, args->car))
  126. {
  127. return cell_f;
  128. }
  129. }
  130. return cell_t;
  131. }
  132. struct cell* builtin_string_to_list(struct cell* args)
  133. {
  134. require(nil != args, "string->list requires arguments\n");
  135. require(STRING == args->car->type, "mes-builtin.c: string->list did not receive a string\n");
  136. struct cell* r = string_to_list(args->car->string);
  137. if(nil == args->cdr)
  138. {
  139. return r;
  140. }
  141. require(INT == args->cdr->car->type, "string->list only accepts integers\n");
  142. int i = args->cdr->car->value;
  143. require(0 <= i, "string->list invalid index\n");
  144. while(0 != i)
  145. {
  146. require(nil != r, "string->list index too large\n");
  147. r = r->cdr;
  148. i = i - 1;
  149. }
  150. return r;
  151. }
  152. struct cell* builtin_list_length(struct cell* args)
  153. {
  154. require(nil != args, "list-length requires arguments\n");
  155. return make_int(list_length(args));
  156. }
  157. struct cell* builtin_list_to_string(struct cell* args)
  158. {
  159. require(nil != args, "list->string requires an argument\n");
  160. require(nil == args->cdr, "list->string only allows a single argument\n");
  161. return make_string(list_to_string(args));
  162. }
  163. struct cell* builtin_list_to_symbol(struct cell* args)
  164. {
  165. require(nil != args, "list->symbol requires an argument\n");
  166. require(nil == args->cdr, "list->symbol only allows a single argument\n");
  167. return make_sym(list_to_string(args));
  168. }
  169. struct cell* builtin_list(struct cell* args)
  170. {
  171. /* List is stupid, just return */
  172. return args;
  173. }
  174. struct cell* builtin_reverse(struct cell* args)
  175. {
  176. require(nil != args, "reverse requires arguments\n");
  177. require(nil == args->cdr, "reverse recieved too many arguments\n");
  178. return reverse(args->car, nil);
  179. }