vector.g 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. const VECTOR_DATA 0
  15. const VECTOR_SIZE 4
  16. const VECTOR_CAP 8
  17. const VECTOR_SIZEOF_ELEM 12
  18. const SIZEOF_VECTOR 16
  19. const INITIAL_CAP 1
  20. fun vector_init 1 {
  21. $vptr
  22. @vptr SIZEOF_VECTOR malloc = ;
  23. vptr VECTOR_SIZE take_addr 0 = ;
  24. vptr VECTOR_CAP take_addr INITIAL_CAP = ;
  25. vptr VECTOR_SIZEOF_ELEM take_addr 0 param = ;
  26. vptr VECTOR_DATA take_addr vptr VECTOR_CAP take vptr VECTOR_SIZEOF_ELEM take * malloc = ;
  27. vptr ret ;
  28. }
  29. fun vector_destroy 1 {
  30. $vptr
  31. @vptr 0 param = ;
  32. vptr VECTOR_DATA take free ;
  33. vptr free ;
  34. }
  35. fun vector_clear 1 {
  36. $vptr
  37. @vptr 0 param = ;
  38. vptr VECTOR_SIZE take_addr 0 = ;
  39. }
  40. fun vector_data 1 {
  41. $vptr
  42. @vptr 0 param = ;
  43. vptr VECTOR_DATA take ret ;
  44. }
  45. fun vector_at_addr 2 {
  46. $vptr
  47. $idx
  48. @vptr 1 param = ;
  49. @idx 0 param = ;
  50. idx 0 >= "vector_at_addr: negative vector access" idx vptr VECTOR_SIZE take assert_msg_int_int ;
  51. idx vptr VECTOR_SIZE take < "vector_at_addr: vector access out of bounds" idx vptr VECTOR_SIZE take assert_msg_int_int ;
  52. vptr VECTOR_DATA take idx vptr VECTOR_SIZEOF_ELEM take * + ret ;
  53. }
  54. fun vector_at 2 {
  55. $vptr
  56. $idx
  57. @vptr 1 param = ;
  58. @idx 0 param = ;
  59. idx 0 >= "vector_at: negative vector access" idx vptr VECTOR_SIZE take assert_msg_int_int ;
  60. idx vptr VECTOR_SIZE take < "vector_at: vector access out of bounds" idx vptr VECTOR_SIZE take assert_msg_int_int ;
  61. vptr VECTOR_DATA take idx vptr VECTOR_SIZEOF_ELEM take * + ** ret ;
  62. }
  63. fun vector_push_back 2 {
  64. $vptr
  65. $elem
  66. @vptr 1 param = ;
  67. @elem 0 param = ;
  68. vptr VECTOR_SIZE take vptr VECTOR_CAP take <= "Internal error: size > capacity" assert_msg ;
  69. if vptr VECTOR_SIZE take vptr VECTOR_CAP take == {
  70. vptr VECTOR_CAP take_addr vptr VECTOR_CAP take 2 * = ;
  71. vptr VECTOR_DATA take_addr vptr VECTOR_CAP take vptr VECTOR_SIZEOF_ELEM take * vptr VECTOR_DATA take realloc = ;
  72. }
  73. vptr VECTOR_SIZE take vptr VECTOR_CAP take < "Internal error: size >= capacity again" assert_msg ;
  74. vptr VECTOR_DATA take vptr VECTOR_SIZE take vptr VECTOR_SIZEOF_ELEM take * + elem = ;
  75. vptr VECTOR_SIZE take_addr vptr VECTOR_SIZE take 1 + = ;
  76. vptr VECTOR_SIZE take 1 - ret ;
  77. }
  78. fun vector_pop_back 1 {
  79. $vptr
  80. @vptr 0 param = ;
  81. vptr VECTOR_SIZE take 0 > "Popping from empty vector" assert_msg ;
  82. vptr VECTOR_SIZE take_addr vptr VECTOR_SIZE take 1 - = ;
  83. vptr VECTOR_DATA take vptr VECTOR_SIZE take vptr VECTOR_SIZEOF_ELEM take * + ** ret ;
  84. }
  85. fun vector_size 1 {
  86. $vptr
  87. @vptr 0 param = ;
  88. vptr VECTOR_SIZE take ret ;
  89. }
  90. fun vector_has 2 {
  91. $vptr
  92. $elem
  93. @vptr 1 param = ;
  94. @elem 0 param = ;
  95. $i
  96. @i 0 = ;
  97. while i vptr vector_size < {
  98. if vptr i vector_at elem == {
  99. 1 ret ;
  100. }
  101. @i i 1 + = ;
  102. }
  103. 0 ret ;
  104. }
  105. fun vector_test 0 {
  106. $v
  107. @v 4 vector_init = ;
  108. v 22 vector_push_back ;
  109. v 23 vector_push_back ;
  110. v 24 vector_push_back ;
  111. v 25 vector_push_back ;
  112. v 26 vector_push_back ;
  113. v 22 vector_push_back ;
  114. v 23 vector_push_back ;
  115. v 24 vector_push_back ;
  116. v 25 vector_push_back ;
  117. v 26 vector_push_back ;
  118. v 22 vector_push_back ;
  119. v 23 vector_push_back ;
  120. v 24 vector_push_back ;
  121. v 25 vector_push_back ;
  122. v 26 vector_push_back ;
  123. v 100 vector_push_back ;
  124. v 22 vector_push_back ;
  125. v 23 vector_push_back ;
  126. v 24 vector_push_back ;
  127. v 25 vector_push_back ;
  128. v 26 vector_push_back ;
  129. v 22 vector_push_back ;
  130. v 23 vector_push_back ;
  131. v 24 vector_push_back ;
  132. v 25 vector_push_back ;
  133. v 26 vector_push_back ;
  134. v 22 vector_push_back ;
  135. v 23 vector_push_back ;
  136. v 24 vector_push_back ;
  137. v 25 vector_push_back ;
  138. v 26 vector_push_back ;
  139. v 100 vector_push_back ;
  140. $i
  141. @i 0 = ;
  142. while i v vector_size < {
  143. $elem
  144. @elem v i vector_at = ;
  145. elem itoa log ;
  146. "\n" log ;
  147. @i i 1 + = ;
  148. }
  149. }