From 95b54bb7baf55e9188cb0625b23992b31dd947b8 Mon Sep 17 00:00:00 2001 From: NoxAether <107390370+NoxAether@users.noreply.github.com> Date: Fri, 17 Oct 2025 19:42:10 +1100 Subject: [PATCH 1/2] added shitty stack impl --- std/stl/stack.lx | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 std/stl/stack.lx diff --git a/std/stl/stack.lx b/std/stl/stack.lx new file mode 100644 index 00000000..2c6c1bda --- /dev/null +++ b/std/stl/stack.lx @@ -0,0 +1,109 @@ +@module "stack" +@use "memory" as mem + +// auto incrementing stack +const Stack = struct { + top: int, // top value in the stack + capacity: int, // total `length` of the stack + currentAmount: int, // spaces from 0 - capacity that are full + stackCeiling: int, // hard cap on the limit + array: *int // stack representation +}; + +#returns_ownership +pub const createStack = fn (stackCeiling: int) *Stack { + let stack: *Stack = cast<*Stack>(alloc(sizeof)); + + // check if allocation failed and return a void* + if (stack == cast<*Stack>(0)) { + return cast<*Stack>(0); + } + + stack.top = -1; + stack.capacity = 1; + stack.currentAmount = 0; + stack.stackCeiling = stackCeiling; + + // assume stack.array is empty + stack.array = cast<*int>(alloc(sizeof)); + + if (stack.array == cast<*int>(0)) { + free(stack); + return cast<*Stack>(0); + } + + return stack; +} + +const extendStack = fn (stack: *Stack, capacity: int) int { + // Cannot extend stack past ceiling + if (capacity > stack.stackCeiling) { + return 0; + } + + // frees itself on fail + stack.array = cast<*int>(mem::realloc(cast<*void>(stack.array), capacity * sizeof)); + + stack.capacity = capacity; + return 1; +} + +#takes_ownership +pub const freeStack = fn (stack: *Stack) void { + free(stack.array); + free(stack); +} + +const isEmpty = fn(stack: *Stack) int { + // goes to -1 instead of 1 so add - + return -cast(stack.top == -1); +} + +const isFull = fn(stack: *Stack) int { + // goes to -1 instead of 1 so add - + return -cast(stack.top == (stack.capacity - 1)); +} + +pub const push = fn(stack: *Stack, value: int) void { + if (isFull(stack) == 1) { + if (stack.capacity >= stack.stackCeiling) { + output("Stack ceiling reached\nCan not push ", value, "\n"); + return; + } + stack.currentAmount = stack.currentAmount + 1; + let newCapacity: int = stack.capacity * 2; + + if (newCapacity > stack.stackCeiling) { + newCapacity = stack.stackCeiling; + } + if (extendStack(stack, newCapacity) == 0) { + output("Failed to extend stack, can not push ", value "\n"); + } + output("Stack extended to capacity: ", stack.capacity, "\n"); + } + stack.top = stack.top + 1; + stack.array[stack.top] = value; + output("Pushed ", value, " to the stack\n"); +} + +pub const pop = fn(stack: *Stack) int { + if (isEmpty(stack) == 1) { + output("Stack underflow (empty stack)\n"); + return 0; + } + + let value: int = stack.array[stack.top]; + output("Popping ", value, " from the stack\n"); + + stack.currentAmount = stack.currentAmount - 1; + stack.top = stack.top - 1; + return value; +} + +const peek = fn(stack: *Stack) int { + if (isEmpty(stack) == 1) { + output("Stack is empty\n"); + return 0; + } + return stack.array[stack.top]; +} From d3449d7acd104013dcd3c9072d195f334a68fa53 Mon Sep 17 00:00:00 2001 From: NoxAether <107390370+NoxAether@users.noreply.github.com> Date: Fri, 17 Oct 2025 22:05:26 +1100 Subject: [PATCH 2/2] idk --- std/ffi/bindgen.lx | 0 std/ffi/c_types.lx | 0 std/ffi/stdlib.lx | 0 std/ffi/syscall.lx | 0 std/stl/stack.lx | 2 +- 5 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 std/ffi/bindgen.lx create mode 100644 std/ffi/c_types.lx create mode 100644 std/ffi/stdlib.lx create mode 100644 std/ffi/syscall.lx diff --git a/std/ffi/bindgen.lx b/std/ffi/bindgen.lx new file mode 100644 index 00000000..e69de29b diff --git a/std/ffi/c_types.lx b/std/ffi/c_types.lx new file mode 100644 index 00000000..e69de29b diff --git a/std/ffi/stdlib.lx b/std/ffi/stdlib.lx new file mode 100644 index 00000000..e69de29b diff --git a/std/ffi/syscall.lx b/std/ffi/syscall.lx new file mode 100644 index 00000000..e69de29b diff --git a/std/stl/stack.lx b/std/stl/stack.lx index 2c6c1bda..7fc2b667 100644 --- a/std/stl/stack.lx +++ b/std/stl/stack.lx @@ -100,7 +100,7 @@ pub const pop = fn(stack: *Stack) int { return value; } -const peek = fn(stack: *Stack) int { +pub const peek = fn(stack: *Stack) int { if (isEmpty(stack) == 1) { output("Stack is empty\n"); return 0;