diff --git a/examples/wip.rs b/examples/wip.rs index b744af2b99a493c30311ce08df35df04451229e6..852c5b27604c6ca2452caa7082d26e328bae4d20 100644 --- a/examples/wip.rs +++ b/examples/wip.rs @@ -3,7 +3,11 @@ fn main() { while true {} - if a {} + if true {}; + + if true { + } else { + }; let mut a = 5; a = 5; @@ -12,5 +16,5 @@ fn main() { } fn b(x: i32) -> i32 { - x + x; } diff --git a/src/ast.rs b/src/ast.rs index a0ca146894a4f379adb8d305d681b782f192eeb0..f57b199461fcbd58a8d4451137e31f0f143e8863 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -214,21 +214,10 @@ impl Stmts { fn ifmt(&self, fmt: &mut Formatter, indent: usize) -> Result<(), Error> { write!(fmt, "{{")?; let len = self.stmts.len(); - for (i, s) in (&self.stmts).into_iter().enumerate() { + for s in &self.stmts { // set tab write!(fmt, "\n{}", " ".repeat(indent + 4))?; s.ifmt(fmt, indent + 4)?; - - if i < len - 1 { - match s { - Stmt::Block(_) | Stmt::If(_, _, _) | Stmt::While(_, _) => (), - _ => write!(fmt, ";")?, - } - } else { - if self.trailing_semi { - write!(fmt, ";")?; - } - } } write!(fmt, "\n{}}}", " ".repeat(indent)) } diff --git a/src/check.rs b/src/check.rs index ddb772f0ac8d96158a2697a1118b47546137b459..84f8d9208fe3b423ab7fe61f12c3ca20d48a7d97 100644 --- a/src/check.rs +++ b/src/check.rs @@ -363,7 +363,7 @@ pub fn check_stmt( } _ => Err("Condition not Boolean".to_string()), }, - Semi => panic!("ICE"), + Semi => Ok(Type::Unit), } } diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index a6ce8a2e32c9539ca68b7a5283cb75ffa72eb7a8..a1d4d6522570484314f2c4ee8c09f255e6dda3e3 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -121,25 +121,30 @@ Type : Type = { } pub Block: Stmts = { - "{" <mut stmts: StmtSeq*> <st: Stmt?> "}" => { - match &st { - Some(st) => stmts.push(st.clone()), - _ =>(), - }; + "{" <Stmt> "}" => + Stmts { + trailing_semi : match <> { + Stmt::Semi => true, + _ => false, + }, + stmts: vec![<>], + }, + "{" <stmts: StmtSeq*> "}" => { + let stmts: Vec<Stmt> = stmts.into_iter().flatten().collect(); Stmts { + trailing_semi: match &stmts.last() { + Some(Stmt::Semi) => true, + _ => false, + }, stmts, - trailing_semi: match st { - Some(_) => false, - _ => true, - } } } } -StmtSeq: Stmt = { - ";" => Stmt::Semi, - <Stmt> ";" => <>, - StmtBlock => <>, +StmtSeq: Vec<Stmt> = { + ";" => vec![Stmt::Semi], + <Stmt> ";" => vec![<>, Stmt::Semi], + StmtBlock => vec![<>], } StmtBlock: Stmt = { @@ -149,7 +154,6 @@ StmtBlock: Stmt = { } Stmt: Stmt = { - // ";" => Stmt::Semi, "let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e), <Expr> "=" <Expr> => Stmt::Assign(<>), <ExprNoBlock> => Stmt::Expr(<>),