From 524f79733cc3f3f3bf394d4d5857190cdd3b650a Mon Sep 17 00:00:00 2001 From: Ava Affine Date: Sun, 27 Jul 2025 07:18:14 +0000 Subject: [PATCH] HyphaeVM Cons instruction prototype This commit implements the Cons instruction with desired behavior. Tests are yet to be implemented for any instruction, but are coming soon. Signed-off-by: Ava Affine --- hyphae/src/heap.rs | 18 +++++++++++++++++- hyphae/src/vm.rs | 16 +++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/hyphae/src/heap.rs b/hyphae/src/heap.rs index f49e9a1..c616803 100644 --- a/hyphae/src/heap.rs +++ b/hyphae/src/heap.rs @@ -41,7 +41,7 @@ use organelle::Number; /* Gc * This is a heap allocated Rc passed around such that it fits into - * a physical register. The pointer is to a Box>, but custom + * a physical register. The pointer is to a Box>, but custom * deref implementation will ensure that deref always points to the * encapsulated T */ @@ -201,6 +201,22 @@ impl Cons { 1 + next.len() } + + pub fn append(&mut self, arg: Gc) { + let Some(_) = self.0 else { + self.0 = Some(arg); + return + }; + + if let Some(next) = &mut self.1 { + if let Datum::Cons(next) = next.deref_mut() { + next.append(arg); + return; + } + } + + self.1 = Some(Datum::Cons(Cons(Some(arg), None)).into()); + } } diff --git a/hyphae/src/vm.rs b/hyphae/src/vm.rs index 5245af9..7bd245d 100644 --- a/hyphae/src/vm.rs +++ b/hyphae/src/vm.rs @@ -22,9 +22,10 @@ use crate::hmap::QuickMap; use crate::stackstack::StackStack; use crate::instr as i; use crate::util::{Operand, Program, Address}; -use crate::heap::{Gc, Datum}; +use crate::heap::{Gc, Datum, Cons}; use core::cell::RefCell; +use core::ops::DerefMut; use alloc::vec; use alloc::vec::Vec; @@ -475,10 +476,15 @@ impl VM { }, i::CONS => { - /* CONS BEHAVIOR - * L Datum is not list means create a new standard form list - * L Datum is list then append the second element to the first - */ + let mut l = access!(&instr.1[0]).clone(); + if let Datum::Cons(l) = l.deref_mut() { + l.append(access!(&instr.1[1]).clone()); + } else { + access!(&instr.1[0], Datum::Cons(Cons( + Some(l), + Some(access!(&instr.1[1]).clone()) + )).into()); + } }, _ => {