No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array.lua 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. local function arrayAllocated(a)
  2. return blockAllocated(a) and a.size >= 5
  3. end
  4. function Array(elements, elementSize)
  5. local self = {
  6. }
  7. return self
  8. end
  9. function ainit(a)
  10. assert(arrayAllocated(a))
  11. local s = a.address
  12. local e = a.address + a.size - 1
  13. local tmp = e - 1
  14. for i = 1, tmp - 2 do
  15. if i % 2 == 0 then
  16. to(cell(a.address + i))
  17. inc()
  18. end
  19. end
  20. to(a)
  21. end
  22. function aset(a, i, v)
  23. assert(arrayAllocated(a))
  24. assert(allocated(i))
  25. assert(allocated(v))
  26. local s = cell(a.address)
  27. local e = cell(a.address + a.size - 1)
  28. local tmp = cell(e.address - 1)
  29. copy(tmp, i)
  30. to(s)
  31. right(2)
  32. open()
  33. dec()
  34. emit(">>")
  35. open()
  36. emit(">>")
  37. close(true)
  38. emit("<")
  39. open()
  40. dec()
  41. emit("<")
  42. open()
  43. emit("<<")
  44. close(true)
  45. close(true)
  46. inc()
  47. emit(">>")
  48. close(true)
  49. emit("<<")
  50. dec()
  51. at(tmp)
  52. copy(tmp, v)
  53. emit("<")
  54. open()
  55. emit("<<")
  56. close(true)
  57. emit("<[-]>>>")
  58. open()
  59. emit(">>")
  60. close(true)
  61. emit("<")
  62. open()
  63. dec()
  64. emit("<")
  65. open()
  66. emit("<<")
  67. close(true)
  68. emit("<+>>>")
  69. open()
  70. emit(">>")
  71. close(true)
  72. emit("<")
  73. close()
  74. emit("<")
  75. open()
  76. emit("<<")
  77. close(true)
  78. inc()
  79. open()
  80. emit("<<")
  81. close(true)
  82. at(s)
  83. end
  84. function aget(r, a, i)
  85. assert(allocated(r))
  86. assert(arrayAllocated(a))
  87. assert(allocated(i))
  88. local s = a
  89. local e = cell(a.address + a.size - 1)
  90. local tmp = cell(e.address - 1)
  91. local tmp2 = alloc()
  92. to(r)
  93. clear()
  94. copy(tmp, i)
  95. to(s)
  96. right(2)
  97. open()
  98. dec()
  99. emit(">>")
  100. open()
  101. emit(">>")
  102. close(true)
  103. emit("<")
  104. open()
  105. dec()
  106. emit("<")
  107. open()
  108. emit("<<")
  109. close(true)
  110. close(true)
  111. inc()
  112. emit(">>")
  113. close(true)
  114. emit("<<")
  115. dec()
  116. at(tmp)
  117. emit("<")
  118. open()
  119. emit("<<")
  120. close(true)
  121. emit("<")
  122. open()
  123. dec()
  124. emit(">>>")
  125. open()
  126. emit(">>")
  127. close(true)
  128. emit("<+<")
  129. open()
  130. emit("<<")
  131. close(true)
  132. emit("<")
  133. close()
  134. emit(">>>")
  135. open()
  136. emit(">>")
  137. close(true)
  138. at(e)
  139. copy(r, tmp)
  140. to(tmp)
  141. open()
  142. dec()
  143. emit("<")
  144. open()
  145. emit("<<")
  146. close(true)
  147. emit("<+>>>")
  148. open()
  149. emit(">>")
  150. close(true)
  151. emit("<")
  152. close(true)
  153. emit("<")
  154. open()
  155. emit("<<")
  156. close(true)
  157. inc()
  158. open()
  159. emit(">>")
  160. close(true)
  161. at(e)
  162. to(r)
  163. free(tmp2)
  164. end
  165. function alen(r, a)
  166. assert(allocated(r))
  167. assert(arrayAllocated(a))
  168. local s = a.address
  169. local e = a.address + a.size - 1
  170. local tmp = e - 1
  171. set(tmp, 0)
  172. to(s + 2)
  173. open()
  174. dec()
  175. emit(">>")
  176. open()
  177. emit(">>")
  178. close(true)
  179. emit("<")
  180. inc()
  181. emit("<")
  182. open()
  183. emit("<<")
  184. close(true)
  185. inc()
  186. emit(">>")
  187. close()
  188. at(e)
  189. move(r, tmp)
  190. end