bring full prelude into Ludus.

This commit is contained in:
Scott Richmond 2024-12-12 19:43:56 -05:00
parent 49a0b0f8a7
commit 73e60b8ced
5 changed files with 17 additions and 7 deletions

View File

@ -295,7 +295,13 @@ fn concat {
fn contains? { fn contains? {
"Returns true if a set or list contains a value." "Returns true if a set or list contains a value."
& (value, s as :set) -> bool (base :get (s, value)) & (value, s as :set) -> bool (base :get (s, value))
(value, l as :list) -> contains? (value, set (list)) (value, l as :list) -> loop (l) with {
([]) -> false
([x]) -> eq? (x, value)
([x, ...xs]) -> if eq? (x, value)
then true
else recur (xs)
}
} }
& fn omit { & fn omit {

View File

@ -10,6 +10,8 @@ fn dec (n as :number) -> base :sub (n, 1)
fn sub (x as :number, y as :number) -> base :sub (x, y) fn sub (x as :number, y as :number) -> base :sub (x, y)
fn panics! () -> add ("foo", "bar")
fn print!(x) -> base :print! (x) fn print!(x) -> base :print! (x)
#{add, dec, sub, print!} #{add, dec, sub, print!, panics!}

View File

@ -257,7 +257,9 @@ pub fn or<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
Value::Boolean(x.bool() || y.bool()) Value::Boolean(x.bool() || y.bool())
} }
// TODO: fix this: x is a list of all the args passed to Ludus's print!
pub fn print<'src>(x: &Value<'src>) -> Value<'src> { pub fn print<'src>(x: &Value<'src>) -> Value<'src> {
// dbg!(x)
println!("{}", x); println!("{}", x);
Value::Keyword("ok") Value::Keyword("ok")
} }

View File

@ -15,7 +15,7 @@
// * [x] investigate using labels (which is behind a compiler flag, somehow) // * [x] investigate using labels (which is behind a compiler flag, somehow)
// * [ ] write parsing errors // * [ ] write parsing errors
// * [ ] wire up Ariadne parsing errors // * [ ] wire up Ariadne parsing errors
// * [ ] add stack traces and code locations to panics // * [x] add stack traces and code locations to panics
// * [x] validation // * [x] validation
// * [x] break this out into multiple files // * [x] break this out into multiple files
// * [x] write a tree-walk VM // * [x] write a tree-walk VM
@ -70,7 +70,7 @@ pub fn prelude<'src>() -> (
Vec<(String, Value<'src>)>, Vec<(String, Value<'src>)>,
std::collections::HashMap<*const Ast, FnInfo>, std::collections::HashMap<*const Ast, FnInfo>,
) { ) {
let prelude = Asset::get("test_prelude.ld").unwrap().data.into_owned(); let prelude = Asset::get("prelude.ld").unwrap().data.into_owned();
// we know for sure Prelude should live through the whole run of the program // we know for sure Prelude should live through the whole run of the program
let leaked = Box::leak(Box::new(prelude)); let leaked = Box::leak(Box::new(prelude));
let prelude = std::str::from_utf8(leaked).unwrap(); let prelude = std::str::from_utf8(leaked).unwrap();

View File

@ -536,7 +536,7 @@ impl<'a, 'src: 'a> Validator<'a, 'src> {
unreachable!() unreachable!()
}; };
dbg!(&input); // dbg!(&input);
let tailpos = self.status.tail_position; let tailpos = self.status.tail_position;
self.status.tail_position = true; self.status.tail_position = true;
@ -551,7 +551,7 @@ impl<'a, 'src: 'a> Validator<'a, 'src> {
self.ast = expr; self.ast = expr;
self.span = *span; self.span = *span;
let arity = self.arity(); let arity = self.arity();
dbg!(&arity); // dbg!(&arity);
match arity { match arity {
Arity::Fixed(clause_arity) => { Arity::Fixed(clause_arity) => {
if clause_arity != loop_arity { if clause_arity != loop_arity {
@ -644,7 +644,7 @@ impl<'a, 'src: 'a> Validator<'a, 'src> {
}, },
_ => { _ => {
println!("internal Ludus error: unexpected splat pattern"); println!("internal Ludus error: unexpected splat pattern");
dbg!(splatted); // dbg!(splatted);
unreachable!() unreachable!()
} }
} }