HyphaeVM Cons instruction prototype
All checks were successful
per-push tests / build (push) Successful in 49s
per-push tests / test-frontend (push) Successful in 33s
per-push tests / test-utility (push) Successful in 35s
per-push tests / test-backend (push) Successful in 29s
per-push tests / timed-decomposer-parse (push) Successful in 34s

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 <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2025-07-27 07:18:14 +00:00
parent cf626b2bfc
commit 524f79733c
2 changed files with 28 additions and 6 deletions

View file

@ -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<Rc<T>>, but custom
* a physical register. The pointer is to a Box<Rc<T>>, 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<Datum>) {
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());
}
}

View file

@ -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());
}
},
_ => {