Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Nov 7, 2023
1 parent 1947606 commit a54c404
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
32 changes: 24 additions & 8 deletions compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,30 @@ ExprInStmt: Expr<'input> = {
"loop" "{" <bdy:Spanned<Expr>> "}" => Expr::Loop {
bdy: Box::new(bdy),
},
"while" <cnd:Spanned<ExprLogicalOr<Never>>> "{" <bdy:Spanned<Expr>> "}" => Expr::Loop {
bdy: Box::new(Expr::If {
cnd: Box::new(cnd),
thn: Box::new(bdy),
els: Box::new(Expr::Seq { // todo this desugaring doesnt have a span
stmt: Box::new(Expr::Break { bdy: Box::new(Expr::Lit { val: Lit::Unit }) }),
cnt: Box::new(Expr::Lit { val: Lit::Unit }),
}),
// todo: the spans in this desugaring do not make a lot sense.
<l:@L> "while" <r:@R> <cnd:Spanned<ExprLogicalOr<Never>>> "{" <bdy:Spanned<Expr>> "}" => Expr::Loop {
bdy: Box::new(Spanned {
span: (l, r),
expr: Expr::If {
cnd: Box::new(cnd),
thn: Box::new(bdy),
els: Box::new(Spanned {
span: (l, r),
expr: Expr::Seq {
stmt: Box::new(Spanned {
span: (l, r),
expr: Expr::Break { bdy: Box::new(Spanned {
span: (l, r),
expr: Expr::Lit { val: Lit::Unit },
})},
}),
cnt: Box::new(Spanned {
span: (l, r),
expr: Expr::Lit { val: Lit::Unit },
}),
},
}),
},
}),
},
"switch" <enm:Spanned<ExprLogicalOr<Never>>> "{" <arms:Comma<(<Ident> "(" <Ident> ")" "=>" <Spanned<Expr>> )>> "}" => Expr::Switch {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ pub enum Expr<'p> {

#[derive(Debug, PartialEq, Functor)]
pub struct Spanned<T> {
span: (usize, usize),
expr: T
pub span: (usize, usize),
pub expr: T
}

/// A primitive operation.
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/passes/validate/type_check/validate_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ pub fn validate_expr<'p>(
bdy,
..
} => {
let bnd = validate_expr(*bnd, env)?;
let bnd = validate_expr(bnd.expr, env)?;
let bdy = env.push(
sym,
EnvEntry::Type {
mutable,
typ: bnd.typ().clone(),
},
|env| validate_expr(*bdy, env),
|env| validate_expr(bdy.expr, env),
)?;

TExpr::Let {
Expand All @@ -63,9 +63,9 @@ pub fn validate_expr<'p>(
}
}
Expr::If { cnd, thn, els, .. } => {
let cnd = validate_expr(*cnd, env)?;
let thn = validate_expr(*thn, env)?;
let els = validate_expr(*els, env)?;
let cnd = validate_expr(cnd.expr, env)?;
let thn = validate_expr(thn.expr, env)?;
let els = validate_expr(els.expr, env)?;

expect_type(&cnd, &Type::Bool)?;
expect_type_eq(&thn, &els)?;
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/validate/type_check/validate_prim.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::passes::parse::types::Type;
use crate::passes::parse::{Expr, Op};
use crate::passes::parse::{Expr, Op, Spanned};
use crate::passes::validate::type_check::error::TypeError;
use crate::passes::validate::type_check::validate_expr::validate_expr;
use crate::passes::validate::type_check::{expect_type, expect_type_eq, Env};
Expand All @@ -8,11 +8,11 @@ use crate::passes::validate::TExpr;
pub fn validate_prim<'p>(
env: &mut Env<'_, 'p>,
op: Op,
args: Vec<Expr<'p>>,
args: Vec<Spanned<Expr<'p>>>,
) -> Result<TExpr<'p, &'p str>, TypeError> {
let args = args
.into_iter()
.map(|arg| validate_expr(arg, env))
.map(|arg| validate_expr(arg.ex, env))
.collect::<Result<Vec<_>, _>>()?;

let typ = match &(op, args.as_slice()) {
Expand Down

0 comments on commit a54c404

Please sign in to comment.