summaryrefslogtreecommitdiff
path: root/libtnsl
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2022-10-06 12:26:42 -0400
committerKyle Gunger <kgunger12@gmail.com>2022-10-06 12:26:42 -0400
commitf4bc4570be7a51b00bf44d5bd4be99108dd94a30 (patch)
treeabb6b32ac979d7dc0bdf1792d86b0daca8818525 /libtnsl
parenta58e2685b815a5a0d2d5f07bc3ae53016ba6f52b (diff)
Libtnsl additions
Diffstat (limited to 'libtnsl')
-rw-r--r--libtnsl/algo/algo.tnsl2
-rw-r--r--libtnsl/box/_vect.tnsl20
-rw-r--r--libtnsl/box/box.tnsl3
-rw-r--r--libtnsl/box/iterator.tnsl142
-rw-r--r--libtnsl/box/list.tnsl66
-rw-r--r--libtnsl/box/string.tnsl30
-rw-r--r--libtnsl/box/vector.tnsl177
-rw-r--r--libtnsl/libtnsl.tnsl1
-rw-r--r--libtnsl/math/basic.tnsl157
-rw-r--r--libtnsl/math/math.tnsl (renamed from libtnsl/algo/math/math.tnsl)2
-rw-r--r--libtnsl/reflect/_type.tnsl (renamed from libtnsl/reflect/type.tnsl)0
-rw-r--r--libtnsl/reflect/reflect.tnsl2
12 files changed, 588 insertions, 14 deletions
diff --git a/libtnsl/algo/algo.tnsl b/libtnsl/algo/algo.tnsl
index 7df9198..90a55da 100644
--- a/libtnsl/algo/algo.tnsl
+++ b/libtnsl/algo/algo.tnsl
@@ -23,7 +23,5 @@
/: include
"alloc.tnsl"
"dealloc.tnsl"
-
- "math"
:/
;/ \ No newline at end of file
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)
+ ;/
+;/
+
diff --git a/libtnsl/libtnsl.tnsl b/libtnsl/libtnsl.tnsl
index 070bfd2..1700efb 100644
--- a/libtnsl/libtnsl.tnsl
+++ b/libtnsl/libtnsl.tnsl
@@ -26,6 +26,7 @@
"algo"
"box"
"io"
+ "math"
"reflect"
"time"
:/
diff --git a/libtnsl/math/basic.tnsl b/libtnsl/math/basic.tnsl
new file mode 100644
index 0000000..3eefc0d
--- /dev/null
+++ b/libtnsl/math/basic.tnsl
@@ -0,0 +1,157 @@
+/##
+ 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
+#/
+
+# ABS functions
+
+/; abs (int a) [int]
+ /; if (a < 0)
+ ;return -a
+ ;/
+ ;return a
+;/
+
+/; absf (float a) [float]
+ /; if (a < 0)
+ ;return -a
+ ;/
+ ;return a
+;/
+
+/; abst (type T, T a) [T]
+ /; if ( a < (0)[T] )
+ ;return -a
+ ;/
+ ;return a
+;/
+
+# Div funcs
+
+/; div (int a, b) [int, int]
+ ;int o1 = a / b, o2 = a % b
+ ;return o1, o2
+;/
+
+/; divf (float a, b) [float, float]
+ ;float o1 = a / b, o2 = a % b
+ ;return o1, o2
+;/
+
+/; divt (type T, T a, b) [T, T]
+ ;T o1 = a / b, o2 = a % b
+ ;return o1, o2
+;/
+
+# Min/Max functions
+
+/; max (int a, b) [int]
+ /; if (a > b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+/; maxf (float a, b) [float]
+ /; if (a > b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+/; maxt (type T, T a, b) [T]
+ /; if (a > b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+/; min (int a, b) [int]
+ /; if (a < b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+/; minf (float a, b) [float]
+ /; if (a > b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+/; mint (type T, T a, b) [T]
+ /; if (a < b)
+ ;return a
+ ;/
+ ;return b
+;/
+
+# Float rounding
+
+/; trunc (float a) [float]
+ ;return a - (a % 1.0)
+;/
+
+/; trunct (type T, T a) [T]
+ ;return a - (a % (1.0)[T])
+;/
+
+/; ceil (float a) [float]
+ /; if (trunc(a) !== a)
+ ;return trunc(a + 1.0)
+ ;/
+ ;return a
+;/
+
+/; ceilt (type T, T a) [T]
+ /; if (trunct(T, a) !== a)
+ ;return trunct(T, a + (1.0)[T])
+ ;/
+ ;return a
+;/
+
+/; floor (float a) [float]
+ /; if (trunc(a) !== a)
+ ;return trunc(a - 1.0)
+ ;/
+ ;return a
+;/
+
+/; floort (type T, T a) [T]
+ /; if (trunct(T, a) !== a)
+ ;return trunct(T, a - (1.0)[T])
+ ;/
+ ;return a
+;/
+
+/; round (float a) [float]
+ /; if (a % 1.0 < 0.5)
+ ;return floor(a)
+ ;/
+ ;return ceil(a)
+;/
+
+/; roundt (type T, T a) [T]
+ /; if (a % (1.0)[T] < (0.5)[T])
+ ;return floort(T, a)
+ ;/
+ ;return ceilt(T, a)
+;/
+
diff --git a/libtnsl/algo/math/math.tnsl b/libtnsl/math/math.tnsl
index 5676d09..eff1aa3 100644
--- a/libtnsl/algo/math/math.tnsl
+++ b/libtnsl/math/math.tnsl
@@ -21,6 +21,6 @@
/; export module math
/; include
-
+ "basic.tnsl"
;/
;/ \ No newline at end of file
diff --git a/libtnsl/reflect/type.tnsl b/libtnsl/reflect/_type.tnsl
index 7a19b7d..7a19b7d 100644
--- a/libtnsl/reflect/type.tnsl
+++ b/libtnsl/reflect/_type.tnsl
diff --git a/libtnsl/reflect/reflect.tnsl b/libtnsl/reflect/reflect.tnsl
index b3640c2..a438a03 100644
--- a/libtnsl/reflect/reflect.tnsl
+++ b/libtnsl/reflect/reflect.tnsl
@@ -25,7 +25,7 @@
#; export module reflect
/: import
- "type.tnsl"
+ "_type.tnsl"
"virtual.tnsl"
"resolver.tnsl"
"dispatch.tnsl"