aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/fib.fwd4
-rw-r--r--examples/vec.fwd51
2 files changed, 53 insertions, 2 deletions
diff --git a/examples/fib.fwd b/examples/fib.fwd
index e6d6ee6..4583a26 100644
--- a/examples/fib.fwd
+++ b/examples/fib.fwd
@@ -25,6 +25,6 @@ fib(i64 n, (i64) res)
main()
{
fib(42) => i64 n;
- print_i64(n);
- print_nl();
+ fwdprint_i64(n);
+ fwdprint_nl();
}
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]()