can evaluate simple expressions
This commit is contained in:
parent
2b447b5517
commit
52950c255a
File diff suppressed because it is too large
Load diff
|
|
@ -8,12 +8,17 @@ unsafe extern "C" {
|
||||||
fn getc() -> SourceIterResult;
|
fn getc() -> SourceIterResult;
|
||||||
fn peekc() -> SourceIterResult;
|
fn peekc() -> SourceIterResult;
|
||||||
|
|
||||||
|
fn eval(expr: Object, env: Object) -> Object;
|
||||||
|
fn init_env() -> Object;
|
||||||
|
|
||||||
fn parse_next_token() -> Object;
|
fn parse_next_token() -> Object;
|
||||||
|
|
||||||
#[link_name = "ifile"]
|
#[link_name = "ifile"]
|
||||||
static mut IFILE: Source;
|
static mut IFILE: Source;
|
||||||
#[link_name = "nil"]
|
#[link_name = "nil"]
|
||||||
static NIL: ();
|
static NIL: ();
|
||||||
|
#[link_name = "env"]
|
||||||
|
static GENV: ();
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
@ -59,8 +64,8 @@ impl std::fmt::Debug for Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_prim(prim: *const (), f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt_prim(prim: *const (), f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let prim_ptr = unsafe { prim.cast::<usize>().read() };
|
let prim_ptr = unsafe { prim.cast::<*const ()>().read() };
|
||||||
write!(f, "#<prim {}>", prim_ptr)
|
write!(f, "#<prim {:?}>", prim_ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_cons(cons: *const (), f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt_cons(cons: *const (), f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
|
@ -110,6 +115,7 @@ impl std::fmt::Debug for Object {
|
||||||
atom.byte_add(8).cast::<usize>().read(),
|
atom.byte_add(8).cast::<usize>().read(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let slice = unsafe { core::slice::from_raw_parts(ptr, len) };
|
let slice = unsafe { core::slice::from_raw_parts(ptr, len) };
|
||||||
let string = core::str::from_utf8(slice).unwrap_or("<invalid utf-8>");
|
let string = core::str::from_utf8(slice).unwrap_or("<invalid utf-8>");
|
||||||
write!(f, "'{string}")
|
write!(f, "'{string}")
|
||||||
|
|
@ -335,4 +341,28 @@ mod tests {
|
||||||
println!("{:?}", obj);
|
println!("{:?}", obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static mut ENV_INIT: std::cell::LazyCell<Object> =
|
||||||
|
std::cell::LazyCell::new(|| unsafe { init_env() });
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn global_env() {
|
||||||
|
unsafe {
|
||||||
|
let env = unsafe { *ENV_INIT };
|
||||||
|
println!("{env:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn eval_add() {
|
||||||
|
let file = ManuallyDrop::new(File::open("tests/add.l").unwrap());
|
||||||
|
unsafe {
|
||||||
|
init_source(&raw mut IFILE, file.as_raw_fd());
|
||||||
|
let expr = parse_next_token();
|
||||||
|
let env = unsafe { *ENV_INIT };
|
||||||
|
let result = eval(expr, env);
|
||||||
|
eprint!("done: ");
|
||||||
|
eprintln!("{result:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue