rudus/src/process.rs

563 lines
21 KiB
Rust
Raw Normal View History

2024-12-09 04:33:46 +00:00
use crate::base::*;
use crate::parser::*;
2024-12-11 03:26:52 +00:00
use crate::spans::*;
2024-12-11 20:42:50 +00:00
use crate::value::Value;
2024-12-09 04:33:46 +00:00
use imbl::HashMap;
use imbl::Vector;
use std::cell::RefCell;
use std::rc::Rc;
pub struct LErr {
pub msg: String,
pub trace: Vec<String>,
}
impl LErr {
pub fn new(msg: String) -> LErr {
LErr {
msg: msg.to_string(),
trace: vec![],
}
}
pub fn add_trace(mut self, fn_name: String) -> LErr {
self.trace.push(fn_name);
self
}
}
type LResult<'src> = Result<Value<'src>, LErr>;
2024-12-11 20:42:50 +00:00
pub struct Process<'src> {
2024-12-09 04:33:46 +00:00
pub locals: Vec<(String, Value<'src>)>,
pub prelude: Vec<(String, Value<'src>)>,
pub ast: &'src Ast,
2024-12-11 20:45:34 +00:00
pub args: Value<'src>,
2024-12-09 04:33:46 +00:00
}
2024-12-11 20:42:50 +00:00
impl<'src> Process<'src> {
2024-12-09 04:33:46 +00:00
pub fn resolve(&self, word: &String) -> LResult<'src> {
let resolved_local = self.locals.iter().rev().find(|(name, _)| word == name);
match resolved_local {
Some((_, value)) => Ok(value.clone()),
None => {
let resolved_prelude = self.prelude.iter().rev().find(|(name, _)| word == name);
match resolved_prelude {
Some((_, value)) => Ok(value.clone()),
None => Err(LErr::new(format!("unbound name {word}"))),
}
}
}
}
pub fn bind(&mut self, word: String, value: &Value<'src>) {
self.locals.push((word, value.clone()));
}
pub fn pop_to(&mut self, n: usize) {
while self.locals.len() > n {
self.locals.pop();
}
}
2024-12-11 20:42:50 +00:00
pub fn match_eq<T>(&self, x: T, y: T) -> Option<&Process<'src>>
2024-12-09 04:33:46 +00:00
where
T: PartialEq,
{
if x == y {
Some(self)
2024-12-09 04:33:46 +00:00
} else {
None
}
}
2024-12-11 20:42:50 +00:00
pub fn match_pattern(&mut self, patt: &Ast, val: &Value<'src>) -> Option<&Process<'src>> {
2024-12-11 03:26:52 +00:00
use Ast::*;
2024-12-09 04:33:46 +00:00
match (patt, val) {
2024-12-11 03:26:52 +00:00
(NilPattern, Value::Nil) => Some(self),
(PlaceholderPattern, _) => Some(self),
(NumberPattern(x), Value::Number(y)) => self.match_eq(x, y),
(BooleanPattern(x), Value::Boolean(y)) => self.match_eq(x, y),
(KeywordPattern(x), Value::Keyword(y)) => self.match_eq(x, y),
(StringPattern(x), Value::InternedString(y)) => self.match_eq(x, y),
(StringPattern(x), Value::AllocatedString(y)) => self.match_eq(&x.to_string(), y),
(InterpolatedPattern(_, StringMatcher(matcher)), Value::InternedString(y)) => {
2024-12-09 04:33:46 +00:00
match matcher(y.to_string()) {
Some(matches) => {
let mut matches = matches
.iter()
.map(|(word, string)| {
(
word.clone(),
Value::AllocatedString(Rc::new(string.clone())),
)
})
.collect::<Vec<_>>();
self.locals.append(&mut matches);
Some(self)
}
None => None,
}
}
2024-12-11 03:26:52 +00:00
(WordPattern(w), val) => {
2024-12-09 04:33:46 +00:00
self.bind(w.to_string(), &val);
Some(self)
}
2024-12-11 03:26:52 +00:00
(AsPattern(word, type_str), value) => {
2024-12-09 04:33:46 +00:00
let ludus_type = r#type(value);
let type_kw = Value::Keyword(type_str);
if type_kw == ludus_type {
self.bind(word.to_string(), value);
Some(self)
} else {
None
}
}
2024-12-11 03:26:52 +00:00
(TuplePattern(x), Value::Tuple(y)) => {
let has_splat = x.iter().any(|patt| matches!(patt, (Splattern(_), _)));
2024-12-09 04:33:46 +00:00
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
return None;
};
let to = self.locals.len();
for i in 0..x.len() {
2024-12-11 03:26:52 +00:00
if let Splattern(patt) = &x[i].0 {
2024-12-09 04:33:46 +00:00
let mut list = Vector::new();
for i in i..y.len() {
list.push_back(y[i].clone())
}
let list = Value::List(list);
self.match_pattern(&patt.0, &list);
} else if self.match_pattern(&x[i].0, &y[i]).is_none() {
self.pop_to(to);
return None;
}
}
Some(self)
}
2024-12-11 03:26:52 +00:00
(ListPattern(x), Value::List(y)) => {
let has_splat = x.iter().any(|patt| matches!(patt, (Splattern(_), _)));
2024-12-09 04:33:46 +00:00
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
return None;
};
let to = self.locals.len();
for (i, (patt, _)) in x.iter().enumerate() {
2024-12-11 03:26:52 +00:00
if let Splattern(patt) = &patt {
2024-12-09 04:33:46 +00:00
let list = Value::List(y.skip(i));
self.match_pattern(&patt.0, &list);
} else if self.match_pattern(patt, y.get(i).unwrap()).is_none() {
self.pop_to(to);
return None;
}
}
Some(self)
}
// TODO: optimize this on several levels
// - [ ] opportunistic mutation
// - [ ] get rid of all the pointer indirection in word splats
2024-12-11 03:26:52 +00:00
(DictPattern(x), Value::Dict(y)) => {
let has_splat = x.iter().any(|patt| matches!(patt, (Splattern(_), _)));
2024-12-09 04:33:46 +00:00
if x.len() > y.len() || (!has_splat && x.len() != y.len()) {
return None;
};
let to = self.locals.len();
let mut matched = vec![];
for (pattern, _) in x {
match pattern {
2024-12-11 03:26:52 +00:00
PairPattern(key, patt) => {
2024-12-09 04:33:46 +00:00
if let Some(val) = y.get(key) {
if self.match_pattern(&patt.0, val).is_none() {
self.pop_to(to);
return None;
} else {
matched.push(key);
}
} else {
return None;
};
}
2024-12-11 03:26:52 +00:00
Splattern(pattern) => match pattern.0 {
WordPattern(w) => {
2024-12-09 04:33:46 +00:00
// TODO: find a way to take ownership
// this will ALWAYS make structural changes, because of this clone
// we want opportunistic mutation if possible
let mut unmatched = y.clone();
for key in matched.iter() {
unmatched.remove(*key);
}
self.bind(w.to_string(), &Value::Dict(unmatched));
}
2024-12-11 03:26:52 +00:00
PlaceholderPattern => (),
2024-12-09 04:33:46 +00:00
_ => unreachable!(),
},
_ => unreachable!(),
}
}
Some(self)
}
_ => None,
}
}
pub fn match_clauses(
&mut self,
value: &Value<'src>,
2024-12-11 03:26:52 +00:00
clauses: &'src [Spanned<Ast>],
2024-12-09 04:33:46 +00:00
) -> LResult<'src> {
{
let parent = self.ast;
let to = self.locals.len();
2024-12-11 03:26:52 +00:00
let mut clauses = clauses.iter();
while let Some((Ast::MatchClause(patt, guard, body), _)) = clauses.next() {
2024-12-09 04:33:46 +00:00
if let Some(_) = self.match_pattern(&patt.0, value) {
2024-12-11 03:26:52 +00:00
let pass_guard = match guard.as_ref() {
2024-12-09 04:33:46 +00:00
None => true,
Some((ast, _)) => {
self.ast = ast;
let guard_res = self.eval();
match &guard_res {
Err(_) => return guard_res,
Ok(val) => val.bool(),
}
}
};
if !pass_guard {
self.pop_to(to);
continue;
}
self.ast = &body.0;
let res = self.eval();
self.pop_to(to);
self.ast = parent;
return res;
}
}
Err(LErr::new(format!("no match")))
}
}
pub fn apply(&mut self, callee: Value<'src>, caller: Value<'src>) -> LResult<'src> {
2024-12-11 03:26:52 +00:00
use Value::*;
2024-12-09 04:33:46 +00:00
match (callee, caller) {
2024-12-11 03:26:52 +00:00
(Keyword(kw), Dict(dict)) => {
2024-12-09 04:33:46 +00:00
if let Some(val) = dict.get(kw) {
Ok(val.clone())
} else {
2024-12-11 03:26:52 +00:00
Ok(Nil)
2024-12-09 04:33:46 +00:00
}
}
2024-12-11 03:26:52 +00:00
(Dict(dict), Keyword(kw)) => {
2024-12-09 04:33:46 +00:00
if let Some(val) = dict.get(kw) {
Ok(val.clone())
} else {
2024-12-11 03:26:52 +00:00
Ok(Nil)
2024-12-09 04:33:46 +00:00
}
}
2024-12-11 03:26:52 +00:00
(Fn(f), Tuple(args)) => {
let args = Tuple(args);
2024-12-09 04:33:46 +00:00
self.match_clauses(&args, f.body)
}
2024-12-11 03:26:52 +00:00
(Fn(_f), Args(_args)) => todo!(),
(_, Keyword(_)) => Ok(Nil),
(_, Args(_)) => Err(LErr::new(format!("you may only call a function"))),
(Base(f), Tuple(args)) => match f {
BaseFn::Nullary(f) => {
2024-12-09 04:33:46 +00:00
let num_args = args.len();
if num_args != 0 {
Err(LErr::new(format!(
"wrong arity: expected 0 arguments, got {num_args}"
)))
} else {
Ok(f())
}
}
2024-12-11 03:26:52 +00:00
BaseFn::Unary(f) => {
2024-12-09 04:33:46 +00:00
let num_args = args.len();
if num_args != 1 {
Err(LErr::new(format!(
"wrong arity: expected 1 argument, got {num_args}"
)))
} else {
Ok(f(&args[0]))
}
}
2024-12-11 03:26:52 +00:00
BaseFn::Binary(r#fn) => {
2024-12-09 04:33:46 +00:00
let num_args = args.len();
if num_args != 2 {
Err(LErr::new(format!(
"wrong arity: expected 2 arguments, got {num_args}"
)))
} else {
Ok(r#fn(&args[0], &args[1]))
}
}
2024-12-11 03:26:52 +00:00
BaseFn::Ternary(f) => {
2024-12-09 04:33:46 +00:00
let num_args = args.len();
if num_args != 3 {
Err(LErr::new(format!(
"wrong arity: expected 3 arguments, got {num_args}"
)))
} else {
Ok(f(&args[0], &args[1], &args[2]))
}
}
},
_ => unreachable!(),
}
}
pub fn eval(&mut self) -> LResult<'src> {
2024-12-11 03:26:52 +00:00
use Ast::*;
2024-12-09 04:33:46 +00:00
let root = self.ast;
let result = match self.ast {
2024-12-11 03:26:52 +00:00
Nil => Ok(Value::Nil),
Boolean(b) => Ok(Value::Boolean(*b)),
Number(n) => Ok(Value::Number(*n)),
Keyword(k) => Ok(Value::Keyword(k)),
String(s) => Ok(Value::InternedString(s)),
Interpolated(parts) => {
let mut interpolated = std::string::String::new();
2024-12-09 04:33:46 +00:00
for part in parts {
match &part.0 {
StringPart::Data(s) => interpolated.push_str(s.as_str()),
StringPart::Word(w) => {
let val = self.resolve(w)?;
interpolated.push_str(val.interpolate().as_str())
}
StringPart::Inline(_) => unreachable!(),
}
}
Ok(Value::AllocatedString(Rc::new(interpolated)))
}
2024-12-11 03:26:52 +00:00
Block(exprs) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let to = self.locals.len();
let mut result = Value::Nil;
for (expr, _) in exprs {
self.ast = &expr;
result = self.eval()?;
}
self.pop_to(to);
self.ast = parent;
Ok(result)
}
2024-12-11 03:26:52 +00:00
If(cond, if_true, if_false) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &cond.0;
let truthy = self.eval()?.bool();
self.ast = if truthy { &if_true.0 } else { &if_false.0 };
let result = self.eval();
self.ast = parent;
result
}
2024-12-11 03:26:52 +00:00
List(members) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let mut vect = Vector::new();
for member in members {
self.ast = &member.0;
if let Ast::Splat(_) = self.ast {
let to_splat = self.eval()?;
match to_splat {
Value::List(list) => vect.append(list),
_ => {
return Err(LErr::new(format!(
"only lists may be splatted into lists"
)))
}
}
} else {
vect.push_back(self.eval()?)
}
}
self.ast = parent;
Ok(Value::List(vect))
}
2024-12-11 03:26:52 +00:00
Tuple(members) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let mut vect = Vec::new();
for member in members {
self.ast = &member.0;
vect.push(self.eval()?);
}
self.ast = parent;
Ok(Value::Tuple(Rc::new(vect)))
}
2024-12-11 03:26:52 +00:00
Word(w) | Ast::Splat(w) => {
2024-12-09 04:33:46 +00:00
let val = self.resolve(&w.to_string())?;
Ok(val)
}
2024-12-11 03:26:52 +00:00
Let(patt, expr) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &expr.0;
let val = self.eval()?;
let result = match self.match_pattern(&patt.0, &val) {
Some(_) => Ok(val),
None => Err(LErr::new(format!("no match"))),
};
self.ast = parent;
result
}
2024-12-11 03:26:52 +00:00
Placeholder => Ok(Value::Placeholder),
Arguments(a) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let mut args = vec![];
for (arg, _) in a.iter() {
self.ast = arg;
let arg = self.eval()?;
args.push(arg);
}
let result = if args.iter().any(|arg| matches!(arg, Value::Placeholder)) {
Ok(Value::Args(Rc::new(args)))
} else {
Ok(Value::Tuple(Rc::new(args)))
};
self.ast = parent;
result
}
2024-12-11 03:26:52 +00:00
Dict(terms) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let mut dict = HashMap::new();
for term in terms {
let (term, _) = term;
match term {
Ast::Pair(key, value) => {
self.ast = &value.0;
let value = self.eval()?;
dict.insert(*key, value);
}
Ast::Splat(_) => {
self.ast = term;
let resolved = self.eval()?;
let Value::Dict(to_splat) = resolved else {
return Err(LErr::new(format!("cannot splat non-dict into dict")));
};
dict = to_splat.union(dict);
}
_ => unreachable!(),
}
}
self.ast = parent;
Ok(Value::Dict(dict))
}
2024-12-11 03:26:52 +00:00
LBox(name, expr) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &expr.0;
let val = self.eval()?;
let boxed = Value::Box(name, Rc::new(RefCell::new(val)));
self.bind(name.to_string(), &boxed);
self.ast = parent;
Ok(boxed)
}
2024-12-11 03:26:52 +00:00
Synthetic(root, first, rest) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &root.0;
let root = self.eval()?;
self.ast = &first.0;
let first = self.eval()?;
let mut curr = self.apply(root, first)?;
for term in rest.iter() {
self.ast = &term.0;
let next = self.eval()?;
curr = self.apply(curr, next)?;
}
self.ast = parent;
Ok(curr)
}
2024-12-11 03:26:52 +00:00
When(clauses) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
for clause in clauses.iter() {
2024-12-11 03:26:52 +00:00
let WhenClause(cond, body) = &clause.0 else {
unreachable!()
};
2024-12-09 04:33:46 +00:00
self.ast = &cond.0;
if self.eval()?.bool() {
self.ast = &body.0;
let result = self.eval();
self.ast = parent;
return result;
};
}
Err(LErr::new(format!("no match")))
}
2024-12-11 03:26:52 +00:00
Match(value, clauses) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &value.0;
let value = self.eval()?;
let result = self.match_clauses(&value, clauses);
self.ast = parent;
result
}
2024-12-11 03:26:52 +00:00
Fn(name, clauses, doc) => {
2024-12-09 04:33:46 +00:00
let doc = doc.map(|s| s.to_string());
2024-12-11 03:26:52 +00:00
let the_fn = Value::Fn::<'src>(Rc::new(crate::value::Fn::<'src> {
2024-12-09 04:33:46 +00:00
name: name.to_string(),
body: clauses,
doc,
}));
self.bind(name.to_string(), &the_fn);
Ok(the_fn)
}
2024-12-11 03:26:52 +00:00
FnDeclaration(_name) => Ok(Value::Nil),
Panic(msg) => {
2024-12-09 04:33:46 +00:00
self.ast = &msg.0;
let msg = self.eval()?;
Err(LErr::new(format!("{msg}")))
}
2024-12-11 03:26:52 +00:00
Repeat(times, body) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &times.0;
let times_num = match self.eval() {
Ok(Value::Number(n)) => n as usize,
_ => return Err(LErr::new(format!("repeat may only take numbers"))),
};
self.ast = &body.0;
for _ in 0..times_num {
self.eval()?;
}
self.ast = parent;
Ok(Value::Nil)
}
2024-12-11 03:26:52 +00:00
Do(terms) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &terms[0].0;
let mut result = self.eval()?;
for (term, _) in terms.iter().skip(1) {
self.ast = term;
let next = self.eval()?;
let arg = Value::Tuple(Rc::new(vec![result]));
result = self.apply(next, arg)?;
}
self.ast = parent;
Ok(result)
}
2024-12-11 03:26:52 +00:00
Loop(init, clauses) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
self.ast = &init.0;
let mut args = self.eval()?;
loop {
let result = self.match_clauses(&args, clauses)?;
if let Value::Recur(recur_args) = result {
args = Value::Tuple(Rc::new(recur_args));
} else {
self.ast = parent;
return Ok(result);
}
}
}
2024-12-11 03:26:52 +00:00
Recur(args) => {
2024-12-09 04:33:46 +00:00
let parent = self.ast;
let mut vect = Vec::new();
for arg in args {
self.ast = &arg.0;
vect.push(self.eval()?);
}
self.ast = parent;
Ok(Value::Recur(vect))
}
2024-12-11 03:26:52 +00:00
_ => unreachable!(),
2024-12-09 04:33:46 +00:00
};
self.ast = root;
result
}
}