From 8d54bda923f9ff0d2fbcdbe5f59f3e1837986bf3 Mon Sep 17 00:00:00 2001 From: Hrutav Modha Date: Thu, 11 Jun 2026 22:17:29 +0530 Subject: [PATCH] feat: implement standard library Vector container and fix primitive type overloads --- Src/Assert.rux | 2 +- Src/Fatal.rux | 2 +- Src/Memory.rux | 8 ++-- Src/String.rux | 2 +- Src/Vector.rux | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 Src/Vector.rux diff --git a/Src/Assert.rux b/Src/Assert.rux index 00eea2e..f4989ea 100644 --- a/Src/Assert.rux +++ b/Src/Assert.rux @@ -15,7 +15,7 @@ module Std { String::From(file) + ":" + Format("{}", line) + ":" + Format("{}", column) + "\n in function " + String::From(function) + "\n"; Print(output); - Exit(2); + Exit(2u32); } } } diff --git a/Src/Fatal.rux b/Src/Fatal.rux index 45a55fd..dccfb5c 100644 --- a/Src/Fatal.rux +++ b/Src/Fatal.rux @@ -15,6 +15,6 @@ module Std { String::From(file) + ":" + Format("{}", line) + ":" + Format("{}", column) + "\n in function " + String::From(function) + "\n"; Print(output); - Exit(1); + Exit(1u32); } } \ No newline at end of file diff --git a/Src/Memory.rux b/Src/Memory.rux index 849586c..35fa6fc 100644 --- a/Src/Memory.rux +++ b/Src/Memory.rux @@ -304,22 +304,22 @@ module Std::Memory { @[Target("BSD")] func Zero(ptr: *opaque, size: uint) { - Set(ptr, size, 0); + Set(ptr, size, 0i32); } @[Target("Illumos")] func Zero(ptr: *opaque, size: uint) { - Set(ptr, size, 0); + Set(ptr, size, 0i32); } @[Target("Linux")] func Zero(ptr: *opaque, size: uint) { - Set(ptr, size, 0); + Set(ptr, size, 0i32); } @[Target("MacOS")] func Zero(ptr: *opaque, size: uint) { - Set(ptr, size, 0); + Set(ptr, size, 0i32); } @[Target("Windows")] diff --git a/Src/String.rux b/Src/String.rux index b99d204..23d256f 100644 --- a/Src/String.rux +++ b/Src/String.rux @@ -162,7 +162,7 @@ module Std { if first == self.length { return String { - data: Alloc(0) as *char8, + data: Alloc(0u) as *char8, length: 0 }; } diff --git a/Src/Vector.rux b/Src/Vector.rux new file mode 100644 index 0000000..483b1e4 --- /dev/null +++ b/Src/Vector.rux @@ -0,0 +1,111 @@ +/* + Rux standard library + Copyright © 2026 Rux Contributors + Licensed under the MIT License +*/ + +module Std { + import Std::Memory::{ Alloc, Realloc, Free, Copy }; + + struct Vector { + data: *uint8; + length: uint; + capacity: uint; + elementSize: uint; + } + + extend Vector { + func New(elementSize: uint) -> Vector { + return Vector { + data: null, + length: 0u, + capacity: 0u, + elementSize: elementSize + }; + } + + func Init(self, elementSize: uint) { + self.data = null; + self.length = 0u; + self.capacity = 0u; + self.elementSize = elementSize; + } + + func Free(self) { + if self.data != null { + Free(self.data as *opaque); + self.data = null; + } + self.length = 0u; + self.capacity = 0u; + } + + func Len(self) -> uint { + return self.length; + } + + func Cap(self) -> uint { + return self.capacity; + } + + func IsEmpty(self) -> bool { + return self.length == 0u; + } + + func Push(self, element: *const opaque) { + self.Reserve(1u); + let dest = self.data + self.length * self.elementSize; + Copy(dest as *opaque, element, self.elementSize); + self.length = self.length + 1u; + } + + func Pop(self, outElement: *opaque) { + if self.length == 0u { + return; + } + let index = self.length - 1u; + let src = self.data + index * self.elementSize; + if outElement != null { + Copy(outElement, src as *const opaque, self.elementSize); + } + self.length = index; + } + + func Get(self, index: uint) -> *opaque { + if index >= self.length { + return null; + } + return (self.data + index * self.elementSize) as *opaque; + } + + func Set(self, index: uint, element: *const opaque) { + if index >= self.length { + return; + } + let dest = self.data + index * self.elementSize; + Copy(dest as *opaque, element, self.elementSize); + } + + func Clear(self) { + self.length = 0u; + } + + func Reserve(self, additional: uint) { + let required = self.length + additional; + if required <= self.capacity { + return; + } + var capacity = self.capacity; + if capacity == 0u { + capacity = 16u; + } + while capacity < required { + capacity *= 2u; + } + self.data = self.data == null + ? Alloc(capacity * self.elementSize) as *uint8 + : Realloc(self.data as *opaque, capacity * self.elementSize) as *uint8; + self.capacity = capacity; + } + } +}