summaryrefslogtreecommitdiff
path: root/tnslc
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2024-03-15 17:27:21 -0400
committerKyle Gunger <kgunger12@gmail.com>2024-03-15 17:27:21 -0400
commit7b009880ad7a4182d3fa8d84468526d0d30d8844 (patch)
tree97b28d976e8486d60d0b5c3b7447891dde709309 /tnslc
parente25b738656928b38781458ed85880e8ae7bdd5a6 (diff)
Slightly better function call
Diffstat (limited to 'tnslc')
-rw-r--r--tnslc/main.tnsl4
-rwxr-xr-xtnslc/tnslcbin0 -> 21440 bytes
-rw-r--r--tnslc/vector.tnsl59
3 files changed, 61 insertions, 2 deletions
diff --git a/tnslc/main.tnsl b/tnslc/main.tnsl
index ac56836..c7e8c95 100644
--- a/tnslc/main.tnsl
+++ b/tnslc/main.tnsl
@@ -5,6 +5,10 @@
Vector a
a.init(1)
+
+
+
+ a.end()
return 0
;/
diff --git a/tnslc/tnslc b/tnslc/tnslc
new file mode 100755
index 0000000..e8d4c46
--- /dev/null
+++ b/tnslc/tnslc
Binary files differ
diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl
index 1377938..9b89081 100644
--- a/tnslc/vector.tnsl
+++ b/tnslc/vector.tnsl
@@ -1,3 +1,4 @@
+# Define vector struct
struct Vector {
~void data,
int
@@ -6,21 +7,75 @@ struct Vector {
_elsz
}
+# Consts used in impl
int VECT_DEFAULT_SIZE = 4
+int VECT_MAX_GROW = 128
+# Methods on the struct
/; method Vector
+ # Initialize a new vector with elements
+ # 'elsz' bytes long
/; init (int elsz)
- self._elsz = elsz
self.size = VECT_DEFAULT_SIZE
- self.data = _alloc(elsz * self.size)
+ self._elsz = elsz
self.count = 0
+ self.data = _alloc(elsz * VECT_DEFAULT_SIZE)
+ ;/
+
+ # Grow the size of the vector by 'size' elements
+ /; _grow (int size)
+ /; if (size > VECT_MAX_GROW)
+ size = VECT_MAX_GROW
+ ;/
+
+ self.size = self.size + size
+ self.data = _realloc(self.data, self.size * self._elsz)
+ ;/
+
+ # Shrink the size of the vector by 'size' elements
+ /; _shrink (int size)
+ /; if (self.size - size < 0)
+ self.size = 1
+ ;; else
+ self.size = self.size - size
+ ;/
+
+ /; if (self.count < self.size)
+ self.count = self.size
+ ;/
+
+ self.data = _realloc(self.data, self.size * self._elsz)
+ ;/
+
+ # Push an element onto the end of the vector
+ /; push (~void data)
+ /; if (count == size - 1)
+ self._grow(self.size)
+ ;/
+
+ /; loop (int i = 0; i < self._elsz) [i++]
+ (self.data + i)` = (data + i)`
+ ;/
+
+ self.count++
+ ;/
+
+ # Pop an element off of the end of the vector
+ /; pop
+ self.count--
+
+ /; if (self.count < self.size / 2)
+ self._shrink(self.size / 3)
+ ;/
;/
+ # Get a pointer to the start of an element in the vector
/; get (int index) [~void]
return self.data
;/
+ # Free all memory associated with the vector
/; end
_delete(self.data)
self.size = 0