HyphaeVM - WIP
This commit is a WORK IN PROGRESS for the base implementation of the HyphaeVM. This will be squashed into a larger commit eventually when the work of implementing the HyphaeVM is finished. Of note, the ISA is mostly finished and much of the VM design is in place. Yet to be done are a few traps in mycelium, migrating pieces like the number package and the sexpr package into the VM package, and of course much testing. Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
parent
3a0a141738
commit
4ad319213d
17 changed files with 2073 additions and 17 deletions
|
|
@ -16,6 +16,9 @@
|
|||
*/
|
||||
|
||||
use core::fmt::{self, Formatter};
|
||||
use core::ops::Index;
|
||||
use core::cell::RefCell;
|
||||
|
||||
use alloc::format;
|
||||
use alloc::rc::Rc;
|
||||
use alloc::vec::Vec;
|
||||
|
|
@ -23,7 +26,7 @@ use alloc::string::String;
|
|||
|
||||
use crate::number::Number;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
#[derive(Default, Clone, PartialEq)]
|
||||
pub enum Datum {
|
||||
Number(Number),
|
||||
Bool(bool),
|
||||
|
|
@ -31,8 +34,8 @@ pub enum Datum {
|
|||
Symbol(String),
|
||||
Char(u8),
|
||||
String(Vec<u8>),
|
||||
Vector(Vec<Rc<Datum>>),
|
||||
ByteVector(Vec<u8>),
|
||||
Vector(RefCell<Vec<Rc<Datum>>>),
|
||||
ByteVector(RefCell<Vec<u8>>),
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
|
|
@ -45,7 +48,8 @@ fn byte_to_escaped_char(b: u8) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
fn fmt_vec<T: fmt::Display>(v: &Vec<T>) -> String {
|
||||
fn fmt_vec<T: fmt::Display>(ve: &RefCell<Vec<T>>) -> String {
|
||||
let v = ve.borrow();
|
||||
if v.len() == 0 {
|
||||
return String::new()
|
||||
}
|
||||
|
|
@ -102,9 +106,46 @@ impl fmt::Debug for Datum {
|
|||
}
|
||||
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
#[derive(Default, Clone, PartialEq)]
|
||||
pub struct Ast(pub Rc<Datum>, pub Rc<Datum>);
|
||||
|
||||
impl Ast {
|
||||
pub fn subsl(&self, start: isize, end: isize) -> Ast {
|
||||
if end - start == 1 {
|
||||
return Ast(Rc::from(self[start as usize].clone()), Rc::from(Datum::None))
|
||||
}
|
||||
|
||||
if end == 0 {
|
||||
return Ast(
|
||||
Rc::from((*(self.0)).clone()),
|
||||
Rc::from(Datum::None)
|
||||
)
|
||||
}
|
||||
|
||||
let Datum::List(ref next) = *self.1 else {
|
||||
panic!("index into improper list form")
|
||||
};
|
||||
|
||||
if start <= 0 {
|
||||
Ast(
|
||||
Rc::from((*(self.0)).clone()),
|
||||
Rc::from(Datum::List(
|
||||
Rc::from(next.subsl(start - 1, end - 1))))
|
||||
)
|
||||
|
||||
} else {
|
||||
next.subsl(start - 1, end - 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
let Datum::List(ref next) = *self.1 else {
|
||||
return 1
|
||||
};
|
||||
1 + next.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Ast {
|
||||
type Item = Rc<Datum>;
|
||||
|
||||
|
|
@ -127,6 +168,25 @@ impl Iterator for Ast {
|
|||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Ast {
|
||||
type Output = Datum;
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
if index == 0 {
|
||||
if let Datum::None = *self.0 {
|
||||
panic!("out of bounds indexing into AST")
|
||||
} else {
|
||||
self.0.as_ref()
|
||||
}
|
||||
} else {
|
||||
let Datum::List(ref next) = *self.1 else {
|
||||
panic!("out of bounds indexing into AST")
|
||||
};
|
||||
|
||||
next.index(index - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Ast {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "({}", self.0)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue