Compare commits

..

No commits in common. "ad076622aa7db77ea812ba8c11164d5f78cb5c23" and "56e67121548fcefe47444152b80d5052035f8d02" have entirely different histories.

4 changed files with 61 additions and 165 deletions

View File

@ -13,9 +13,7 @@
// - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc. // - [x] do this to extract/simplify/DRY things like tuple patterns, fn clauses, etc.
// * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version // * [x] Work around chumsky::Stream::from_iter().spanned disappearing in most recent version
// * [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
// * [ ] wire up Ariadne parsing errors // * [ ] wire up Ariadne parsing errors
// * [ ] add stack traces and code locations to panics
// * [ ] validation // * [ ] 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
@ -26,8 +24,8 @@
// * [x] guards in match clauses // * [x] guards in match clauses
// * [x] `as` patterns // * [x] `as` patterns
// * [x] splat patterns in tuples, lists, dicts // * [x] splat patterns in tuples, lists, dicts
// * [x] splats in list and dict literals // * [ ] splats in list and dict literals
// * [x] `loop` and `recur` // * [ ] `loop` and `recur`
// * [ ] string patterns // * [ ] string patterns
// * [ ] string interpolation // * [ ] string interpolation
// * [~] write `base` in Rust // * [~] write `base` in Rust
@ -57,13 +55,14 @@ use crate::base::*;
pub fn main() { pub fn main() {
let src = " let src = "
let foo = false fn t () -> true
loop (foo) with { fn f () -> false
(:foo) -> :done
(x as :boolean) -> recur (:bar) fn id {
(x as :number) -> recur (:foo) (x) if f () -> x
(x as :keyword) -> recur (:foo) (x) -> :whoops
} }
id (:foo)
"; ";
let (tokens, lex_errs) = lexer().parse(src).into_output_errors(); let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
if lex_errs.len() > 0 { if lex_errs.len() > 0 {

View File

@ -32,17 +32,17 @@ impl<'src> fmt::Display for MatchClause<'src> {
} }
} }
// #[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
// pub struct Pair<'src> { pub struct Pair<'src> {
// pub key: &'src str, pub key: &'src str,
// pub value: Spanned<Ast<'src>>, pub value: Spanned<Ast<'src>>,
// } }
// impl<'src> fmt::Display for Pair<'src> { impl<'src> fmt::Display for Pair<'src> {
// fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result {
// write!(f, "pair: {}: {}", self.key, self.value.0) write!(f, "pair: {}: {}", self.key, self.value.0)
// } }
// } }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Ast<'src> { pub enum Ast<'src> {
@ -59,7 +59,7 @@ pub enum Ast<'src> {
Tuple(Vec<Spanned<Self>>), Tuple(Vec<Spanned<Self>>),
Arguments(Vec<Spanned<Self>>), Arguments(Vec<Spanned<Self>>),
List(Vec<Spanned<Self>>), List(Vec<Spanned<Self>>),
Dict(Vec<Spanned<Self>>), Dict(Vec<Pair<'src>>),
Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>), Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>),
Box(&'src str, Box<Spanned<Self>>), Box(&'src str, Box<Spanned<Self>>),
Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>), Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>),
@ -70,10 +70,8 @@ pub enum Ast<'src> {
Panic(Box<Spanned<Self>>), Panic(Box<Spanned<Self>>),
Do(Vec<Spanned<Self>>), Do(Vec<Spanned<Self>>),
Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>), Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>),
Splat(&'src str), // Loop(Box<Spanned<Self>>, Vec<Spanned<Self>>),
Pair(&'src str, Box<Spanned<Self>>), // Recur(Vec<Spanned<Self>>),
Loop(Box<Spanned<Self>>, Vec<MatchClause<'src>>),
Recur(Vec<Spanned<Self>>),
} }
impl<'src> fmt::Display for Ast<'src> { impl<'src> fmt::Display for Ast<'src> {
@ -107,7 +105,7 @@ impl<'src> fmt::Display for Ast<'src> {
"#{{{}}}", "#{{{}}}",
entries entries
.iter() .iter()
.map(|pair| (*pair).0.to_string()) .map(|pair| pair.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", ") .join(", ")
), ),
@ -186,33 +184,8 @@ impl<'src> fmt::Display for Ast<'src> {
) )
} }
Ast::Repeat(_times, _body) => todo!(), Ast::Repeat(_times, _body) => todo!(),
Ast::Splat(word) => { // Ast::Loop(init, body) => todo!(),
write!(f, "splat: {}", word) // Ast::Recur(args) => todo!(),
}
Ast::Pair(k, v) => {
write!(f, "pair: {} {}", k, (*v).0.to_string())
}
Ast::Loop(init, body) => {
write!(
f,
"loop: {} with {}",
(*init).0.to_string(),
body.iter()
.map(|clause| clause.to_string())
.collect::<Vec<_>>()
.join("\n")
)
}
Ast::Recur(args) => {
write!(
f,
"recur: {}",
args.iter()
.map(|(arg, _)| arg.to_string())
.collect::<Vec<_>>()
.join(", ")
)
}
} }
} }
} }
@ -455,22 +428,8 @@ where
) )
}); });
let splat = just(Token::Punctuation("..."))
.ignore_then(word.clone())
.map_with(|(w, _), e| {
(
Ast::Splat(if let Ast::Word(w) = w {
w
} else {
unreachable!()
}),
e.span(),
)
});
let list = simple let list = simple
.clone() .clone()
.or(splat.clone())
.separated_by(separators.clone()) .separated_by(separators.clone())
.allow_leading() .allow_leading()
.allow_trailing() .allow_trailing()
@ -480,14 +439,15 @@ where
let pair = select! {Token::Keyword(k) => k} let pair = select! {Token::Keyword(k) => k}
.then(simple.clone()) .then(simple.clone())
.map_with(|(key, value), e| (Ast::Pair(key, Box::new(value)), e.span())); .map_with(|(key, value), e| Pair { key, value });
let shorthand = select! {Token::Word(w) => w} let shorthand = select! {Token::Word(w) => w}.map_with(|w, e| Pair {
.map_with(|w, e| (Ast::Pair(w, Box::new((Ast::Word(w), e.span()))), e.span())); key: w,
value: ((Ast::Word(w), e.span())),
});
let dict = pair let dict = pair
.or(shorthand) .or(shorthand)
.or(splat.clone())
.separated_by(separators.clone()) .separated_by(separators.clone())
.allow_leading() .allow_leading()
.allow_trailing() .allow_trailing()
@ -498,22 +458,12 @@ where
) )
.map_with(|dict, e| (Ast::Dict(dict), e.span())); .map_with(|dict, e| (Ast::Dict(dict), e.span()));
let recur = just(Token::Reserved("recur"))
.ignore_then(tuple.clone())
.map_with(|args, e| {
let (Ast::Tuple(args), _) = args else {
unreachable!()
};
(Ast::Recur(args), e.span())
});
simple.define( simple.define(
synthetic synthetic
.or(recur)
.or(word) .or(word)
.or(keyword) .or(keyword)
.or(value) .or(value)
.or(tuple.clone()) .or(tuple)
.or(list) .or(list)
.or(dict) .or(dict)
.labelled("simple expression"), .labelled("simple expression"),
@ -649,7 +599,7 @@ where
}) })
.labelled("function clause"); .labelled("function clause");
let fn_unguarded = tuple_pattern let fn_clause = tuple_pattern
.clone() .clone()
.then_ignore(just(Token::Punctuation("->"))) .then_ignore(just(Token::Punctuation("->")))
.then(nonbinding.clone()) .then(nonbinding.clone())
@ -660,28 +610,10 @@ where
}) })
.labelled("function clause"); .labelled("function clause");
let fn_clause = fn_guarded.clone().or(fn_unguarded.clone());
let lambda = just(Token::Reserved("fn")) let lambda = just(Token::Reserved("fn"))
.ignore_then(fn_unguarded.clone()) .ignore_then(fn_clause.clone())
.map_with(|clause, e| (Ast::Fn("anonymous", vec![clause]), e.span())); .map_with(|clause, e| (Ast::Fn("anonymous", vec![clause]), e.span()));
let fn_multiclause = fn_clause
.clone()
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}")));
let fn_single_clause = fn_clause.clone().map_with(|c, e| vec![c]);
let r#loop = just(Token::Reserved("loop"))
.ignore_then(tuple.clone())
.then_ignore(just(Token::Reserved("with")))
.then(fn_multiclause.clone().or(fn_single_clause.clone()))
.map_with(|(init, body), e| (Ast::Loop(Box::new(init), body), e.span()));
nonbinding.define( nonbinding.define(
simple simple
.clone() .clone()
@ -691,7 +623,6 @@ where
.or(panic) .or(panic)
.or(do_) .or(do_)
.or(repeat) .or(repeat)
.or(r#loop)
.labelled("nonbinding expression"), .labelled("nonbinding expression"),
); );
@ -727,9 +658,18 @@ where
(Ast::FnDeclaration(name), e.span()) (Ast::FnDeclaration(name), e.span())
}); });
// let tuple_pattern = pattern
// .clone()
// .separated_by(separators.clone())
// .allow_leading()
// .allow_trailing()
// .collect()
// .delimited_by(just(Token::Punctuation("(")), just(Token::Punctuation(")")))
// .map_with(|tuple, e| (Pattern::Tuple(tuple), e.span()));
let fn_named = just(Token::Reserved("fn")) let fn_named = just(Token::Reserved("fn"))
.ignore_then(word.clone()) .ignore_then(word.clone())
.then(fn_unguarded.clone()) .then(fn_clause.clone().or(fn_guarded.clone()))
.map_with(|(word, clause), e| { .map_with(|(word, clause), e| {
let name = if let Ast::Word(word) = word.0 { let name = if let Ast::Word(word) = word.0 {
word word
@ -741,7 +681,16 @@ where
let fn_compound = just(Token::Reserved("fn")) let fn_compound = just(Token::Reserved("fn"))
.ignore_then(word.clone()) .ignore_then(word.clone())
.then(fn_multiclause.clone()) .then(
fn_clause
.clone()
.or(fn_guarded.clone())
.separated_by(terminators.clone())
.allow_leading()
.allow_trailing()
.collect()
.delimited_by(just(Token::Punctuation("{")), just(Token::Punctuation("}"))),
)
.map_with(|(word, clauses), e| { .map_with(|(word, clauses), e| {
let name = if let Ast::Word(word) = word.0 { let name = if let Ast::Word(word) = word.0 {
word word

View File

@ -25,12 +25,10 @@ pub enum Value<'src> {
// ref-counted, immutable, persistent // ref-counted, immutable, persistent
List(Vector<Self>), List(Vector<Self>),
// ref-counted, immutable, persistent // ref-counted, immutable, persistent
// dicts may only use keywords as keys
Dict(HashMap<&'src str, Self>), Dict(HashMap<&'src str, Self>),
Box(&'src str, Rc<RefCell<Self>>), Box(&'src str, Rc<RefCell<Self>>),
Fn(Rc<Fn<'src>>), Fn(Rc<Fn<'src>>),
Base(Base<'src>), Base(Base<'src>),
Recur(Vec<Self>),
// Set(HashSet<Self>), // Set(HashSet<Self>),
// Sets are hard // Sets are hard
// Sets require Eq // Sets require Eq
@ -59,7 +57,6 @@ impl<'src> Clone for Value<'src> {
Value::Box(name, b) => Value::Box(name, b.clone()), Value::Box(name, b) => Value::Box(name, b.clone()),
Value::Placeholder => Value::Placeholder, Value::Placeholder => Value::Placeholder,
Value::Base(b) => Value::Base(b.clone()), Value::Base(b) => Value::Base(b.clone()),
Value::Recur(..) => unreachable!(),
} }
} }
} }
@ -114,7 +111,6 @@ impl<'src> fmt::Display for Value<'src> {
}; };
write!(f, "base fn {}", name) write!(f, "base fn {}", name)
} }
Value::Recur(..) => unreachable!(),
} }
} }
} }
@ -142,7 +138,6 @@ impl<'src> Value<'src> {
Value::Placeholder => unreachable!(), Value::Placeholder => unreachable!(),
Value::Args(_) => unreachable!(), Value::Args(_) => unreachable!(),
Value::Base(_) => Value::Keyword("fn"), Value::Base(_) => Value::Keyword("fn"),
Value::Recur(..) => unreachable!(),
} }
} }
} }

View File

@ -322,20 +322,8 @@ pub fn eval<'src, 'a>(
Ast::List(members) => { Ast::List(members) => {
let mut vect = Vector::new(); let mut vect = Vector::new();
for member in members { for member in members {
if let Ast::Splat(_) = member.0 {
let to_splat = eval(&member.0, ctx)?;
match to_splat {
Value::List(list) => vect.append(list),
_ => {
return Err(LudusError {
msg: "only lists may be splatted into lists".to_string(),
})
}
}
} else {
vect.push_back(eval(&member.0, ctx)?) vect.push_back(eval(&member.0, ctx)?)
} }
}
Ok(Value::List(vect)) Ok(Value::List(vect))
} }
Ast::Tuple(members) => { Ast::Tuple(members) => {
@ -345,7 +333,7 @@ pub fn eval<'src, 'a>(
} }
Ok(Value::Tuple(Rc::new(vect))) Ok(Value::Tuple(Rc::new(vect)))
} }
Ast::Word(w) | Ast::Splat(w) => { Ast::Word(w) => {
let val = if let Some((_, value)) = ctx.iter().rev().find(|(name, _)| w == name) { let val = if let Some((_, value)) = ctx.iter().rev().find(|(name, _)| w == name) {
value.clone() value.clone()
} else { } else {
@ -384,27 +372,12 @@ pub fn eval<'src, 'a>(
Ok(Value::Tuple(Rc::new(args))) Ok(Value::Tuple(Rc::new(args)))
} }
} }
Ast::Dict(terms) => { Ast::Dict(pairs) => {
let mut dict = HashMap::new(); let mut dict = HashMap::new();
for term in terms { for Pair { key, value } in pairs {
let (term, _) = term;
match term {
Ast::Pair(key, value) => {
let value = eval(&value.0, ctx)?; let value = eval(&value.0, ctx)?;
dict.insert(*key, value); dict.insert(*key, value);
} }
Ast::Splat(_) => {
let resolved = eval(term, ctx)?;
let Value::Dict(to_splat) = resolved else {
return Err(LudusError {
msg: "cannot splat non-dict into dict".to_string(),
});
};
dict = to_splat.union(dict);
}
_ => unreachable!(),
}
}
Ok(Value::Dict(dict)) Ok(Value::Dict(dict))
} }
Ast::Box(name, expr) => { Ast::Box(name, expr) => {
@ -478,27 +451,7 @@ pub fn eval<'src, 'a>(
result = apply(next, arg, ctx)?; result = apply(next, arg, ctx)?;
} }
Ok(result) Ok(result)
} } // Ast::Loop(_, _) => todo!(),
Ast::Pair(..) => { // Ast::Recur(_) => todo!(),
unreachable!()
}
Ast::Loop(init, clauses) => {
let mut args = eval(&init.0, ctx)?;
loop {
let result = match_clauses(&args, clauses, ctx)?;
if let Value::Recur(recur_args) = result {
args = Value::Tuple(Rc::new(recur_args));
} else {
return Ok(result);
}
}
}
Ast::Recur(args) => {
let mut vect = Vec::new();
for arg in args {
vect.push(eval(&arg.0, ctx)?);
}
Ok(Value::Recur(vect))
}
} }
} }