aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 15:22:47 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 15:22:59 +0000
commite1d05f02ab7c865d752d38d0611b95583876bd8b (patch)
treeb4e7d91ac89df17a203e8186b96dfbe3ef6eb22f
parent6270bd1fd1edd376e87b2a45c0fd39879303e636 (diff)
Partial implementation of array and lists
-rw-r--r--examples/44-list-builder/shed-examples.json59
-rw-r--r--examples/44-list-builder/shed.json511
-rw-r--r--examples/44-list-builder/src/Lists.shed43
-rw-r--r--projects/std/src/Std/Lists.shed58
-rw-r--r--runtime/meson.build1
-rw-r--r--runtime/src/shed/namespaces/Std/Lists.c84
-rw-r--r--runtime/src/shed/namespaces/Std/Lists.h15
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java2
8 files changed, 222 insertions, 1 deletions
diff --git a/examples/44-list-builder/shed-examples.json5 b/examples/44-list-builder/shed-examples.json5
new file mode 100644
index 00000000..b669a2e6
--- /dev/null
+++ b/examples/44-list-builder/shed-examples.json5
@@ -0,0 +1,9 @@
+{
+ examples: [
+ {
+ project: ".",
+ bin: "lists",
+ expectedOutput: "buildList()\nhello world\n\nbuildSameListTwice()\nhello world\nhello world\n",
+ },
+ ],
+}
diff --git a/examples/44-list-builder/shed.json5 b/examples/44-list-builder/shed.json5
new file mode 100644
index 00000000..aaa788d6
--- /dev/null
+++ b/examples/44-list-builder/shed.json5
@@ -0,0 +1,11 @@
+{
+ bins: [
+ {
+ name: "lists",
+ mainNamespace: "Lists",
+ },
+ ],
+ dependencies: [
+ {path: "../../projects/std"},
+ ],
+}
diff --git a/examples/44-list-builder/src/Lists.shed b/examples/44-list-builder/src/Lists.shed
new file mode 100644
index 00000000..7744e8d8
--- /dev/null
+++ b/examples/44-list-builder/src/Lists.shed
@@ -0,0 +1,43 @@
+import Std/Lists;
+
+pub fun main() ! Io -> Unit {
+ buildList!();
+ buildSameListTwice!();
+}
+
+fun buildList() ! Io -> Unit {
+ print!("buildList()\n");
+
+ val builder = Lists.listBuilder[String]();
+ builder.add("hello");
+ builder.add(" world\n");
+
+ val list = builder.build();
+
+ list.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ print!("\n");
+}
+
+fun buildSameListTwice() ! Io -> Unit {
+ print!("buildSameListTwice()\n");
+
+ val builder = Lists.listBuilder[String]();
+ builder.add("hello");
+ builder.add(" world\n");
+
+ val list1 = builder.build();
+ val list2 = builder.build();
+
+ list1.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ list2.forEach!(fn (element) ! Io {
+ print!(element);
+ });
+
+ print!("\n");
+}
diff --git a/projects/std/src/Std/Lists.shed b/projects/std/src/Std/Lists.shed
new file mode 100644
index 00000000..8771eb74
--- /dev/null
+++ b/projects/std/src/Std/Lists.shed
@@ -0,0 +1,58 @@
+struct List[T](array: Array[T]) {
+ // TODO: generic effect
+ fun forEach(this, func: fn (T) ! Io -> Unit) ! Io -> Unit {
+ var index = 0;
+ val size = this.array.size();
+
+ while (index < size) {
+ func!(this.array.unsafeGet(index));
+ assign index = index + 1;
+ }
+ }
+}
+
+struct ListBuilder[T](arrayBuilder: ArrayBuilder[T]) {
+ fun add(this, element: T) -> ListBuilder[T] {
+ if (this.arrayBuilder.size() < this.arrayBuilder.capacity()) {
+ ListBuilder(this.arrayBuilder.unsafeAdd(element))
+ } else {
+ val newCapacity = this.arrayBuilder.capacity() * 2;
+ val newArrayBuilder = this.arrayBuilder
+ .unsafeEnlarge(.capacity = newCapacity)
+ .unsafeAdd(element);
+ ListBuilder(newArrayBuilder)
+ }
+ }
+
+ fun build(this) -> List[T] {
+ List(this.arrayBuilder.build())
+ }
+}
+
+pub fun listBuilder[T]() -> ListBuilder[T] {
+ ListBuilder[T](arrayBuilder[T](.capacity = 16))
+}
+
+native type Array[T] {
+ // Precondition: the index is in the range [0, size).
+ native fun unsafeGet(This, Int) -> T;
+
+ native fun size(This) -> Int;
+}
+
+native type ArrayBuilder[T] {
+ native fun capacity(This) -> Int;
+
+ native fun size(This) -> Int;
+
+ // Precondition: size < capacity.
+ native fun unsafeAdd(This, T) -> ArrayBuilder[T];
+
+ // Precondition: the new capacity is greater than or equal to the current
+ // capacity.
+ native fun unsafeEnlarge(This, .capacity: Int) -> ArrayBuilder[T];
+
+ native fun build(This) -> Array[T];
+}
+
+native fun arrayBuilder[T](.capacity: Int) -> ArrayBuilder[T];
diff --git a/runtime/meson.build b/runtime/meson.build
index 70bf51fd..90e17f4d 100644
--- a/runtime/meson.build
+++ b/runtime/meson.build
@@ -21,6 +21,7 @@ static_library(
'src/shed/primitives.c',
'src/shed/strings.c',
'src/shed/namespaces/Shed/Builtins.c',
+ 'src/shed/namespaces/Std/Lists.c',
'src/shed/namespaces/Std/Process.c',
gc_source,
c_args: ['-DSHED_GC_COLLECT_INTERVAL=' + get_option('gc_collect_interval').to_string()],
diff --git a/runtime/src/shed/namespaces/Std/Lists.c b/runtime/src/shed/namespaces/Std/Lists.c
new file mode 100644
index 00000000..41760d83
--- /dev/null
+++ b/runtime/src/shed/namespaces/Std/Lists.c
@@ -0,0 +1,84 @@
+#include "shed/gc.h"
+#include "shed/ints.h"
+#include "./Lists.h"
+
+struct ShedArrayBuilder {
+ struct ShedGcObjectHeader object_header;
+ PatioInt capacity;
+ PatioInt size;
+ struct ShedArray* array;
+};
+
+struct ShedArray {
+ struct ShedGcObjectHeader object_header;
+ PatioInt size;
+ ShedObject elements[];
+};
+
+ShedObject shed__runtime__namespace__Std__Lists__arrayBuilder(ShedClosurePointer closurePointer, ShedObject capacityObject) {
+ // TODO: runtime type tag / GC support (don't set mark to 0b110, set appropriate runtime type)
+
+ PatioInt capacity = shed__runtime__int_value(capacityObject);
+
+ struct ShedArray* array = shed__runtime__gc_alloc(
+ sizeof(struct ShedArray) + sizeof(ShedObject) * capacity,
+ 0
+ );
+ array->object_header.mark = 0b110;
+ array->size = 0;
+
+ struct ShedArrayBuilder* array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0);
+ array_builder->object_header.mark = 0b110;
+ array_builder->capacity = capacity;
+ array_builder->size = 0;
+ array_builder->array = array;
+
+ return (ShedObject) array_builder;
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__Array__unsafeGet(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1) {
+ struct ShedArray* array = (struct ShedArray*) arg0;
+ PatioInt index = shed__runtime__int_value(arg1);
+ return array->elements[index];
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__Array__size(ShedClosurePointer closurePointer, ShedObject arg0) {
+ struct ShedArray* array = (struct ShedArray*) arg0;
+ return shed__runtime__int_create(array->size);
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__capacity(ShedClosurePointer closurePointer, ShedObject arg0) {
+ struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0;
+ return shed__runtime__int_create(array_builder->capacity);
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__size(ShedClosurePointer closurePointer, ShedObject arg0) {
+ struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0;
+ return shed__runtime__int_create(array_builder->size);
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1) {
+ // TODO: track whether array has been built or added to
+ struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0;
+ array_builder->array->elements[array_builder->array->size] = arg1;
+ array_builder->array->size += 1;
+
+ struct ShedArrayBuilder* new_array_builder = shed__runtime__gc_alloc(sizeof(struct ShedArrayBuilder), 0);
+ new_array_builder->object_header.mark = 0b110;
+ new_array_builder->capacity = array_builder->capacity;
+ new_array_builder->size = array_builder->size + 1;
+ new_array_builder->array = array_builder->array;
+
+ return (ShedObject) new_array_builder;
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeEnlarge(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject capacity) {
+ // TODO: implement
+ return arg0;
+}
+
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__build(ShedClosurePointer closurePointer, ShedObject arg0) {
+ // TODO: track whether array has been built or added to
+ struct ShedArrayBuilder* array_builder = (struct ShedArrayBuilder*) arg0;
+ return (ShedObject) array_builder->array;
+}
diff --git a/runtime/src/shed/namespaces/Std/Lists.h b/runtime/src/shed/namespaces/Std/Lists.h
new file mode 100644
index 00000000..373d58d8
--- /dev/null
+++ b/runtime/src/shed/namespaces/Std/Lists.h
@@ -0,0 +1,15 @@
+#ifndef SHED_NAMESPACES__Std__Lists__H
+#define SHED_NAMESPACES__Std__Lists__H
+
+#include "shed/shed.h"
+
+ShedObject shed__runtime__namespace__Std__Lists__arrayBuilder(ShedClosurePointer closurePointer, ShedObject capacity);
+ShedObject shed__runtime__namespace__Std__Lists__method__Array__unsafeGet(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1);
+ShedObject shed__runtime__namespace__Std__Lists__method__Array__size(ShedClosurePointer closurePointer, ShedObject arg0);
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__capacity(ShedClosurePointer closurePointer, ShedObject arg0);
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__size(ShedClosurePointer closurePointer, ShedObject arg0);
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeAdd(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject arg1);
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__unsafeEnlarge(ShedClosurePointer closurePointer, ShedObject arg0, ShedObject capacity);
+ShedObject shed__runtime__namespace__Std__Lists__method__ArrayBuilder__build(ShedClosurePointer closurePointer, ShedObject arg0);
+
+#endif
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
index 97f5d4cd..d08dbb2f 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
@@ -224,7 +224,7 @@ public class ShidToPatio {
}
case ShidDeclareNativeType declareNativeType -> {
- throw new UnsupportedOperationException("TODO");
+ yield pushValue(0, declareNativeType.source(), context);
}
case ShidDefineEffect defineEffect -> {