continue work on compiling functions

This commit is contained in:
Scott Richmond 2024-12-23 10:55:28 -05:00
parent 9f4e630544
commit a4f12c8f7d
4 changed files with 21 additions and 7 deletions

View File

@ -431,6 +431,9 @@ impl Chunk {
self.bytecode[idx] = self.bytecode.len() as u8 - idx as u8 + 2;
}
}
Fn(lfn) => {
let fn_chunk = Chunk::new()
}
_ => todo!(),
}
}

View File

@ -41,6 +41,9 @@ pub fn run(src: &'static str) {
return;
}
// ::sigh:: The AST should be 'static
// This simplifies lifetimes, and
// in any event, the AST should live forever
let parsed: &'static Spanned<Ast> = Box::leak(Box::new(parse_result.unwrap()));
let mut chunk = Chunk::new(parsed, "test", src);

View File

@ -51,6 +51,12 @@ impl fmt::Display for StringPart {
}
}
pub struct LFn {
name: &'static str,
clauses: Vec<Spanned<Ast>>,
doc: Option<&'static str>,
}
#[derive(Clone, Debug, PartialEq, Dissectible)]
pub enum Ast {
// a special Error node
@ -83,7 +89,7 @@ pub enum Ast {
Box<Option<Spanned<Self>>>,
Box<Spanned<Self>>,
),
Fn(&'static str, Vec<Spanned<Self>>, Option<&'static str>),
Fn(LFn),
FnDeclaration(&'static str),
Panic(Box<Spanned<Self>>),
Do(Vec<Spanned<Self>>),

View File

@ -9,11 +9,11 @@ use std::rc::Rc;
pub struct LFn {
pub name: &'static str,
pub body: Vec<Spanned<Ast>>,
pub doc: Option<&'static str>,
pub enclosing: Vec<(usize, Value)>,
pub has_run: bool,
pub input: &'static str,
pub src: &'static str,
// pub doc: Option<&'static str>,
// pub enclosing: Vec<(usize, Value)>,
// pub has_run: bool,
// pub input: &'static str,
// pub src: &'static str,
pub chunk: Chunk,
}
@ -31,7 +31,7 @@ pub enum Value {
List(Box<Vector<Value>>),
Dict(Box<HashMap<usize, Value>>),
Box(Rc<RefCell<Value>>),
Fn(Rc<RefCell<LFn>>),
Fn(Rc<LFn>),
}
impl std::fmt::Display for Value {
@ -72,6 +72,7 @@ impl std::fmt::Display for Value {
.join(", ")
),
Box(value) => write!(f, "box {}", value.as_ref().borrow()),
Fn(lfn) => write!(f, "fn {}", lfn.name),
_ => todo!(),
}
}
@ -115,6 +116,7 @@ impl Value {
}
String(s) => s.as_ref().clone(),
Box(x) => format!("box {{ {} }}", x.as_ref().borrow().show(ctx)),
Fn(lfn) => format!("fn {}", lfn.name),
_ => todo!(),
}
}