Compare commits
No commits in common. "ad076622aa7db77ea812ba8c11164d5f78cb5c23" and "56e67121548fcefe47444152b80d5052035f8d02" have entirely different histories.
ad076622aa
...
56e6712154
19
src/main.rs
19
src/main.rs
|
@ -13,9 +13,7 @@
|
|||
// - [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] investigate using labels (which is behind a compiler flag, somehow)
|
||||
// * [ ] write parsing errors
|
||||
// * [ ] wire up Ariadne parsing errors
|
||||
// * [ ] add stack traces and code locations to panics
|
||||
// * [ ] validation
|
||||
// * [x] break this out into multiple files
|
||||
// * [x] write a tree-walk VM
|
||||
|
@ -26,8 +24,8 @@
|
|||
// * [x] guards in match clauses
|
||||
// * [x] `as` patterns
|
||||
// * [x] splat patterns in tuples, lists, dicts
|
||||
// * [x] splats in list and dict literals
|
||||
// * [x] `loop` and `recur`
|
||||
// * [ ] splats in list and dict literals
|
||||
// * [ ] `loop` and `recur`
|
||||
// * [ ] string patterns
|
||||
// * [ ] string interpolation
|
||||
// * [~] write `base` in Rust
|
||||
|
@ -57,13 +55,14 @@ use crate::base::*;
|
|||
|
||||
pub fn main() {
|
||||
let src = "
|
||||
let foo = false
|
||||
loop (foo) with {
|
||||
(:foo) -> :done
|
||||
(x as :boolean) -> recur (:bar)
|
||||
(x as :number) -> recur (:foo)
|
||||
(x as :keyword) -> recur (:foo)
|
||||
fn t () -> true
|
||||
fn f () -> false
|
||||
|
||||
fn id {
|
||||
(x) if f () -> x
|
||||
(x) -> :whoops
|
||||
}
|
||||
id (:foo)
|
||||
";
|
||||
let (tokens, lex_errs) = lexer().parse(src).into_output_errors();
|
||||
if lex_errs.len() > 0 {
|
||||
|
|
139
src/parser.rs
139
src/parser.rs
|
@ -32,17 +32,17 @@ impl<'src> fmt::Display for MatchClause<'src> {
|
|||
}
|
||||
}
|
||||
|
||||
// #[derive(Clone, Debug, PartialEq)]
|
||||
// pub struct Pair<'src> {
|
||||
// pub key: &'src str,
|
||||
// pub value: Spanned<Ast<'src>>,
|
||||
// }
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Pair<'src> {
|
||||
pub key: &'src str,
|
||||
pub value: Spanned<Ast<'src>>,
|
||||
}
|
||||
|
||||
// impl<'src> fmt::Display for Pair<'src> {
|
||||
// fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// write!(f, "pair: {}: {}", self.key, self.value.0)
|
||||
// }
|
||||
// }
|
||||
impl<'src> fmt::Display for Pair<'src> {
|
||||
fn fmt(self: &Pair<'src>, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "pair: {}: {}", self.key, self.value.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Ast<'src> {
|
||||
|
@ -59,7 +59,7 @@ pub enum Ast<'src> {
|
|||
Tuple(Vec<Spanned<Self>>),
|
||||
Arguments(Vec<Spanned<Self>>),
|
||||
List(Vec<Spanned<Self>>),
|
||||
Dict(Vec<Spanned<Self>>),
|
||||
Dict(Vec<Pair<'src>>),
|
||||
Let(Box<Spanned<Pattern<'src>>>, Box<Spanned<Self>>),
|
||||
Box(&'src str, Box<Spanned<Self>>),
|
||||
Synthetic(Box<Spanned<Self>>, Box<Spanned<Self>>, Vec<Spanned<Self>>),
|
||||
|
@ -70,10 +70,8 @@ pub enum Ast<'src> {
|
|||
Panic(Box<Spanned<Self>>),
|
||||
Do(Vec<Spanned<Self>>),
|
||||
Repeat(Box<Spanned<Self>>, Box<Spanned<Self>>),
|
||||
Splat(&'src str),
|
||||
Pair(&'src str, Box<Spanned<Self>>),
|
||||
Loop(Box<Spanned<Self>>, Vec<MatchClause<'src>>),
|
||||
Recur(Vec<Spanned<Self>>),
|
||||
// Loop(Box<Spanned<Self>>, Vec<Spanned<Self>>),
|
||||
// Recur(Vec<Spanned<Self>>),
|
||||
}
|
||||
|
||||
impl<'src> fmt::Display for Ast<'src> {
|
||||
|
@ -107,7 +105,7 @@ impl<'src> fmt::Display for Ast<'src> {
|
|||
"#{{{}}}",
|
||||
entries
|
||||
.iter()
|
||||
.map(|pair| (*pair).0.to_string())
|
||||
.map(|pair| pair.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
|
@ -186,33 +184,8 @@ impl<'src> fmt::Display for Ast<'src> {
|
|||
)
|
||||
}
|
||||
Ast::Repeat(_times, _body) => todo!(),
|
||||
Ast::Splat(word) => {
|
||||
write!(f, "splat: {}", word)
|
||||
}
|
||||
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(", ")
|
||||
)
|
||||
}
|
||||
// Ast::Loop(init, body) => todo!(),
|
||||
// Ast::Recur(args) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
.clone()
|
||||
.or(splat.clone())
|
||||
.separated_by(separators.clone())
|
||||
.allow_leading()
|
||||
.allow_trailing()
|
||||
|
@ -480,14 +439,15 @@ where
|
|||
|
||||
let pair = select! {Token::Keyword(k) => k}
|
||||
.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}
|
||||
.map_with(|w, e| (Ast::Pair(w, Box::new((Ast::Word(w), e.span()))), e.span()));
|
||||
let shorthand = select! {Token::Word(w) => w}.map_with(|w, e| Pair {
|
||||
key: w,
|
||||
value: ((Ast::Word(w), e.span())),
|
||||
});
|
||||
|
||||
let dict = pair
|
||||
.or(shorthand)
|
||||
.or(splat.clone())
|
||||
.separated_by(separators.clone())
|
||||
.allow_leading()
|
||||
.allow_trailing()
|
||||
|
@ -498,22 +458,12 @@ where
|
|||
)
|
||||
.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(
|
||||
synthetic
|
||||
.or(recur)
|
||||
.or(word)
|
||||
.or(keyword)
|
||||
.or(value)
|
||||
.or(tuple.clone())
|
||||
.or(tuple)
|
||||
.or(list)
|
||||
.or(dict)
|
||||
.labelled("simple expression"),
|
||||
|
@ -649,7 +599,7 @@ where
|
|||
})
|
||||
.labelled("function clause");
|
||||
|
||||
let fn_unguarded = tuple_pattern
|
||||
let fn_clause = tuple_pattern
|
||||
.clone()
|
||||
.then_ignore(just(Token::Punctuation("->")))
|
||||
.then(nonbinding.clone())
|
||||
|
@ -660,28 +610,10 @@ where
|
|||
})
|
||||
.labelled("function clause");
|
||||
|
||||
let fn_clause = fn_guarded.clone().or(fn_unguarded.clone());
|
||||
|
||||
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()));
|
||||
|
||||
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(
|
||||
simple
|
||||
.clone()
|
||||
|
@ -691,7 +623,6 @@ where
|
|||
.or(panic)
|
||||
.or(do_)
|
||||
.or(repeat)
|
||||
.or(r#loop)
|
||||
.labelled("nonbinding expression"),
|
||||
);
|
||||
|
||||
|
@ -727,9 +658,18 @@ where
|
|||
(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"))
|
||||
.ignore_then(word.clone())
|
||||
.then(fn_unguarded.clone())
|
||||
.then(fn_clause.clone().or(fn_guarded.clone()))
|
||||
.map_with(|(word, clause), e| {
|
||||
let name = if let Ast::Word(word) = word.0 {
|
||||
word
|
||||
|
@ -741,7 +681,16 @@ where
|
|||
|
||||
let fn_compound = just(Token::Reserved("fn"))
|
||||
.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| {
|
||||
let name = if let Ast::Word(word) = word.0 {
|
||||
word
|
||||
|
|
|
@ -25,12 +25,10 @@ pub enum Value<'src> {
|
|||
// ref-counted, immutable, persistent
|
||||
List(Vector<Self>),
|
||||
// ref-counted, immutable, persistent
|
||||
// dicts may only use keywords as keys
|
||||
Dict(HashMap<&'src str, Self>),
|
||||
Box(&'src str, Rc<RefCell<Self>>),
|
||||
Fn(Rc<Fn<'src>>),
|
||||
Base(Base<'src>),
|
||||
Recur(Vec<Self>),
|
||||
// Set(HashSet<Self>),
|
||||
// Sets are hard
|
||||
// Sets require Eq
|
||||
|
@ -59,7 +57,6 @@ impl<'src> Clone for Value<'src> {
|
|||
Value::Box(name, b) => Value::Box(name, b.clone()),
|
||||
Value::Placeholder => Value::Placeholder,
|
||||
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)
|
||||
}
|
||||
Value::Recur(..) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +138,6 @@ impl<'src> Value<'src> {
|
|||
Value::Placeholder => unreachable!(),
|
||||
Value::Args(_) => unreachable!(),
|
||||
Value::Base(_) => Value::Keyword("fn"),
|
||||
Value::Recur(..) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
57
src/vm.rs
57
src/vm.rs
|
@ -322,20 +322,8 @@ pub fn eval<'src, 'a>(
|
|||
Ast::List(members) => {
|
||||
let mut vect = Vector::new();
|
||||
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)?)
|
||||
}
|
||||
}
|
||||
Ok(Value::List(vect))
|
||||
}
|
||||
Ast::Tuple(members) => {
|
||||
|
@ -345,7 +333,7 @@ pub fn eval<'src, 'a>(
|
|||
}
|
||||
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) {
|
||||
value.clone()
|
||||
} else {
|
||||
|
@ -384,27 +372,12 @@ pub fn eval<'src, 'a>(
|
|||
Ok(Value::Tuple(Rc::new(args)))
|
||||
}
|
||||
}
|
||||
Ast::Dict(terms) => {
|
||||
Ast::Dict(pairs) => {
|
||||
let mut dict = HashMap::new();
|
||||
for term in terms {
|
||||
let (term, _) = term;
|
||||
match term {
|
||||
Ast::Pair(key, value) => {
|
||||
for Pair { key, value } in pairs {
|
||||
let value = eval(&value.0, ctx)?;
|
||||
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))
|
||||
}
|
||||
Ast::Box(name, expr) => {
|
||||
|
@ -478,27 +451,7 @@ pub fn eval<'src, 'a>(
|
|||
result = apply(next, arg, ctx)?;
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
Ast::Pair(..) => {
|
||||
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))
|
||||
}
|
||||
} // Ast::Loop(_, _) => todo!(),
|
||||
// Ast::Recur(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user