diff options
Diffstat (limited to 'libtnsl/box')
-rw-r--r-- | libtnsl/box/_vect.tnsl | 20 | ||||
-rw-r--r-- | libtnsl/box/box.tnsl | 3 | ||||
-rw-r--r-- | libtnsl/box/iterator.tnsl | 142 | ||||
-rw-r--r-- | libtnsl/box/list.tnsl | 66 | ||||
-rw-r--r-- | libtnsl/box/string.tnsl | 30 | ||||
-rw-r--r-- | libtnsl/box/vector.tnsl | 177 |
6 files changed, 428 insertions, 10 deletions
diff --git a/libtnsl/box/_vect.tnsl b/libtnsl/box/_vect.tnsl new file mode 100644 index 0000000..52ab19f --- /dev/null +++ b/libtnsl/box/_vect.tnsl @@ -0,0 +1,20 @@ +/## + Copyright 2021 Kyle Gunger + + Dual licensed under the CDDL 1.0 and BSD 3-Clause licenses. + + This file may only be used in accordance with one of the two + licenses. You should have received a copy of each license with + the source code. In the event that you did not recieve a copy + of the licenses, they may be found at the following URLs: + + CDDL 1.0: + https://opensource.org/licenses/CDDL-1.0 + + BSD 3-Clause: + https://opensource.org/licenses/BSD-3-Clause + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ diff --git a/libtnsl/box/box.tnsl b/libtnsl/box/box.tnsl index 2f270b6..0dffdc9 100644 --- a/libtnsl/box/box.tnsl +++ b/libtnsl/box/box.tnsl @@ -25,10 +25,11 @@ #; export module box /: import + "_vect.tnsl" "iterator.tnsl" "map.tnsl" "string.tnsl" "tree.tnsl" "vector.tnsl" :/ -;/
\ No newline at end of file +;/ diff --git a/libtnsl/box/iterator.tnsl b/libtnsl/box/iterator.tnsl index 13b9ef9..2e3389b 100644 --- a/libtnsl/box/iterator.tnsl +++ b/libtnsl/box/iterator.tnsl @@ -20,21 +20,149 @@ #/ # Interface for moving through a list -/; interface ForwardsIterator (type T) +/; interface Iterator (type T) # Advance to the next element in the list - /; next [bool] - ;/ + /; next [bool] ;/ # Get the current element in the list - /; current [T] - ;/ + /; current [T] ;/ + + # Seek to the end of the iterator + # Returns true if this was successfull + /; end [bool] ;/ ;/ # Interface for moving through a list forwards or backwards -/; interface TwoWayIterator (type T) extends ForwardsIterator(T) +/; interface DoubleIterator (type T) extends Iterator(T) # Move to the previous element in the list - /; prev [bool] + /; prev [bool] ;/ + + # Seek to the start of the iterator + # Returns true if this was successfull + /; start [bool] ;/ +;/ + +# Iterate over an array +;raw struct ArrayIterator (type T) extends DoubleIterator(T) { + uint place, + ~{}T data +} + +/; method ArrayIterator + /; ArrayIterator (~{}T arr) + ;self.place = 0 + ;self.data = arr + ;/ + + /; override next [bool] + /; if (self.place + 1 >= len self.data`) + ;self.place = len self.data` + ;return false + ;/ + ;self.place++ + ;return true + ;/ + + /; override current [T] + ;return self.data`{self.place} + ;/ + + /; override prev [bool] + /; if (self.place == 0) + ;return false + ;/ + ;self.place-- + ;return true + ;/ + + /; override end [bool] + ;self.place = len self.data` + ;return true + ;/ + + /; override start [bool] + ;self.place = 0 + ;return true + ;/ +;/ + +# Iterate over a pointer +;raw struct PointerIterator (type T) extends DoubleIterator(T) { + uint + place, + length, + + ~T data +} + +/; method PointerIterator + /; PointerIterator (~T ptr, uint length) + ;self.place = 0 + ;self.length = length + ;self.data = ptr + ;/ + + /; override next [bool] + /; if (self.place + 1 >= self.length) + ;self.place = self.length + ;return false + ;/ + ;self.place++ + ;return true + ;/ + + /; override current [T] + ;return self.data{self.place} + ;/ + + /; override prev [bool] + /; if (self.place == 0) + ;return false + ;/ + ;self.place-- + ;return true + ;/ + + /; override end [bool] + ;self.place = self.length + ;return true + ;/ + + /; override start [bool] + ;self.place = 0 + ;return true + ;/ +;/ + +;struct ReverseIterator (type T) extends DoubleIterator(T) { + DoubleIterator(T) it +} + +/; method ReverseIterator + /; ReverseIterator (DoubleIterator(T) it) + ;self.it = it + ;self.it.end() + ;/ + + /; override next [bool] + ;return self.it.prev() + ;/ + + /; override current [T] + ;return self.it.current() + ;/ + + /; override prev [bool] + ;return self.it.next() + ;/ + + /; override end [bool] + ;return self.it.start() + ;/ + + /; override start [bool] + ;return self.it.end() ;/ ;/
\ No newline at end of file diff --git a/libtnsl/box/list.tnsl b/libtnsl/box/list.tnsl new file mode 100644 index 0000000..ed9c4b7 --- /dev/null +++ b/libtnsl/box/list.tnsl @@ -0,0 +1,66 @@ +/## + Copyright 2021 Kyle Gunger + + Dual licensed under the CDDL 1.0 and BSD 3-Clause licenses. + + This file may only be used in accordance with one of the two + licenses. You should have received a copy of each license with + the source code. In the event that you did not recieve a copy + of the licenses, they may be found at the following URLs: + + CDDL 1.0: + https://opensource.org/licenses/CDDL-1.0 + + BSD 3-Clause: + https://opensource.org/licenses/BSD-3-Clause + + THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO + WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE + EXPRESS OR IMPLIED +#/ + +;struct LNode (type T) { + T data, + ~void next +} + +/; method LNode + /; get_next_type (type B) [LNode(B)] + ;return (self.next)[~LNode(B)]` + ;/ + + /; get_next [LNode(T)] + ;return (self.next)[~LNode(T)]` + ;/ +;/ + +;struct DLNode (type T) extends LNode(T) { + super, + ~void prev +} + +/; method DLNode + /; get_prev_type (type B) [LNode(B)] + ;return (self.prev)[~LNode(B)]` + ;/ + + /; get_prev [LNode(T)] + ;return (self.prev)[~LNode(T)]` + ;/ +;/ + +;struct List { + uint length, + + LNode + first, + last +} + +;struct DList { + uint length, + + DLNode + first, + last +}
\ No newline at end of file diff --git a/libtnsl/box/string.tnsl b/libtnsl/box/string.tnsl index d346464..5163328 100644 --- a/libtnsl/box/string.tnsl +++ b/libtnsl/box/string.tnsl @@ -17,4 +17,32 @@ THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE EXPRESS OR IMPLIED -#/
\ No newline at end of file +#/ + +;raw enum TEXT_ENCODING { + UTF_8, + UN7_1, + ASCII +} + +;struct String extends Vector (uint8) { + uint +} + +/; method String + /; cmp (String str) [int] + /; loop (self.) + + ;/ + ;/ + + /; override operator == (String str) [bool] + ;return cmp(str) == 0 + ;/ +;/ + +;struct WString extends Vector (uint16) {} + +/; method WString + +;/
\ No newline at end of file diff --git a/libtnsl/box/vector.tnsl b/libtnsl/box/vector.tnsl index d346464..7d2c73b 100644 --- a/libtnsl/box/vector.tnsl +++ b/libtnsl/box/vector.tnsl @@ -17,4 +17,179 @@ THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE EXPRESS OR IMPLIED -#/
\ No newline at end of file +#/ + +;struct Vector (type T) { + uint + length, + dataSize, + + ~T data +} + +/; method Vector + + # Constructors + + /; Vector + ;self.length = 0 + ;self.dataSize = 1 + ;alloc self.data, size T + ;/ + + /; Vector (T first) + ;self.length = 1 + ;self.dataSize = 1 + ;alloc self.data, size T + ;self.data{0} = first + ;/ + + /; Vector ({}T arr) + ;self.length = len arr + ;self.data_size = 1 + + /; loop (self.dataSize < self.length) + ;self.dataSize *= 2 + ;/ + + ;alloc self.data, self.dataSize * size T + + /; loop (uint i = 0; i < self.length) [i++] + ;self.data{i} = arr{i} + ;/ + ;/ + + + # Operator overloads + + /; operator {} (uint index) [~T] + /; if (index !< 0 && index < self.length) + ;return self.data + index + ;/ + # Probably not what errors will look like long term, but + # serves as a proof of concept for now + ;throw Error{"Bad Index"} + ;/ + + /; operator len [uint] + ;return self.length + ;/ + + /; operator == (Vector(T) vec) [bool] + /; if (vec.length != self.length) + ;return false + ;/ + + /; loop (uint i = 0; i < self.length) [i++] + /; if (vec.data{i} != self.data{i}) + ;return false + ;/ + ;/ + + ;return true + ;/ + + /; operator delete + ;delete self.data + ;/ + + # Internal + + /; _grow + ;realloc self.data, self.dataSize * 2 * size T + ;self.dataSize *= 2 + ;/ + + /; _shrink + ;realloc self.data, self.dataSize / 2 * size T + ;self.dataSize /= 2 + ;/ + + # Basic operations + + /; add (T item, uint index) + /; if (index < 0 || index > self.length) + ;throw Error{"Bad Index"} + ;/ + + # If it checks out then we check if we should grow + /; if (self.length >= self.dataSize) + # Might throw a memory allocation error + ;self._grow() + ;/ + + /; loop (uint i = self.length; i > index) [i--] + ;self.data{i} = self.data{i - 1} + ;/ + + ;self{} = replace + + ;self.length += 1 + ;/ + + /; remove (uint index) [T] + ;T out = self{index} + + /; loop (uint i = index; i < self.length - 1) [i++] + ;self.data{i} = self.data{i + 1} + ;/ + + self.length -= 1 + + /; if (self.length < self.dataSize / 4) + ;self._shrink() + ;/ + + ;return out + ;/ + + /; remove_item (T item) [bool] + ;bool removed = false + + /; loop (uint i = 0; i < self.length) [i++] + /; if (!removed && self.data{i} == item) + ;removed = true + ;; else if (removed) + ;self.data{i - 1} = self.data{i} + ;/ + ;/ + + /; if (removed) + ;self.length -= 1 + /; if (self.length < self.dataSize / 4) + ;self._shrink() + ;/ + ;/ + + ;return removed + ;/ + + /; remove_all (T item) [uint] + ;uint removed = 0 + + /; loop (uint i = 0; i < self.length - removed) [i++] + /; loop (self.data{i + removed} == item && removed < self.length) + ;removed += 1 + ;; if (removed != 0 && removed < self.length) + ;self.data{i} = self.data{i + removed} + ;/ + ;/ + + ;self.length -= removed + /; loop (self.length < self.dataSize / 4) + ;self._shrink() + ;/ + + ;return removed + ;/ + + /; push (T item) [uint] + ;self.add(item, self.length) + ;return self.length - 1 + ;/ + + /; pop [T] + ;return self.remove(self.length - 1) + ;/ +;/ + |