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,5 +1,7 @@
use crate::utils::string_from_charset;
use crate::interner::Token;
use super::location::Location;
use super::primitive::Primitive;
use std::fmt::{Debug, Write};
@@ -10,20 +12,16 @@ use std::rc::Rc;
#[derive(PartialEq, Eq, Clone, Copy)]
struct Wrap(bool, bool);
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Expr(pub Clause, pub Rc<Vec<Clause>>);
#[derive(Clone)]
pub struct Expr{
pub value: Clause,
pub location: Location,
}
impl Expr {
fn deep_fmt(&self, f: &mut std::fmt::Formatter<'_>, depth: usize, tr: Wrap) -> std::fmt::Result {
let Expr(val, typ) = self;
if typ.len() > 0 {
val.deep_fmt(f, depth, Wrap(true, true))?;
for typterm in typ.as_ref() {
f.write_char(':')?;
typterm.deep_fmt(f, depth, Wrap(true, true))?;
}
} else {
val.deep_fmt(f, depth, tr)?;
}
let Expr{ value, .. } = self;
value.deep_fmt(f, depth, tr)?;
Ok(())
}
}
@@ -34,14 +32,12 @@ impl Debug for Expr {
}
}
#[derive(PartialEq, Eq, Hash, Clone)]
#[derive(Clone)]
pub enum Clause {
Apply(Rc<Expr>, Rc<Expr>),
Explicit(Rc<Expr>, Rc<Expr>),
Lambda(Rc<Vec<Clause>>, Rc<Expr>),
Auto(Rc<Vec<Clause>>, Rc<Expr>),
Lambda(Rc<Expr>),
Constant(Token<Vec<Token<String>>>),
LambdaArg(usize),
AutoArg(usize),
P(Primitive),
}
@@ -49,15 +45,11 @@ const ARGNAME_CHARSET: &str = "abcdefghijklmnopqrstuvwxyz";
fn parametric_fmt(
f: &mut std::fmt::Formatter<'_>, depth: usize,
prefix: &str, argtyp: &[Clause], body: &Expr, wrap_right: bool
prefix: &str, body: &Expr, wrap_right: bool
) -> std::fmt::Result {
if wrap_right { f.write_char('(')?; }
f.write_str(prefix)?;
f.write_str(&string_from_charset(depth as u64, ARGNAME_CHARSET))?;
for typ in argtyp.iter() {
f.write_str(":")?;
typ.deep_fmt(f, depth, Wrap(false, false))?;
}
f.write_str(".")?;
body.deep_fmt(f, depth + 1, Wrap(false, false))?;
if wrap_right { f.write_char(')')?; }
@@ -69,9 +61,8 @@ impl Clause {
-> std::fmt::Result {
match self {
Self::P(p) => write!(f, "{p:?}"),
Self::Lambda(argtyp, body) => parametric_fmt(f, depth, "\\", argtyp, body, wr),
Self::Auto(argtyp, body) => parametric_fmt(f, depth, "@", argtyp, body, wr),
Self::LambdaArg(skip) | Self::AutoArg(skip) => {
Self::Lambda(body) => parametric_fmt(f, depth, "\\", body, wr),
Self::LambdaArg(skip) => {
let lambda_depth = (depth - skip - 1).try_into().unwrap();
f.write_str(&string_from_charset(lambda_depth, ARGNAME_CHARSET))
},
@@ -83,18 +74,13 @@ impl Clause {
if wl { f.write_char(')')?; }
Ok(())
}
Self::Explicit(gen, t) => {
if wl { f.write_char('(')?; }
gen.deep_fmt(f, depth, Wrap(false, true))?;
f.write_str(" @")?;
t.deep_fmt(f, depth, Wrap(true, wr && !wl))?;
if wl { f.write_char(')'); }
Ok(())
}
Self::Constant(token) => write!(f, "{:?}", token)
}
}
pub fn wrap(self) -> Box<Expr> { Box::new(Expr(self, Rc::new(vec![]))) }
pub fn wrap_t(self, t: Clause) -> Box<Expr> { Box::new(Expr(self, Rc::new(vec![t]))) }
#[allow(unused)]
pub fn wrap(self) -> Box<Expr> {
Box::new(Expr{ value: self, location: Location::Unknown })
}
}
impl Debug for Clause {