bug fixes and performance improvements

This commit is contained in:
2023-05-07 22:35:38 +01:00
parent f3ce910f66
commit a604e40bad
167 changed files with 5965 additions and 4229 deletions

View File

@@ -1,21 +1,20 @@
use std::{rc::Rc, fmt::Display};
use std::{rc::Rc, cell::RefCell};
use crate::utils::Side;
use super::{postmacro, interpreted, path_set::PathSet};
fn collect_paths_expr_rec(expr: &postmacro::Expr, depth: usize) -> Option<PathSet> {
collect_paths_cls_rec(&expr.0, depth)
collect_paths_cls_rec(&expr.value, depth)
}
fn collect_paths_cls_rec(cls: &postmacro::Clause, depth: usize) -> Option<PathSet> {
match cls {
postmacro::Clause::P(_) | postmacro::Clause::Auto(..) | postmacro::Clause::AutoArg(_)
| postmacro::Clause::Explicit(..) => None,
postmacro::Clause::P(_) | postmacro::Clause::Constant(_) => None,
postmacro::Clause::LambdaArg(h) => if *h != depth {None} else {
Some(PathSet{ next: None, steps: Rc::new(vec![]) })
}
postmacro::Clause::Lambda(_, b) => collect_paths_expr_rec(b, depth + 1),
postmacro::Clause::Lambda(b) => collect_paths_expr_rec(b, depth + 1),
postmacro::Clause::Apply(f, x) => {
let f_opt = collect_paths_expr_rec(f, depth);
let x_opt = collect_paths_expr_rec(x, depth);
@@ -29,46 +28,26 @@ fn collect_paths_cls_rec(cls: &postmacro::Clause, depth: usize) -> Option<PathSe
}
}
#[derive(Clone)]
pub enum Error {
/// Auto, Explicit and AutoArg are unsupported
GenericMention,
/// Type annotations are unsupported
ExplicitType
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ExplicitType => write!(f, "Type annotations are unsupported in the interpreter"),
Self::GenericMention
=> write!(f, "The interpreter is typeless and therefore can't resolve generics")
}
}
}
pub fn clause_rec(cls: &postmacro::Clause) -> Result<interpreted::Clause, Error> {
pub fn clause(cls: &postmacro::Clause) -> interpreted::Clause {
match cls {
postmacro::Clause::P(p) => Ok(interpreted::Clause::P(p.clone())),
postmacro::Clause::Explicit(..) | postmacro::Clause::AutoArg(..) | postmacro::Clause::Auto(..)
=> Err(Error::GenericMention),
postmacro::Clause::Apply(f, x) => Ok(interpreted::Clause::Apply {
f: Rc::new(expr_rec(f.as_ref())?),
x: Rc::new(expr_rec(x.as_ref())?),
id: 0
}),
postmacro::Clause::Lambda(typ, body) => if typ.len() != 0 {Err(Error::ExplicitType)} else {
Ok(interpreted::Clause::Lambda {
args: collect_paths_expr_rec(body, 0),
body: Rc::new(expr_rec(body)?)
})
postmacro::Clause::Constant(name)
=> interpreted::Clause::Constant(*name),
postmacro::Clause::P(p) => interpreted::Clause::P(p.clone()),
postmacro::Clause::Apply(f, x) => interpreted::Clause::Apply {
f: expr(f.as_ref()),
x: expr(x.as_ref()),
},
postmacro::Clause::LambdaArg(_) => Ok(interpreted::Clause::LambdaArg)
postmacro::Clause::Lambda(body) => interpreted::Clause::Lambda {
args: collect_paths_expr_rec(body, 0),
body: expr(body)
},
postmacro::Clause::LambdaArg(_) => interpreted::Clause::LambdaArg
}
}
pub fn expr_rec(expr: &postmacro::Expr) -> Result<interpreted::Clause, Error> {
let postmacro::Expr(c, t) = expr;
if t.len() != 0 {Err(Error::ExplicitType)}
else {clause_rec(c)}
pub fn expr(expr: &postmacro::Expr) -> interpreted::ExprInst {
interpreted::ExprInst(Rc::new(RefCell::new(interpreted::Expr{
location: expr.location.clone(),
clause: clause(&expr.value),
})))
}