@shd101wyy/yo 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/README.md +4 -1
  2. package/out/cjs/index.cjs +5598 -45
  3. package/out/cjs/yo-cli.cjs +755 -764
  4. package/out/esm/index.mjs +5595 -42
  5. package/out/types/src/codegen/async/state-code-gen.d.ts +3 -3
  6. package/out/types/src/codegen/async/state-machine.d.ts +4 -4
  7. package/out/types/src/codegen/c/collection.d.ts +1 -1
  8. package/out/types/src/codegen/codegen-c.d.ts +1 -1
  9. package/out/types/src/codegen/constants.d.ts +1 -1
  10. package/out/types/src/codegen/exprs/{and_or.d.ts → and-or.d.ts} +2 -2
  11. package/out/types/src/codegen/exprs/{array_fns.d.ts → array-fns.d.ts} +2 -2
  12. package/out/types/src/codegen/exprs/assignment.d.ts +2 -2
  13. package/out/types/src/codegen/exprs/async.d.ts +2 -2
  14. package/out/types/src/codegen/exprs/atom.d.ts +2 -2
  15. package/out/types/src/codegen/exprs/await.d.ts +2 -2
  16. package/out/types/src/codegen/exprs/begin.d.ts +2 -2
  17. package/out/types/src/codegen/exprs/binding.d.ts +2 -2
  18. package/out/types/src/codegen/exprs/closures.d.ts +3 -3
  19. package/out/types/src/codegen/exprs/comptime-value.d.ts +4 -0
  20. package/out/types/src/codegen/exprs/cond.d.ts +2 -2
  21. package/out/types/src/codegen/exprs/consume.d.ts +2 -2
  22. package/out/types/src/codegen/exprs/{drop_dup.d.ts → drop-dup.d.ts} +4 -4
  23. package/out/types/src/codegen/exprs/dyn.d.ts +2 -2
  24. package/out/types/src/codegen/exprs/expr.d.ts +2 -2
  25. package/out/types/src/codegen/exprs/gc.d.ts +2 -2
  26. package/out/types/src/codegen/exprs/generation.d.ts +2 -2
  27. package/out/types/src/codegen/exprs/{initialization_assignment.d.ts → initialization-assignment.d.ts} +2 -2
  28. package/out/types/src/codegen/exprs/{inline_fns.d.ts → inline-fns.d.ts} +2 -2
  29. package/out/types/src/codegen/exprs/iso.d.ts +2 -2
  30. package/out/types/src/codegen/exprs/match.d.ts +2 -2
  31. package/out/types/src/codegen/exprs/open.d.ts +2 -2
  32. package/out/types/src/codegen/exprs/{other_fn_call.d.ts → other-fn-call.d.ts} +2 -2
  33. package/out/types/src/codegen/exprs/panic.d.ts +2 -2
  34. package/out/types/src/codegen/exprs/parallelism.d.ts +2 -2
  35. package/out/types/src/codegen/exprs/{property_access.d.ts → property-access.d.ts} +2 -2
  36. package/out/types/src/codegen/exprs/{ptr_fns.d.ts → ptr-fns.d.ts} +2 -2
  37. package/out/types/src/codegen/exprs/{rc_fns.d.ts → rc-fns.d.ts} +2 -2
  38. package/out/types/src/codegen/exprs/recur.d.ts +2 -2
  39. package/out/types/src/codegen/exprs/return.d.ts +2 -2
  40. package/out/types/src/codegen/exprs/sizeof.d.ts +2 -2
  41. package/out/types/src/codegen/exprs/{tuple_fn.d.ts → tuple-fn.d.ts} +2 -2
  42. package/out/types/src/codegen/exprs/while.d.ts +2 -2
  43. package/out/types/src/codegen/functions/collection.d.ts +4 -3
  44. package/out/types/src/codegen/functions/context.d.ts +6 -6
  45. package/out/types/src/codegen/functions/declarations.d.ts +4 -4
  46. package/out/types/src/codegen/functions/dyn.d.ts +1 -1
  47. package/out/types/src/codegen/functions/generation.d.ts +5 -5
  48. package/out/types/src/codegen/types/collection.d.ts +4 -4
  49. package/out/types/src/codegen/types/dyn.d.ts +2 -2
  50. package/out/types/src/codegen/types/generation.d.ts +2 -2
  51. package/out/types/src/codegen/utils/fixup.d.ts +1 -1
  52. package/out/types/src/codegen/utils/index.d.ts +6 -6
  53. package/out/types/src/env.d.ts +34 -5
  54. package/out/types/src/error.d.ts +1 -1
  55. package/out/types/src/evaluator/async/await-analysis-types.d.ts +1 -1
  56. package/out/types/src/evaluator/async/await-analysis.d.ts +1 -1
  57. package/out/types/src/evaluator/builtins/alignof.d.ts +3 -3
  58. package/out/types/src/evaluator/builtins/{and_or.d.ts → and-or.d.ts} +3 -3
  59. package/out/types/src/evaluator/builtins/{array_fns.d.ts → array-fns.d.ts} +3 -3
  60. package/out/types/src/evaluator/builtins/as.d.ts +3 -3
  61. package/out/types/src/evaluator/builtins/{async_fns.d.ts → async-fns.d.ts} +3 -3
  62. package/out/types/src/evaluator/builtins/comptime-assert.d.ts +8 -0
  63. package/out/types/src/evaluator/builtins/comptime-bool-fns.d.ts +8 -0
  64. package/out/types/src/evaluator/builtins/comptime-expect-error.d.ts +8 -0
  65. package/out/types/src/evaluator/builtins/comptime-fn.d.ts +8 -0
  66. package/out/types/src/evaluator/builtins/comptime-list-fns.d.ts +33 -0
  67. package/out/types/src/evaluator/builtins/comptime-numeric-fns.d.ts +8 -0
  68. package/out/types/src/evaluator/builtins/comptime-print.d.ts +8 -0
  69. package/out/types/src/evaluator/builtins/comptime-string-fns.d.ts +8 -0
  70. package/out/types/src/evaluator/builtins/consume.d.ts +3 -3
  71. package/out/types/src/evaluator/builtins/drop.d.ts +3 -3
  72. package/out/types/src/evaluator/builtins/dup.d.ts +3 -3
  73. package/out/types/src/evaluator/builtins/{expr_fns.d.ts → expr-fns.d.ts} +3 -3
  74. package/out/types/src/evaluator/builtins/{future_fns.d.ts → future-fns.d.ts} +3 -3
  75. package/out/types/src/evaluator/builtins/gc.d.ts +3 -3
  76. package/out/types/src/evaluator/builtins/gensym.d.ts +3 -3
  77. package/out/types/src/evaluator/builtins/{impl_constraint.d.ts → impl-constraint.d.ts} +3 -3
  78. package/out/types/src/evaluator/builtins/{macro_expand.d.ts → macro-expand.d.ts} +3 -3
  79. package/out/types/src/evaluator/builtins/panic.d.ts +3 -3
  80. package/out/types/src/evaluator/builtins/process.d.ts +3 -3
  81. package/out/types/src/evaluator/builtins/{ptr_fns.d.ts → ptr-fns.d.ts} +3 -3
  82. package/out/types/src/evaluator/builtins/quote.d.ts +3 -3
  83. package/out/types/src/evaluator/builtins/{rc_fns.d.ts → rc-fns.d.ts} +3 -3
  84. package/out/types/src/evaluator/builtins/rc.d.ts +3 -3
  85. package/out/types/src/evaluator/builtins/sizeof.d.ts +3 -3
  86. package/out/types/src/evaluator/builtins/the.d.ts +3 -3
  87. package/out/types/src/evaluator/builtins/{type_fns.d.ts → type-fns.d.ts} +3 -3
  88. package/out/types/src/evaluator/builtins/{va_start.d.ts → va-start.d.ts} +3 -3
  89. package/out/types/src/evaluator/builtins/{var_fns.d.ts → var-fns.d.ts} +3 -3
  90. package/out/types/src/evaluator/calls/{array_type.d.ts → array-type.d.ts} +4 -4
  91. package/out/types/src/evaluator/calls/array.d.ts +5 -5
  92. package/out/types/src/evaluator/calls/{closure_type.d.ts → closure-type.d.ts} +4 -4
  93. package/out/types/src/evaluator/calls/comptime-fn.d.ts +19 -0
  94. package/out/types/src/evaluator/calls/comptime-list-type.d.ts +11 -0
  95. package/out/types/src/evaluator/calls/{function_type.d.ts → function-type.d.ts} +5 -5
  96. package/out/types/src/evaluator/calls/function.d.ts +6 -6
  97. package/out/types/src/evaluator/calls/helper.d.ts +6 -6
  98. package/out/types/src/evaluator/calls/iso.d.ts +4 -4
  99. package/out/types/src/evaluator/calls/{module_type.d.ts → module-type.d.ts} +4 -4
  100. package/out/types/src/evaluator/calls/{numeric_type.d.ts → numeric-type.d.ts} +4 -4
  101. package/out/types/src/evaluator/calls/{pointer_type.d.ts → pointer-type.d.ts} +4 -4
  102. package/out/types/src/evaluator/calls/pointer.d.ts +3 -3
  103. package/out/types/src/evaluator/calls/{trait_type.d.ts → trait-type.d.ts} +4 -4
  104. package/out/types/src/evaluator/calls/type.d.ts +4 -4
  105. package/out/types/src/evaluator/context.d.ts +6 -6
  106. package/out/types/src/evaluator/ctfe/ctfe-analysis.d.ts +5 -5
  107. package/out/types/src/evaluator/exprs/_expr.d.ts +3 -3
  108. package/out/types/src/evaluator/exprs/assignment.d.ts +3 -3
  109. package/out/types/src/evaluator/exprs/begin.d.ts +3 -3
  110. package/out/types/src/evaluator/exprs/binding.d.ts +3 -3
  111. package/out/types/src/evaluator/exprs/{c_include.d.ts → c-include.d.ts} +3 -3
  112. package/out/types/src/evaluator/exprs/cond.d.ts +3 -3
  113. package/out/types/src/evaluator/exprs/{destructuring_assignment.d.ts → destructuring-assignment.d.ts} +5 -5
  114. package/out/types/src/evaluator/exprs/expr.d.ts +3 -3
  115. package/out/types/src/evaluator/exprs/extern.d.ts +3 -3
  116. package/out/types/src/evaluator/exprs/{identifer_and_operator.d.ts → identifer-and-operator.d.ts} +3 -3
  117. package/out/types/src/evaluator/exprs/import.d.ts +3 -3
  118. package/out/types/src/evaluator/exprs/{initialization_assignment.d.ts → initialization-assignment.d.ts} +3 -3
  119. package/out/types/src/evaluator/exprs/match.d.ts +3 -3
  120. package/out/types/src/evaluator/exprs/open.d.ts +3 -3
  121. package/out/types/src/evaluator/exprs/{property_access.d.ts → property-access.d.ts} +3 -3
  122. package/out/types/src/evaluator/exprs/recur.d.ts +3 -3
  123. package/out/types/src/evaluator/exprs/runtime.d.ts +3 -3
  124. package/out/types/src/evaluator/exprs/subtype-of.d.ts +8 -0
  125. package/out/types/src/evaluator/exprs/test.d.ts +3 -3
  126. package/out/types/src/evaluator/exprs/typeof.d.ts +3 -3
  127. package/out/types/src/evaluator/exprs/while.d.ts +3 -3
  128. package/out/types/src/evaluator/index.d.ts +4 -4
  129. package/out/types/src/evaluator/trait-checking.d.ts +31 -0
  130. package/out/types/src/evaluator/types/array.d.ts +3 -3
  131. package/out/types/src/evaluator/types/closure.d.ts +3 -3
  132. package/out/types/src/evaluator/types/comptime-list.d.ts +8 -0
  133. package/out/types/src/evaluator/types/{concrete_trait.d.ts → concrete-trait.d.ts} +3 -3
  134. package/out/types/src/evaluator/types/dyn.d.ts +3 -3
  135. package/out/types/src/evaluator/types/enum.d.ts +3 -3
  136. package/out/types/src/evaluator/types/{expr_synthesizer.d.ts → expr-synthesizer.d.ts} +4 -4
  137. package/out/types/src/evaluator/types/field.d.ts +4 -4
  138. package/out/types/src/evaluator/types/{fn_trait.d.ts → fn-trait.d.ts} +3 -3
  139. package/out/types/src/evaluator/types/function.d.ts +16 -10
  140. package/out/types/src/evaluator/types/{future_trait.d.ts → future-trait.d.ts} +3 -3
  141. package/out/types/src/evaluator/types/module.d.ts +4 -4
  142. package/out/types/src/evaluator/types/newtype.d.ts +3 -3
  143. package/out/types/src/evaluator/types/object.d.ts +3 -3
  144. package/out/types/src/evaluator/types/slice.d.ts +3 -3
  145. package/out/types/src/evaluator/types/struct.d.ts +3 -3
  146. package/out/types/src/evaluator/types/synthesizer.d.ts +2 -2
  147. package/out/types/src/evaluator/types/trait.d.ts +4 -4
  148. package/out/types/src/evaluator/types/tuple.d.ts +4 -4
  149. package/out/types/src/evaluator/types/union.d.ts +3 -3
  150. package/out/types/src/evaluator/types/utils.d.ts +84 -5
  151. package/out/types/src/evaluator/types/validation.d.ts +2 -2
  152. package/out/types/src/evaluator/utils/closure.d.ts +7 -7
  153. package/out/types/src/evaluator/utils.d.ts +2 -2
  154. package/out/types/src/evaluator/values/{anonymous_function.d.ts → anonymous-function.d.ts} +3 -3
  155. package/out/types/src/evaluator/values/{anonymous_module.d.ts → anonymous-module.d.ts} +5 -5
  156. package/out/types/src/evaluator/values/{anonymous_struct.d.ts → anonymous-struct.d.ts} +3 -3
  157. package/out/types/src/evaluator/values/array.d.ts +3 -3
  158. package/out/types/src/evaluator/values/boolean.d.ts +2 -2
  159. package/out/types/src/evaluator/values/char.d.ts +2 -2
  160. package/out/types/src/evaluator/values/{clone_value.d.ts → clone-value.d.ts} +1 -1
  161. package/out/types/src/evaluator/values/comptime-list.d.ts +8 -0
  162. package/out/types/src/evaluator/values/dyn.d.ts +3 -3
  163. package/out/types/src/evaluator/values/float.d.ts +3 -3
  164. package/out/types/src/evaluator/values/impl.d.ts +5 -5
  165. package/out/types/src/evaluator/values/integer.d.ts +3 -3
  166. package/out/types/src/evaluator/values/string.d.ts +2 -2
  167. package/out/types/src/evaluator/values/tuple.d.ts +5 -5
  168. package/out/types/src/expr.d.ts +283 -283
  169. package/out/types/src/function-value.d.ts +6 -6
  170. package/out/types/src/index.d.ts +3 -0
  171. package/out/types/src/lexer.d.ts +1 -1
  172. package/out/types/src/module-manager.d.ts +1 -1
  173. package/out/types/src/parser.d.ts +2 -2
  174. package/out/types/src/test-runner.d.ts +1 -1
  175. package/out/types/src/type-value.d.ts +1 -1
  176. package/out/types/src/types/compatibility.d.ts +2 -2
  177. package/out/types/src/types/creators.d.ts +29 -16
  178. package/out/types/src/types/definitions.d.ts +29 -26
  179. package/out/types/src/types/env-lookup.d.ts +4 -0
  180. package/out/types/src/types/guards.d.ts +6 -6
  181. package/out/types/src/types/hierarchy.d.ts +2 -2
  182. package/out/types/src/types/tags.d.ts +4 -4
  183. package/out/types/src/types/utils.d.ts +13 -38
  184. package/out/types/src/unit-value.d.ts +1 -1
  185. package/out/types/src/value-tag.d.ts +4 -4
  186. package/out/types/src/value.d.ts +32 -26
  187. package/out/types/tsconfig.tsbuildinfo +1 -1
  188. package/package.json +3 -1
  189. package/std/allocator.yo +5 -5
  190. package/std/collections/array_list.yo +394 -390
  191. package/std/collections/hash_map.yo +411 -406
  192. package/std/collections/hash_set.yo +646 -642
  193. package/std/collections/linked_list.yo +347 -341
  194. package/std/io.yo +55 -53
  195. package/std/path.yo +17 -16
  196. package/std/prelude.yo +1587 -1455
  197. package/std/string/rune.yo +25 -24
  198. package/std/string/string.yo +46 -43
  199. package/std/sync.yo +50 -30
  200. package/std/thread.yo +6 -5
  201. package/std/time.yo +1 -1
  202. package/out/types/src/codegen/exprs/compt_value.d.ts +0 -4
  203. package/out/types/src/codegen/types/index.d.ts +0 -3
  204. package/out/types/src/codegen/values/index.d.ts +0 -1
  205. package/out/types/src/evaluator/builtins/compt_assert.d.ts +0 -8
  206. package/out/types/src/evaluator/builtins/compt_boolean_fns.d.ts +0 -8
  207. package/out/types/src/evaluator/builtins/compt_expect_error.d.ts +0 -8
  208. package/out/types/src/evaluator/builtins/compt_fn.d.ts +0 -8
  209. package/out/types/src/evaluator/builtins/compt_list_fns.d.ts +0 -33
  210. package/out/types/src/evaluator/builtins/compt_numeric_fns.d.ts +0 -8
  211. package/out/types/src/evaluator/builtins/compt_print.d.ts +0 -8
  212. package/out/types/src/evaluator/builtins/compt_string_fns.d.ts +0 -8
  213. package/out/types/src/evaluator/calls/compt_function.d.ts +0 -19
  214. package/out/types/src/evaluator/calls/compt_list_type.d.ts +0 -11
  215. package/out/types/src/evaluator/exprs/subtype_of.d.ts +0 -21
  216. package/out/types/src/evaluator/types/compt_list.d.ts +0 -8
  217. package/out/types/src/evaluator/values/anonymous_trait.d.ts +0 -17
  218. package/out/types/src/evaluator/values/compt_list.d.ts +0 -8
  219. package/out/types/src/types/constants.d.ts +0 -5
  220. package/out/types/src/types/index.d.ts +0 -8
  221. package/std/allocators/c_allocator.yo +0 -118
  222. package/std/index.yo +0 -12
@@ -28,414 +28,418 @@ ArrayListError :: enum(
28
28
  * - _length: current number of elements
29
29
  * - _capacity: total capacity of the buffer
30
30
  */
31
- ArrayList :: (fn(compt(T): Type) -> compt(Type))
31
+ ArrayList :: (fn(comptime(T): Type) -> comptime(Type))(
32
32
  object(
33
33
  _ptr : ?*(T),
34
34
  _length : usize,
35
- _capacity : usize,
36
-
37
- len :: (fn(self: Self) -> usize)({
38
- return self._length;
39
- }),
40
-
41
- capacity :: (fn(self: Self) -> usize)({
42
- return self._capacity;
43
- }),
35
+ _capacity : usize
36
+ )
37
+ );
38
+ impl(forall(T : Type), ArrayList(T),
39
+ len : (fn(self: Self) -> usize)({
40
+ return self._length;
41
+ }),
44
42
 
45
- /**
46
- * Get a pointer to the underlying data buffer
47
- * Returns .None if the list is empty or has no allocated buffer
48
- */
49
- ptr :: (fn(self: Self) -> ?*(T))(
50
- self._ptr
51
- ),
43
+ capacity : (fn(self: Self) -> usize)({
44
+ return self._capacity;
45
+ }),
52
46
 
53
- /** * Get a slice view of the ArrayList
54
- * Returns a fat pointer [T] with data pointer and length
55
- * This allows passing ArrayList to functions expecting slices
56
- */
57
- // as_slice :: (fn(self: Self) -> [T])({
58
- // ptr := match(self._ptr,
59
- // .Some(p) => p,
60
- // .None => (*(T))(*(void)(usize(0))) // NULL for empty list
61
- // );
62
- // return [T](ptr, self._length);
63
- // }),
47
+ /**
48
+ * Get a pointer to the underlying data buffer
49
+ * Returns .None if the list is empty or has no allocated buffer
50
+ */
51
+ ptr : (fn(self: Self) -> ?*(T))(
52
+ self._ptr
53
+ ),
64
54
 
65
- /** * Directly set the length of the ArrayList
66
- * UNSAFE: This does not initialize new elements or free removed elements
67
- * Only use when you've manually populated the buffer (e.g., via direct memory writes)
68
- */
69
- set_len :: (fn(self: Self, new_len: usize) -> unit)({
70
- cond(
71
- (new_len <= self._capacity) => {
72
- self._length = new_len;
73
- },
74
- true => panic("set_len: new length exceeds capacity")
75
- );
76
- }),
55
+ /**
56
+ * Get a slice view of the ArrayList
57
+ * Returns a fat pointer [T] with data pointer and length
58
+ * This allows passing ArrayList to functions expecting slices
59
+ */
60
+ // as_slice :: (fn(self: Self) -> [T])({
61
+ // ptr := match(self._ptr,
62
+ // .Some(p) => p,
63
+ // .None => (*(T))(*(void)(usize(0))) // NULL for empty list
64
+ // );
65
+ // return [T](ptr, self._length);
66
+ // }),
77
67
 
78
- /**
79
- * Create a new empty ArrayList
80
- */
81
- new :: (fn() -> Self)(
82
- Self(
83
- _ptr: .None,
84
- _length: usize(0),
85
- _capacity: usize(0)
86
- )),
68
+ /** * Directly set the length of the ArrayList
69
+ * UNSAFE: This does not initialize new elements or free removed elements
70
+ * Only use when you've manually populated the buffer (e.g., via direct memory writes)
71
+ */
72
+ set_len : (fn(self: Self, new_len: usize) -> unit)({
73
+ cond(
74
+ (new_len <= self._capacity) => {
75
+ self._length = new_len;
76
+ },
77
+ true => panic("set_len: new length exceeds capacity")
78
+ );
79
+ }),
87
80
 
88
- /**
89
- * Create an ArrayList with a specific initial capacity
90
- */
91
- with_capacity :: (fn(capacity: usize) -> Self)(
92
- cond(
93
- (capacity == usize(0)) =>
94
- Self.new(),
95
- true => {
96
- ptr_result := malloc(sizeof(T) * capacity);
97
- match(ptr_result,
98
- .Some(ptr) =>
99
- Self(
100
- _ptr: .Some((*(T))(ptr)),
101
- _length: usize(0),
102
- _capacity: capacity
103
- ),
104
- .None => panic("malloc returned None")
105
- )
106
- }
107
- )
108
- ),
81
+ /**
82
+ * Create a new empty ArrayList
83
+ */
84
+ new : (fn() -> Self)(
85
+ Self(
86
+ _ptr: .None,
87
+ _length: usize(0),
88
+ _capacity: usize(0)
89
+ )),
109
90
 
110
- /**
111
- * Add an element to the end of the ArrayList
112
- */
113
- push :: (fn(self: Self, value: T) -> Result(unit, ArrayListError))(
114
- // Check if we need to grow
115
- cond(
116
- (self._length >= self._capacity) => {
117
- // Need to grow - calculate new capacity
118
- new_capacity := cond
119
- (self._capacity == usize(0)) => usize(4),
120
- true => (self._capacity * usize(2));
121
-
122
- // Allocate or reallocate
123
- new_some_ptr := match self._ptr,
124
- .None => GlobalAllocator.malloc(sizeof(T) * new_capacity),
125
- .Some(old_ptr) => GlobalAllocator.realloc(
126
- .Some((*(void))(old_ptr)),
127
- sizeof(T) * new_capacity
128
- );
129
-
130
- match(new_some_ptr,
131
- .Some(new_ptr) => {
132
- typed_ptr := (*(T))(new_ptr);
133
- self._ptr = .Some(typed_ptr);
134
- self._capacity = new_capacity;
135
- // Now add the element at position length
136
- target_ptr := (typed_ptr &+ self._length);
137
- consume(target_ptr.* = value);
138
- self._length = (self._length + usize(1));
139
- .Ok(())
140
- },
141
- .None => .Err(.AllocError(.OutOfMemory))
142
- )
143
- },
144
- true =>
145
- // Have capacity - just add the element
146
- match(self._ptr,
147
- .Some(ptr) => {
148
- target_ptr := (ptr &+ self._length);
149
- consume(target_ptr.* = value);
150
- self._length = (self._length + usize(1));
151
- .Ok(())
152
- },
153
- .None => panic("ArrayList has capacity but no ptr")
154
- )
155
- )
156
- ),
91
+ /**
92
+ * Create an ArrayList with a specific initial cap
93
+ */
94
+ with_capacity : (fn(cap: usize) -> Self)(
95
+ cond(
96
+ (cap == usize(0)) =>
97
+ Self.new(),
98
+ true => {
99
+ ptr_result := malloc(sizeof(T) * cap);
100
+ match(ptr_result,
101
+ .Some(_ptr) =>
102
+ Self(
103
+ _ptr: .Some((*(T))(_ptr)),
104
+ _length: usize(0),
105
+ _capacity: cap
106
+ ),
107
+ .None => panic("malloc returned None")
108
+ )
109
+ }
110
+ )
111
+ ),
157
112
 
158
- /**
159
- * Remove and return the last element
160
- */
161
- pop :: (fn(self: Self) -> Option(T))(
162
- cond(
163
- (self._length == usize(0)) => .None,
164
- true => {
165
- self._length = (self._length - usize(1));
166
- match(self._ptr,
167
- .None => .None,
168
- .Some(ptr) => {
169
- last_element_ptr := (ptr &+ self._length);
170
- last_element := last_element_ptr.*;
171
- // Drop the slot's reference since it's no longer tracked
172
- // (length was decremented, so dispose won't reach this slot)
173
- unsafe.drop(last_element_ptr.*);
174
- .Some(last_element)
175
- }
176
- )
177
- }
178
- )
179
- ),
113
+ /**
114
+ * Add an element to the end of the ArrayList
115
+ */
116
+ push : (fn(self: Self, value: T) -> Result(unit, ArrayListError))(
117
+ // Check if we need to grow
118
+ cond(
119
+ (self._length >= self._capacity) => {
120
+ // Need to grow - calculate new capacity
121
+ new_capacity := cond
122
+ (self._capacity == usize(0)) => usize(4),
123
+ true => (self._capacity * usize(2));
124
+
125
+ // Allocate or reallocate
126
+ new_some_ptr := match self._ptr,
127
+ .None => GlobalAllocator.malloc(sizeof(T) * new_capacity),
128
+ .Some(old_ptr) => GlobalAllocator.realloc(
129
+ .Some((*(void))(old_ptr)),
130
+ sizeof(T) * new_capacity
131
+ );
132
+
133
+ match(new_some_ptr,
134
+ .Some(new_ptr) => {
135
+ typed_ptr := *(T)(new_ptr);
136
+ self._ptr = .Some(typed_ptr);
137
+ self._capacity = new_capacity;
138
+ // Now add the element at position length
139
+ target_ptr := (typed_ptr &+ self._length);
140
+ consume(target_ptr.* = value);
141
+ self._length = (self._length + usize(1));
142
+ .Ok(())
143
+ },
144
+ .None => .Err(.AllocError(.OutOfMemory))
145
+ )
146
+ },
147
+ true =>
148
+ // Have capacity - just add the element
149
+ match(self._ptr,
150
+ .Some(_ptr) => {
151
+ target_ptr := (_ptr &+ self._length);
152
+ consume(target_ptr.* = value);
153
+ self._length = (self._length + usize(1));
154
+ .Ok(())
155
+ },
156
+ .None => panic("ArrayList has capacity but no ptr")
157
+ )
158
+ )
159
+ ),
180
160
 
181
- /**
182
- * Get an element by index (bounds checked)
183
- */
184
- get :: (fn(self: Self, index: usize) -> Option(T))(
185
- cond(
186
- (index >= self._length) => .None,
187
- true =>
188
- match(self._ptr,
189
- .None => .None,
190
- .Some(ptr) => .Some((ptr &+ index).*)
191
- )
192
- )
193
- ),
161
+ /**
162
+ * Remove and return the last element
163
+ */
164
+ pop : (fn(self: Self) -> Option(T))(
165
+ cond(
166
+ (self._length == usize(0)) => .None,
167
+ true => {
168
+ self._length = (self._length - usize(1));
169
+ match(self._ptr,
170
+ .None => .None,
171
+ .Some(_ptr) => {
172
+ last_element_ptr := (_ptr &+ self._length);
173
+ last_element := last_element_ptr.*;
174
+ // Drop the slot's reference since it's no longer tracked
175
+ // (length was decremented, so dispose won't reach this slot)
176
+ unsafe.drop(last_element_ptr.*);
177
+ .Some(last_element)
178
+ }
179
+ )
180
+ }
181
+ )
182
+ ),
194
183
 
195
- /**
196
- * Set an element at a specific index (bounds checked)
197
- * Returns Ok(()) on success, or Error with bounds information
198
- */
199
- set :: (fn(self: Self, index: usize, value: T) -> Result(unit, ArrayListError))(
200
- cond(
201
- (index >= self._length) =>
202
- .Err(.IndexOutOfBounds(index: index, length: self._length)),
203
- true =>
204
- match(self._ptr,
205
- .None => panic("ArrayList has length but no ptr"),
206
- .Some(ptr) => {
207
- target_ptr := (ptr &+ index);
208
- target_ptr.* = value;
209
- .Ok(())
210
- }
211
- )
212
- )
213
- ),
184
+ /**
185
+ * Get an element by index (bounds checked)
186
+ */
187
+ get : (fn(self: Self, index: usize) -> Option(T))(
188
+ cond(
189
+ (index >= self._length) => .None,
190
+ true =>
191
+ match(self._ptr,
192
+ .None => .None,
193
+ .Some(_ptr) => .Some((_ptr &+ index).*)
194
+ )
195
+ )
196
+ ),
214
197
 
215
- /**
216
- * Shrink the capacity to match the current length
217
- * This reduces memory usage but may cause reallocation on next push
218
- */
219
- shrink_to_fit :: ((fn(self: Self) -> Result(unit, ArrayListError))
220
- // If length equals capacity or both are zero, nothing to do
221
- cond(
222
- ((self._length == self._capacity) || (self._capacity == usize(0))) => .Ok(()),
223
- true =>
224
- // If length is zero, free the buffer
225
- cond(
226
- (self._length == usize(0)) => {
227
- match(self._ptr,
228
- .Some(ptr) => {
229
- GlobalAllocator.free(.Some(*(void)(ptr)));
230
- self._ptr = .None;
231
- self._capacity = usize(0);
232
- },
233
- .None => ());
234
- .Ok(())
235
- },
236
- true =>
237
- // Otherwise, reallocate to exact size
238
- match(self._ptr,
239
- .None => panic("ArrayList has capacity but no ptr"),
240
- .Some(old_ptr) => {
241
- new_some_ptr := GlobalAllocator.realloc(
242
- .Some(*(void)(old_ptr)),
243
- sizeof(T) * self._length
244
- );
245
- match(new_some_ptr,
246
- .Some(new_ptr) => {
247
- self._ptr = .Some(*(T)(new_ptr));
248
- self._capacity = self._length;
249
- .Ok(())
250
- },
251
- .None => .Err(.AllocError(.OutOfMemory))
252
- )
253
- }
254
- )
255
- )
256
- )
257
- ),
198
+ /**
199
+ * Set an element at a specific index (bounds checked)
200
+ * Returns Ok(()) on success, or Error with bounds information
201
+ */
202
+ set : (fn(self: Self, index: usize, value: T) -> Result(unit, ArrayListError))(
203
+ cond(
204
+ (index >= self._length) =>
205
+ .Err(.IndexOutOfBounds(index: index, length: self._length)),
206
+ true =>
207
+ match(self._ptr,
208
+ .None => panic("ArrayList has length but no ptr"),
209
+ .Some(_ptr) => {
210
+ target_ptr := (_ptr &+ index);
211
+ target_ptr.* = value;
212
+ .Ok(())
213
+ }
214
+ )
215
+ )
216
+ ),
258
217
 
259
- _free_elements :: ((fn(self : Self) -> unit)
260
- cond(
261
- Type.contains_rc_type(T) => {
262
- i := usize(0);
263
- base_ptr := self._ptr.unwrap();
264
- while(i < self._length, i = (i + usize(1)), {
265
- element_ptr := (base_ptr &+ i);
266
- unsafe.drop(element_ptr.*);
267
- });
268
- },
269
- true => ()
270
- )
271
- ),
218
+ /**
219
+ * Shrink the capacity to match the current length
220
+ * This reduces memory usage but may cause reallocation on next push
221
+ */
222
+ shrink_to_fit : ((fn(self: Self) -> Result(unit, ArrayListError))
223
+ // If length equals capacity or both are zero, nothing to do
224
+ cond(
225
+ ((self._length == self._capacity) || (self._capacity == usize(0))) => .Ok(()),
226
+ true =>
227
+ // If length is zero, free the buffer
228
+ cond(
229
+ (self._length == usize(0)) => {
230
+ match(self._ptr,
231
+ .Some(_ptr) => {
232
+ GlobalAllocator.free(.Some(*(void)(_ptr)));
233
+ self._ptr = .None;
234
+ self._capacity = usize(0);
235
+ },
236
+ .None => ());
237
+ .Ok(())
238
+ },
239
+ true =>
240
+ // Otherwise, reallocate to exact size
241
+ match(self._ptr,
242
+ .None => panic("ArrayList has capacity but no ptr"),
243
+ .Some(old_ptr) => {
244
+ new_some_ptr := GlobalAllocator.realloc(
245
+ .Some(*(void)(old_ptr)),
246
+ sizeof(T) * self._length
247
+ );
248
+ match(new_some_ptr,
249
+ .Some(new_ptr) => {
250
+ self._ptr = .Some(*(T)(new_ptr));
251
+ self._capacity = self._length;
252
+ .Ok(())
253
+ },
254
+ .None => .Err(.AllocError(.OutOfMemory))
255
+ )
256
+ }
257
+ )
258
+ )
259
+ )
260
+ ),
272
261
 
273
- /**
274
- * Remove elements from the ArrayList starting at index, removing count elements
275
- * Returns the number of elements actually removed
276
- */
277
- remove :: (fn(self: Self, start: usize, (count : usize) ?= 1) -> Result(usize, ArrayListError))(
278
- // Check if start index is valid
279
- cond(
280
- (start >= self._length) =>
281
- .Err(.IndexOutOfBounds(index: start, length: self._length)),
282
- true => {
283
- // Calculate actual number of elements to remove
284
- // (can't remove more than what's available)
285
- available := (self._length - start);
286
- actual_count := cond(
287
- (count > available) => available,
288
- true => count
289
- );
290
-
291
- // If removing 0 elements, just return success
292
- cond(
293
- (actual_count == usize(0)) => .Ok(usize(0)),
294
- true =>
295
- match(self._ptr,
296
- .None => panic("ArrayList has length but no ptr"),
297
- .Some(ptr) => {
298
- // Shift remaining elements left to fill the gap
299
- // We need to move elements from [start + actual_count, length) to [start, ...)
300
- elements_after := ((self._length - start) - actual_count);
301
- new_length := (self._length - actual_count);
302
- cond(
303
- (elements_after > usize(0)) => {
304
- src_ptr := (ptr &+ (start + actual_count));
305
- dst_ptr := (ptr &+ start);
306
-
307
- cond(
308
- Type.contains_rc_type(T) => {
309
- // For RC types, iterate and properly manage RC
310
- // The assignment dst.* = src.* will:
311
- // 1. Dup src.* (RC+1)
312
- // 2. Drop old dst.* (either element being removed, or previous iteration's copy)
313
- // 3. Write to dst.*
314
- i := usize(0);
315
- while(i < elements_after, i = (i + usize(1)), {
316
- dst := (dst_ptr &+ i);
317
- src := (src_ptr &+ i);
318
- dst.* = src.*;
319
- });
320
- // Drop the non-overlapping tail stale pointers
321
- j := usize(0);
322
- tail_start := (start + elements_after);
323
- tail_count := actual_count;
324
- while(j < tail_count, j = (j + usize(1)), {
325
- tail_ptr := (ptr &+ (tail_start + j));
326
- unsafe.drop(tail_ptr.*);
327
- });
328
- },
329
- true => {
330
- // For non-RC types, use fast memmove
331
- element_size := sizeof(T);
332
- bytes_to_move := (elements_after * element_size);
333
- dst_void := *(void)(dst_ptr);
334
- src_void := *(void)(src_ptr);
335
- memmove(dst_void, src_void, bytes_to_move);
336
- }
337
- );
338
- },
339
- true => {
340
- // No elements to shift, just drop the removed elements
341
- cond(
342
- Type.contains_rc_type(T) => {
343
- i := usize(0);
344
- while(i < actual_count, i = (i + usize(1)), {
345
- element_ptr := (ptr &+ (start + i));
346
- unsafe.drop(element_ptr.*);
347
- });
348
- },
349
- true => ()
350
- );
351
- }
352
- );
353
-
354
- // Update length
355
- self._length = new_length;
356
- .Ok(actual_count)
357
- }
358
- )
359
- )
360
- }
361
- )
362
- ),
262
+ _free_elements : ((fn(self : Self) -> unit)
263
+ cond(
264
+ Type.contains_rc_type(T) => {
265
+ i := usize(0);
266
+ base_ptr := self._ptr.unwrap();
267
+ while(i < self._length, i = (i + usize(1)), {
268
+ element_ptr := (base_ptr &+ i);
269
+ unsafe.drop(element_ptr.*);
270
+ });
271
+ },
272
+ true => ()
273
+ )
274
+ ),
363
275
 
364
- /**
365
- * Create a new ArrayList containing a slice of elements from start to end (exclusive)
366
- * Similar to array slicing in many languages: array[start:end]
367
- * Returns a new ArrayList with copied elements
368
- */
369
- slice :: (fn(self: Self, start: usize, end: usize) -> Result(Self, ArrayListError))(
370
- // Validate indices
371
- cond(
372
- (start > self._length) =>
373
- .Err(.IndexOutOfBounds(index: start, length: self._length)),
374
- (end > self._length) =>
375
- .Err(.IndexOutOfBounds(index: end, length: self._length)),
376
- (start > end) =>
377
- .Err(.IndexOutOfBounds(index: start, length: end)),
378
- true => {
379
- slice_length := (end - start);
380
-
381
- // If slice is empty, return empty ArrayList
382
- cond(
383
- (slice_length == usize(0)) =>
384
- .Ok(Self.new()),
385
- true =>
386
- match(self._ptr,
387
- .None => panic("ArrayList has length but no ptr"),
388
- .Some(src_ptr) => {
389
- // Create new ArrayList with exact capacity
390
- new_list := Self.with_capacity(slice_length);
391
-
392
- match(new_list._ptr,
393
- .None => .Err(.AllocError(.OutOfMemory)),
394
- .Some(dst_ptr) => {
395
- // Copy elements
396
- i := usize(0);
397
- while(i < slice_length, i = (i + usize(1)), {
398
- src := (src_ptr &+ (start + i));
399
- dst := (dst_ptr &+ i);
400
- dst.* = src.*;
401
- });
402
-
403
- // Set the length of the new list
404
- new_list._length = slice_length;
405
- .Ok(new_list)
406
- }
407
- )
408
- }
409
- )
410
- )
411
- }
412
- )
413
- ),
276
+ /**
277
+ * Remove elements from the ArrayList starting at index, removing count elements
278
+ * Returns the number of elements actually removed
279
+ */
280
+ remove : (fn(self: Self, start: usize, (count : usize) ?= 1) -> Result(usize, ArrayListError))(
281
+ // Check if start index is valid
282
+ cond(
283
+ (start >= self._length) =>
284
+ .Err(.IndexOutOfBounds(index: start, length: self._length)),
285
+ true => {
286
+ // Calculate actual number of elements to remove
287
+ // (can't remove more than what's available)
288
+ available := (self._length - start);
289
+ actual_count := cond(
290
+ (count > available) => available,
291
+ true => count
292
+ );
293
+
294
+ // If removing 0 elements, just return success
295
+ cond(
296
+ (actual_count == usize(0)) => .Ok(usize(0)),
297
+ true =>
298
+ match(self._ptr,
299
+ .None => panic("ArrayList has length but no ptr"),
300
+ .Some(_ptr) => {
301
+ // Shift remaining elements left to fill the gap
302
+ // We need to move elements from [start + actual_count, length) to [start, ...)
303
+ elements_after := ((self._length - start) - actual_count);
304
+ new_length := (self._length - actual_count);
305
+ cond(
306
+ (elements_after > usize(0)) => {
307
+ src_ptr := (_ptr &+ (start + actual_count));
308
+ dst_ptr := (_ptr &+ start);
309
+
310
+ cond(
311
+ Type.contains_rc_type(T) => {
312
+ // For RC types, iterate and properly manage RC
313
+ // The assignment dst.* = src.* will:
314
+ // 1. Dup src.* (RC+1)
315
+ // 2. Drop old dst.* (either element being removed, or previous iteration's copy)
316
+ // 3. Write to dst.*
317
+ i := usize(0);
318
+ while(i < elements_after, i = (i + usize(1)), {
319
+ dst := (dst_ptr &+ i);
320
+ src := (src_ptr &+ i);
321
+ dst.* = src.*;
322
+ });
323
+ // Drop the non-overlapping tail stale pointers
324
+ j := usize(0);
325
+ tail_start := (start + elements_after);
326
+ tail_count := actual_count;
327
+ while(j < tail_count, j = (j + usize(1)), {
328
+ tail_ptr := (_ptr &+ (tail_start + j));
329
+ unsafe.drop(tail_ptr.*);
330
+ });
331
+ },
332
+ true => {
333
+ // For non-RC types, use fast memmove
334
+ element_size := sizeof(T);
335
+ bytes_to_move := (elements_after * element_size);
336
+ dst_void := *(void)(dst_ptr);
337
+ src_void := *(void)(src_ptr);
338
+ memmove(dst_void, src_void, bytes_to_move);
339
+ }
340
+ );
341
+ },
342
+ true => {
343
+ // No elements to shift, just drop the removed elements
344
+ cond(
345
+ Type.contains_rc_type(T) => {
346
+ i := usize(0);
347
+ while(i < actual_count, i = (i + usize(1)), {
348
+ element_ptr := (_ptr &+ (start + i));
349
+ unsafe.drop(element_ptr.*);
350
+ });
351
+ },
352
+ true => ()
353
+ );
354
+ }
355
+ );
356
+
357
+ // Update length
358
+ self._length = new_length;
359
+ .Ok(actual_count)
360
+ }
361
+ )
362
+ )
363
+ }
364
+ )
365
+ ),
414
366
 
415
- /**
416
- * Clear all elements but keep capacity
417
- */
418
- clear :: (fn(self: Self) -> unit)({
419
- self._free_elements();
420
- self._length = usize(0);
421
- }),
367
+ /**
368
+ * Create a new ArrayList containing a slice of elements from start to end (exclusive)
369
+ * Similar to array slicing in many languages: array[start:end]
370
+ * Returns a new ArrayList with copied elements
371
+ */
372
+ slice : (fn(self: Self, start: usize, end: usize) -> Result(Self, ArrayListError))(
373
+ // Validate indices
374
+ cond(
375
+ (start > self._length) =>
376
+ .Err(.IndexOutOfBounds(index: start, length: self._length)),
377
+ (end > self._length) =>
378
+ .Err(.IndexOutOfBounds(index: end, length: self._length)),
379
+ (start > end) =>
380
+ .Err(.IndexOutOfBounds(index: start, length: end)),
381
+ true => {
382
+ slice_length := (end - start);
383
+
384
+ // If slice is empty, return empty ArrayList
385
+ cond(
386
+ (slice_length == usize(0)) =>
387
+ .Ok(Self.new()),
388
+ true =>
389
+ match(self._ptr,
390
+ .None => panic("ArrayList has length but no ptr"),
391
+ .Some(src_ptr) => {
392
+ // Create new ArrayList with exact capacity
393
+ new_list := Self.with_capacity(slice_length);
394
+
395
+ match(new_list._ptr,
396
+ .None => .Err(.AllocError(.OutOfMemory)),
397
+ .Some(dst_ptr) => {
398
+ // Copy elements
399
+ i := usize(0);
400
+ while(i < slice_length, i = (i + usize(1)), {
401
+ src := (src_ptr &+ (start + i));
402
+ dst := (dst_ptr &+ i);
403
+ dst.* = src.*;
404
+ });
405
+
406
+ // Set the length of the new list
407
+ new_list._length = slice_length;
408
+ .Ok(new_list)
409
+ }
410
+ )
411
+ }
412
+ )
413
+ )
414
+ }
415
+ )
416
+ ),
422
417
 
423
- /**
424
- * RAII destructor - automatically called when ArrayList goes out of scope
425
- * For `object`, this is called when the reference count drops to zero
426
- */
427
- dispose :: (fn(self: Self) -> unit)({
428
- match(self._ptr,
429
- .Some(ptr) => {
430
- self._free_elements();
431
- self._length = usize(0);
432
- GlobalAllocator.free(.Some(*(void)(ptr)));
433
- self._ptr = .None;
434
- },
435
- .None => ()
436
- );
437
- })
438
- );
418
+ /**
419
+ * Clear all elements but keep capacity
420
+ */
421
+ clear : (fn(self: Self) -> unit)({
422
+ Self._free_elements(self);
423
+ self._length = usize(0);
424
+ })
425
+ );
426
+ impl(forall(T : Type), ArrayList(T), Dispose(
427
+ /**
428
+ * RAII destructor - automatically called when ArrayList goes out of scope
429
+ * For `object`, this is called when the reference count drops to zero
430
+ */
431
+ dispose : (fn(self: Self) -> unit)({
432
+ match(self._ptr,
433
+ .Some(_ptr) => {
434
+ Self._free_elements(self);
435
+ self._length = usize(0);
436
+ GlobalAllocator.free(.Some(*(void)(_ptr)));
437
+ self._ptr = .None;
438
+ },
439
+ .None => ()
440
+ );
441
+ })
442
+ ));
439
443
 
440
444
  export
441
445
  ArrayList,