mes_vector.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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* make_int(int a);
  24. struct cell* make_vector(int count, struct cell* init);
  25. struct cell* equal(struct cell* a, struct cell* b);
  26. struct cell* vector_to_list(struct cell* a)
  27. {
  28. require(VECTOR == a->type, "mes_vector.c: vector_to_list received non-vector\n");
  29. return a->cdr;
  30. }
  31. struct cell* vector_ref(struct cell* a, int i)
  32. {
  33. require(VECTOR == a->type, "mes_vector.c: vector_ref received non-vector\n");
  34. require(i >= 0, "mes_vector.c: vector_ref received negative index\n");
  35. require(i < a->value, "mes_vector.c: vector_ref received index past end of vector\n");
  36. struct cell* e = a->cdr;
  37. while(0 < i)
  38. {
  39. e = e->cdr;
  40. i = i - 1;
  41. }
  42. return e->car;
  43. }
  44. struct cell* vector_set(struct cell* v, int i, struct cell* e)
  45. {
  46. require(VECTOR == v->type, "mes_vector.c: vector_set received non-vector\n");
  47. require(i >= 0, "mes_vector.c: vector_set received negative index\n");
  48. require(i < v->value, "mes_vector.c: vector_set received index past end of vector\n");
  49. v = v->cdr;
  50. while(i > 0)
  51. {
  52. v = v->cdr;
  53. i = i - 1;
  54. }
  55. v->car = e;
  56. return NULL;
  57. }
  58. struct cell* list_to_vector(struct cell* i)
  59. {
  60. if(nil == i) return make_vector(0, cell_unspecified);
  61. require(CONS == i->type, "mes_vector.c: list_to_vector did not recieve a list\n");
  62. struct cell* r = make_vector(0, cell_unspecified);
  63. r->cdr = i;
  64. int count = 1;
  65. while(nil != i->cdr)
  66. {
  67. require(CONS == i->type, "mes_vector.c: list_to_vector did not recieve a true list\n");
  68. i = i->cdr;
  69. count = count + 1;
  70. }
  71. r->value = count;
  72. return r;
  73. }
  74. struct cell* vector_equal(struct cell* a, struct cell* b)
  75. {
  76. require(VECTOR == a->type, "mes_vector.c: vector_equal received non-vector\n");
  77. require(VECTOR == b->type, "mes_vector.c: vector_equal received non-vector\n");
  78. if(a->value != b->value) return cell_f;
  79. if(0 == a->value) return cell_t;
  80. a = a->cdr;
  81. b = b->cdr;
  82. while(nil != a)
  83. {
  84. if(cell_t != equal(a->car, b->car)) return cell_f;
  85. a = a->cdr;
  86. b = b->cdr;
  87. }
  88. return cell_t;
  89. }
  90. /* Exposed primitives */
  91. struct cell* builtin_vectoreq(struct cell* args)
  92. {
  93. require(nil != args, "vector=? requires arguments\n");
  94. require(VECTOR == args->car->type, "vector=? received non-vector\n");
  95. struct cell* temp = args->car;
  96. for(args = args->cdr; nil != args; args = args->cdr)
  97. {
  98. require(VECTOR == args->car->type, "vector=? received non-vector\n");
  99. if(cell_t != vector_equal(temp, args->car))
  100. {
  101. return cell_f;
  102. }
  103. }
  104. return cell_t;
  105. }
  106. struct cell* builtin_make_vector(struct cell* args)
  107. {
  108. require(nil != args, "make-vector requires arguments\n");
  109. require(INT == args->car->type, "make-vector requires a numerical argument\n");
  110. require(0 <= args->car->value, "make-vector requires a number >= 0\n");
  111. if(nil == args->cdr) return make_vector(args->car->value, cell_unspecified);
  112. require(nil == args->cdr->cdr, "make-vector recieved too many arguments\n");
  113. return make_vector(args->car->value, args->cdr->car);
  114. }
  115. struct cell* builtin_vector_length(struct cell* args)
  116. {
  117. require(nil != args, "vector-length requires an argument\n");
  118. return make_int(args->car->value);
  119. }
  120. struct cell* builtin_vector_ref(struct cell* args)
  121. {
  122. require(nil != args, "vector-ref requires an argument\n");
  123. require(nil != args->cdr, "vector-ref requires an argument\n");
  124. require(nil == args->cdr->cdr, "vector-ref received too many arguments\n");
  125. require(VECTOR == args->car->type, "vector-ref did not receive vector\n");
  126. require(INT == args->cdr->car->type, "vector-ref did not receive index\n");
  127. return vector_ref(args->car, args->cdr->car->value);
  128. }
  129. struct cell* builtin_vector_set(struct cell* args)
  130. {
  131. require(nil != args, "vector-set! requires an argument\n");
  132. require(nil != args->cdr, "vector-set! requires a second argument\n");
  133. require(nil != args->cdr->cdr, "vector-set! requires a third argument\n");
  134. require(nil == args->cdr->cdr->cdr, "vector-set! recieved too many argument\n");
  135. require(VECTOR == args->car->type, "vector-set! did not receive a vector\n");
  136. require(INT == args->cdr->car->type, "vector-set! did not receive an index\n");
  137. return vector_set(args->car, args->cdr->car->value, args->cdr->cdr->car);
  138. }
  139. struct cell* builtin_vector_to_list(struct cell* args)
  140. {
  141. require(nil != args, "vector->list! requires an argument\n");
  142. require(VECTOR == args->car->type, "vector->list! ");
  143. require(nil == args->cdr, "vector-set! too many arguments\n");
  144. return vector_to_list(args->car);
  145. }
  146. struct cell* builtin_list_to_vector(struct cell* args)
  147. {
  148. require(nil != args, "list->vector requires an argument\n");
  149. require(nil == args->cdr, "list->vector only allows a single argument\n");
  150. return list_to_vector(args->car);
  151. }
  152. struct cell* builtin_vectorp(struct cell* args)
  153. {
  154. require(nil != args, "vector? requires arguments\n");
  155. require(nil == args->cdr, "vector? recieved too many arguments\n");
  156. if(VECTOR == args->car->type) return cell_t;
  157. return cell_f;
  158. }