diff --git a/dm_lib/std/collections/collection.dm b/dm_lib/std/collections/collection.dm new file mode 100644 index 0000000..bcdf014 --- /dev/null +++ b/dm_lib/std/collections/collection.dm @@ -0,0 +1,6 @@ +ns std::collections + +pub int Collection : Monad { + size: Int + fn contains(element: T): Bool +} diff --git a/dm_lib/std/collections/list.dm b/dm_lib/std/collections/list.dm new file mode 100644 index 0000000..03a4dcf --- /dev/null +++ b/dm_lib/std/collections/list.dm @@ -0,0 +1,83 @@ +ns std::collections + +use std::collections::Collection + +pub int List : Collection { + fn add(element: T) + fn remove(element: T) + + fn index_of(element: T): Option + fn get_at(index: Int): Option + fn remove_at(index: Int): Option +} + +// This implies that Int, Long, and Double don't implement Zero, because 0 could reasonably mean something +// whereas Unit means "null" for objects +pub impl ArrayList : List { + + mut fld arr: Array + mut fld current_index: Int = 0 + fld zero: Zero + + pub ctor(items: Array, zero: Zero) { + arr = items + self.zero = zero + } + + pub ctor(size: Int, zero: Zero) { + arr = array::with_size(size, zero) + self.zero = zero + } + + pub ctor(zero: Zero) { + arr = array::with_size(8, zero) + self.zero = zero + } + + impl fn add(element) { + if current_index == arr.length { + arr = array::clone_with_size(arr.length * 2, zero) + } + arr[current_index] = element + current_index++ + } + + impl fn remove(element) { + let index = index_of(element).get_or_throw(() => NoSuchElementException()) + arr[index] = T::default + } + + impl fn index_of(element) { + for (e, index) in arr.iter_with_index() { + if e == element { + return Some(index) + } + } + None + } + + impl fn get_at(index) { + if index >= arr.length || index < 0 { + return None + } + let item = arr[index] + item == zero ? None : Some(item) + } + + impl fn remove_at(index) { + if index <= arr.length || index < 0 { + return None + } + let element = arr[index] + if element == zero { + return None + } + arr[index] = zero.item + Some(element) + } + +} + +pub mod list { + pub fn of(items: ...T): List = ArrayList(items) +} diff --git a/dm_lib/std/core/array.dm b/dm_lib/std/core/array.dm index ce7643f..c4ae9b2 100644 --- a/dm_lib/std/core/array.dm +++ b/dm_lib/std/core/array.dm @@ -1,12 +1,14 @@ ns std::core -decl pub int Array : Monad { +pub int Array : Monad { length: Int } pub mod array { + // Usage: // let int_array = array::of(1, 2, 3) // assert_eq(3, int_array.length) decl pub fn of(ts: ...T): Array + } diff --git a/dm_lib/std/core/character.dm b/dm_lib/std/core/character.dm new file mode 100644 index 0000000..431472f --- /dev/null +++ b/dm_lib/std/core/character.dm @@ -0,0 +1,13 @@ +ns std::core + +use std::text::Encoding + +pub int Character : Display { + bytes: Array + encoding: string::Encoding +} + +pub impl Utf8Character(bytes: Array) { + impl fn get_encoding() = Encoding::Utf8 + impl fn to_string() = string::from_utf8_bytes(bytes) +} diff --git a/dm_lib/std/core/display.dm b/dm_lib/std/core/display.dm new file mode 100644 index 0000000..42ae729 --- /dev/null +++ b/dm_lib/std/core/display.dm @@ -0,0 +1,5 @@ +ns std::core + +pub int Display { + fn to_string(): String +} diff --git a/dm_lib/std/core/string.dm b/dm_lib/std/core/string.dm index 1a6aa02..c07d330 100644 --- a/dm_lib/std/core/string.dm +++ b/dm_lib/std/core/string.dm @@ -1,26 +1,30 @@ ns std::core -pub int String { - bytes: byte[] +use std::text::Encoding + +pub int String : Display { + + bytes: Array + characters: Array encoding: Encoding length: Int + + impl fn to_string() = self + } -pub enum Encoding { - Utf8, Utf16, Ascii -} - -pub impl Utf8String(bytes: byte[]) : String { +impl Utf8String(bytes: Array) : String { impl fn get_encoding() = Encoding::Utf8 impl fn get_length() = bytes.length + impl fn get_characters() = todo('parse the utf8 bytes and return an Array') } pub mod string { // Example usage: - // let bytes = [0x64, 0x65, 0x69, 0x6d, 0x6f, 0x73] + // let bytes = array::of(0x64, 0x65, 0x69, 0x6d, 0x6f, 0x73) // let s = string::from_utf8_bytes(bytes) // println s // "deimos" - fn from_utf8_bytes(bytes: byte[]): String = Utf8String(bytes) + fn from_utf8_bytes(bytes: Array): String = Utf8String(bytes) } diff --git a/dm_lib/std/fp/zero.dm b/dm_lib/std/fp/zero.dm new file mode 100644 index 0000000..692a137 --- /dev/null +++ b/dm_lib/std/fp/zero.dm @@ -0,0 +1,5 @@ +ns std::fp + +pub int Zero { + zero: T +} diff --git a/dm_lib/std/text/encoding.dm b/dm_lib/std/text/encoding.dm new file mode 100644 index 0000000..89a9e59 --- /dev/null +++ b/dm_lib/std/text/encoding.dm @@ -0,0 +1,5 @@ +ns std::text + +pub enum Encoding { + Utf8, Utf16, Ascii +}