Skip to content

Commit

Permalink
Clippy pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBrouwer committed Nov 25, 2023
1 parent 3bb3298 commit b21a147
Show file tree
Hide file tree
Showing 33 changed files with 197 additions and 184 deletions.
10 changes: 5 additions & 5 deletions compiler/src/passes/atomize/atomize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::passes::atomize::{AExpr, Atom, DefAtomized, PrgAtomized};
use crate::passes::parse::types::Type;
use crate::passes::parse::Meta;
use crate::passes::parse::{Meta, Typed};
use crate::passes::reveal::{DefRevealed, PrgRevealed, RExpr};
use crate::utils::gen_sym::{gen_sym, UniqueSym};

Expand Down Expand Up @@ -37,8 +37,8 @@ fn atomize_def(def: DefRevealed) -> DefAtomized {
}

fn atomize_expr<'p>(
expr: Meta<Type<UniqueSym<'p>>, RExpr<'p>>,
) -> Meta<Type<UniqueSym<'p>>, AExpr<'p>> {
expr: Typed<'p, RExpr<'p>>,
) -> Typed<'p, AExpr<'p>> {
// Keep track of all the priors. These are bindings that should come before evaluating the expression.
let mut priors = Vec::new();

Expand Down Expand Up @@ -142,8 +142,8 @@ fn atomize_expr<'p>(
}

fn atomize_atom<'p>(
expr: Meta<Type<UniqueSym<'p>>, RExpr<'p>>,
priors: &mut Vec<(UniqueSym<'p>, Meta<Type<UniqueSym<'p>>, AExpr<'p>>)>,
expr: Typed<'p, RExpr<'p>>,
priors: &mut Vec<(UniqueSym<'p>, Typed<'p, AExpr<'p>>)>,
) -> Atom<'p> {
if let RExpr::Lit { val } = expr.inner {
Atom::Val { val }
Expand Down
34 changes: 17 additions & 17 deletions compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod atomize;
mod tests;

use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Def, Meta, UnaryOp};
use crate::passes::parse::{BinaryOp, Def, Meta, Typed, UnaryOp};
use crate::passes::select::std_lib::Std;
use crate::passes::validate::{DefValidated, ExprValidated, PrgValidated, TLit};
use crate::utils::gen_sym::UniqueSym;
Expand All @@ -15,7 +15,7 @@ pub struct PrgAtomized<'p> {
pub std: Std<'p>,
}

pub type DefAtomized<'p> = Def<UniqueSym<'p>, &'p str, Meta<Type<UniqueSym<'p>>, AExpr<'p>>>;
pub type DefAtomized<'p> = Def<UniqueSym<'p>, &'p str, Typed<'p, AExpr<'p>>>;

pub enum AExpr<'p> {
Atom {
Expand All @@ -31,13 +31,13 @@ pub enum AExpr<'p> {
},
Let {
sym: UniqueSym<'p>,
bnd: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bdy: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bnd: Box<Typed<'p, AExpr<'p>>>,
bdy: Box<Typed<'p, AExpr<'p>>>,
},
If {
cnd: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
thn: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
els: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
cnd: Box<Typed<'p, AExpr<'p>>>,
thn: Box<Typed<'p, AExpr<'p>>>,
els: Box<Typed<'p, AExpr<'p>>>,
},
Apply {
fun: Atom<'p>,
Expand All @@ -47,22 +47,22 @@ pub enum AExpr<'p> {
sym: UniqueSym<'p>,
},
Loop {
bdy: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bdy: Box<Typed<'p, AExpr<'p>>>,
},
Break {
bdy: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bdy: Box<Typed<'p, AExpr<'p>>>,
},
Continue,
Seq {
stmt: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
cnt: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
stmt: Box<Typed<'p, AExpr<'p>>>,
cnt: Box<Typed<'p, AExpr<'p>>>,
},
Assign {
sym: UniqueSym<'p>,
bnd: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bnd: Box<Typed<'p, AExpr<'p>>>,
},
Return {
bdy: Box<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>,
bdy: Box<Typed<'p, AExpr<'p>>>,
},
Struct {
sym: UniqueSym<'p>,
Expand Down Expand Up @@ -123,10 +123,10 @@ impl<'p> From<DefAtomized<'p>> for DefValidated<'p> {
}
}

impl<'p> From<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>
for Meta<Type<UniqueSym<'p>>, ExprValidated<'p>>
impl<'p> From<Typed<'p, AExpr<'p>>>
for Typed<'p, ExprValidated<'p>>
{
fn from(value: Meta<Type<UniqueSym<'p>>, AExpr<'p>>) -> Self {
fn from(value: Typed<'p, AExpr<'p>>) -> Self {
let inner = match value.inner {
AExpr::Atom { atm, .. } => return atm.into(),
AExpr::BinaryOp { op, exprs } => ExprValidated::BinaryOp {
Expand Down Expand Up @@ -193,7 +193,7 @@ impl<'p> From<Meta<Type<UniqueSym<'p>>, AExpr<'p>>>
}

// Note that casting to Never here is safe because this `From` is only used by the interpreter which doesn't care about the type information.
impl<'p> From<Atom<'p>> for Meta<Type<UniqueSym<'p>>, ExprValidated<'p>> {
impl<'p> From<Atom<'p>> for Typed<'p, ExprValidated<'p>> {
fn from(value: Atom<'p>) -> Self {
Meta {
meta: Type::Never,
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/eliminate/eliminate_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::passes::eliminate::eliminate_params::flatten_type;
use crate::passes::eliminate::{EExpr, ETail};
use crate::passes::explicate::CExpr;
use crate::passes::parse::types::Type;
use crate::passes::parse::{Meta, TypeDef};
use crate::passes::parse::{Meta, Typed, TypeDef};
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;

pub fn eliminate_seq<'p>(
sym: UniqueSym<'p>,
ctx: &mut Ctx<'p>,
bnd: Meta<Type<UniqueSym<'p>>, CExpr<'p>>,
bnd: Typed<'p, CExpr<'p>>,
tail: ETail<'p>,
defs: &HashMap<UniqueSym<'p>, TypeDef<UniqueSym<'p>, &'p str>>,
) -> ETail<'p> {
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/explicate/explicate_assign.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::passes::atomize::{AExpr, Atom};
use crate::passes::explicate::explicate::Env;
use crate::passes::explicate::{explicate_pred, CExpr, CTail};
use crate::passes::parse::types::Type;
use crate::passes::parse::Meta;

use crate::passes::parse::{Meta, Typed};
use crate::passes::validate::TLit;
use crate::utils::gen_sym::{gen_sym, UniqueSym};

pub fn explicate_assign<'p>(
sym: UniqueSym<'p>,
bnd: Meta<Type<UniqueSym<'p>>, AExpr<'p>>,
bnd: Typed<'p, AExpr<'p>>,
tail: CTail<'p>,
env: &mut Env<'_, 'p>,
) -> CTail<'p> {
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/passes/explicate/explicate_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::passes::atomize::{AExpr, Atom};
use crate::passes::explicate::explicate::Env;
use crate::passes::explicate::explicate_assign::explicate_assign;
use crate::passes::explicate::CTail;
use crate::passes::parse::types::Type;
use crate::passes::parse::Meta;
use crate::utils::gen_sym::{gen_sym, UniqueSym};

use crate::passes::parse::{Meta, Typed};
use crate::utils::gen_sym::{gen_sym};

pub fn explicate_tail<'p>(
expr: Meta<Type<UniqueSym<'p>>, AExpr<'p>>,
expr: Typed<'p, AExpr<'p>>,
env: &mut Env<'_, 'p>,
) -> CTail<'p> {
let tmp = gen_sym("return");
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/explicate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod tests;

use crate::passes::atomize::Atom;
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Meta, Param, TypeDef, UnaryOp};
use crate::passes::parse::{BinaryOp, Param, Typed, TypeDef, UnaryOp};
use crate::passes::select::std_lib::Std;
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;
Expand All @@ -24,11 +24,11 @@ pub struct PrgExplicated<'p> {

pub enum CTail<'p> {
Return {
expr: Meta<Type<UniqueSym<'p>>, Atom<'p>>,
expr: Typed<'p, Atom<'p>>,
},
Seq {
sym: UniqueSym<'p>,
bnd: Meta<Type<UniqueSym<'p>>, CExpr<'p>>,
bnd: Typed<'p, CExpr<'p>>,
tail: Box<CTail<'p>>,
},
IfStmt {
Expand Down
33 changes: 17 additions & 16 deletions compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::passes::parse::{
BinaryOp, Def, Expr, Lit, Meta, Param, PrgParsed, Span, Type, TypeDef, UnaryOp,
};
use crate::passes::validate::generate_constraints::PartialType;
use crate::passes::validate::partial_type::PartialType;
use functor_derive::Functor;
use crate::passes::parse::Spanned;

grammar;

Expand Down Expand Up @@ -92,7 +93,7 @@ pub Program: PrgParsed<'input> = {
}
}

Def: Def<Meta<Span, &'input str>, Meta<Span, &'input str>, Meta<Span, Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span>>> = {
Def: Def<Spanned<&'input str>, Spanned<&'input str>, Spanned<Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span>>> = {
"struct" <sym:Ident> "{" <fields:Comma<(<Ident> ":" <Type>)>> "}" => Def::TypeDef {
sym,
def: TypeDef::Struct { fields },
Expand All @@ -109,15 +110,15 @@ Def: Def<Meta<Span, &'input str>, Meta<Span, &'input str>, Meta<Span, Expr<Meta<
},
}

Param: Param<Meta<Span, &'input str>> = {
Param: Param<Spanned<&'input str>> = {
<mutable:"mut"?> <sym:Ident> ":" <typ:Type> => Param {
mutable: mutable.is_some(),
sym,
typ,
}
}

Type: Type<Meta<Span, &'input str>> = {
Type: Type<Spanned<&'input str>> = {
"I64" => Type::I64,
"U64" => Type::U64,
"Bool" => Type::Bool,
Expand All @@ -142,7 +143,7 @@ Type: Type<Meta<Span, &'input str>> = {
// Num/Bool/Ident
Expr = ExprStmt;

ExprStmt: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprStmt: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
"let" <mutable:"mut"?> <sym:Ident> <typ:(":" <Type>)?> "=" <bnd:Spanned<ExprLogicalOr<Struct>>> ";" <bdy:Spanned<ExprStmt?>> => Expr::Let {
sym,
mutable: mutable.is_some(),
Expand All @@ -157,7 +158,7 @@ ExprStmt: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Sp
ExprInStmt,
}

ExprInStmt: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprInStmt: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<sym:Ident> "=" <bnd:Spanned<ExprLogicalOr<Struct>>> => Expr::Assign {
sym,
bnd: Box::new(bnd),
Expand Down Expand Up @@ -210,7 +211,7 @@ ExprInStmt: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>,
ExprLogicalOr<Struct>,
}

BinaryOps<Op,Next>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
BinaryOps<Op,Next>: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<e1:Spanned<BinaryOps<Op,Next>>> <op:Op> <e2:Spanned<Next>> => Expr::BinaryOp {
op,
exprs: [Box::new(e1), Box::new(e2)],
Expand Down Expand Up @@ -250,31 +251,31 @@ UnaryOp: UnaryOp = {
"!" => UnaryOp::Not,
}

ExprUnary<T>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprUnary<T>: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<op:UnaryOp> <e:Spanned<ExprUnary<T>>> => Expr::UnaryOp {
op,
expr: Box::new(e),
},
ExprAccess<T>,
}

ExprAccess<T>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprAccess<T>: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<strct:Spanned<ExprAccess<T>>> "." <field:Ident> => Expr::AccessField {
strct: Box::new(strct),
field,
},
ExprCall<T>,
}

ExprCall<T>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprCall<T>: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<fun:Spanned<ExprAtom<T>>> "(" <args:Comma<Spanned<Expr>>> ")" => Expr::Apply {
fun: Box::new(fun),
args,
},
ExprAtom<T>,
}

ExprAtom<T>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
ExprAtom<T>: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<val:integer> => Expr::Lit {
val: Lit::Int {
val,
Expand Down Expand Up @@ -305,21 +306,21 @@ ExprAtom<T>: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>,
<T>,
}

Struct: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {
Struct: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {
<sym:Ident> "{" <fields:Comma<StructArg>> "}" => Expr::Struct {
sym,
fields,
},
}

StructArg : (Meta<Span, &'input str>, Meta<Span, Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span>>) = {
StructArg : (Spanned<&'input str>, Spanned<Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span>>) = {
<Ident> ":" <Spanned<Expr>>,
<l:@L> <sym:Ident> <r:@R> => (sym.clone(), Meta { meta: (l, r - l), inner: Expr::Var { sym } })
}

Never: Expr<Meta<Span, &'input str>, Meta<Span, &'input str>, Lit<'input>, Span> = {};
Never: Expr<Spanned<&'input str>, Spanned<&'input str>, Lit<'input>, Span> = {};

Ident: Meta<Span, &'input str> = Spanned<identifier>;
Ident: Spanned<&'input str> = Spanned<identifier>;

Bool: bool = {
"true" => true,
Expand All @@ -337,4 +338,4 @@ Comma<T>: Vec<T> = {
}
}

Spanned<T>: Meta<Span, T> = <l:@L> <inner:T> <r:@R> => Meta { meta: (l, r - l), inner };
Spanned<T>: Spanned<T> = <l:@L> <inner:T> <r:@R> => Meta { meta: (l, r - l), inner };
8 changes: 6 additions & 2 deletions compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use functor_derive::Functor;
use itertools::Itertools;
use std::fmt::Display;
use types::Type;
use crate::utils::gen_sym::UniqueSym;

/// A parsed program with global definitions and an entry point.
#[derive(Display)]
Expand Down Expand Up @@ -47,8 +48,8 @@ pub enum Def<IdentVars, IdentFields, Expr> {
},
}

pub type DefParsed<'p> = Def<Meta<Span, &'p str>, Meta<Span, &'p str>, Meta<Span, ExprParsed<'p>>>;
pub type ExprParsed<'p> = Expr<Meta<Span, &'p str>, Meta<Span, &'p str>, Lit<'p>, Span>;
pub type DefParsed<'p> = Def<Spanned<&'p str>, Spanned<&'p str>, Spanned<ExprParsed<'p>>>;
pub type ExprParsed<'p> = Expr<Spanned<&'p str>, Spanned<&'p str>, Lit<'p>, Span>;

#[derive(Clone, Debug)]
pub enum TypeDef<IdentVars, IdentFields> {
Expand Down Expand Up @@ -249,6 +250,9 @@ pub struct Meta<M, B> {
pub inner: B,
}

pub type Spanned<T> = Meta<Span, T>;
pub type Typed<'p, T> = Meta<Type<UniqueSym<'p>>, T>;

impl<M, B> Functor<B> for Meta<M, B> {
type Target<T> = Meta<M, T>;

Expand Down
Loading

0 comments on commit b21a147

Please sign in to comment.