aboutsummaryrefslogtreecommitdiff
path: root/examples/vec.fwd
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-05-07 21:22:38 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2026-01-06 17:19:52 +0200
commitd501b2c9ebab6f5b90c808ea0e5fde912818707d (patch)
treec41f5292d1107ac56c3671860a18717a63730772 /examples/vec.fwd
parent49320376a1ba3fcc7eba7ceda2c728649ee8323e (diff)
downloadfwd-d501b2c9ebab6f5b90c808ea0e5fde912818707d.tar.gz
fwd-d501b2c9ebab6f5b90c808ea0e5fde912818707d.zip
implement enough type analysis for vector example
+ Big commit, scary + Some details still a bit up in the air, mainly about move checking structure member access ('register' types are freely copied I guess, same as in rust? How about user types?)
Diffstat (limited to 'examples/vec.fwd')
-rw-r--r--examples/vec.fwd51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/vec.fwd b/examples/vec.fwd
new file mode 100644
index 0000000..1cc10b3
--- /dev/null
+++ b/examples/vec.fwd
@@ -0,0 +1,51 @@
+import "../mod/libfwdmem.so"
+import "../mod/libfwdutil.so"
+
+/* trait */
+any = {}
+
+/* template */
+vec[any type]() {
+ <> {
+ i64 s;
+ i64 n;
+ *[]type buf;
+ }
+
+ <>create((<>) next)
+ {
+ next((0 => s, 0 => n, * => buf)<>);
+ }
+
+ <>append(<> v, type n, (<>) next)
+ {
+ v.n = v.n + 1;
+ if v.s == 0 {
+ v.s = 1;
+ }
+
+ if v.n > v.s {
+ if v.s == 0 {
+ v.s = 1;
+ }
+
+ v.s = v.s * 2;
+ fwdrealloc(v.buf, v.s as u64 * sizeof(type)) => ptr;
+ v.buf = ptr;
+ }
+
+ /* in a real implementation this would be moved to after
+ * fwdrealloc I guess, but this is fine for now */
+ nil v.buf {fwdpanic("no buf for vector");} => buf;
+ buf*[v.n - 1] = n;
+ next(v);
+ }
+
+ <>destroy(<> v)
+ {
+ fwdfree(v.buf);
+ nil v;
+ }
+}
+
+u8vec = vec[u8]()