diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 03a5bd1..98036bf 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -102,8 +102,12 @@ pub enum Statement { VarDecl(VarDeclStmt), VarAssign(VarAssignStmt), - If(IfStmt), - While(WhileStmt), + If { + initial: StatementBranch, + else_if: Vec, + else_branch: Option>, + }, + While(StatementBranch), For(ForStmt), Return(Option), @@ -131,14 +135,7 @@ pub struct VarAssignStmt { } #[derive(Debug, Clone, Serialize)] -pub struct IfStmt { - pub condition: Expression, - pub then_branch: Box, - pub else_branch: Option>, -} - -#[derive(Debug, Clone, Serialize)] -pub struct WhileStmt { +pub struct StatementBranch { pub condition: Expression, pub body: Box, } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index e2014f4..1019e98 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -308,18 +308,20 @@ impl From> for Statement { let else_branch = inner.next().map(Statement::from); - Statement::If(IfStmt { - condition, - then_branch: Box::new(then_branch), - else_branch: else_branch.map(Box::new), - }) + Statement::Break + + // Statement::If(IfStmt { + // condition, + // then_branch: Box::new(then_branch), + // else_branch: else_branch.map(Box::new), + // }) } Rule::while_stmt => { let condition = Expression::from(inner.next().unwrap()); let body = Statement::from(inner.next().unwrap()); - Statement::While(WhileStmt { + Statement::While(StatementBranch { condition, body: Box::new(body), }) diff --git a/src/codegen.rs b/src/codegen.rs index a900512..d6af6dd 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,7 +1,7 @@ use parser::ast::{ - Attribute, BinaryOp, Block, Expression, IfStmt, Literal, Path, Postfix, Prefix, Statement, - TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, - VarDeclStmt, WhileStmt, + Attribute, BinaryOp, Block, Expression, Literal, Path, Postfix, Prefix, Statement, + StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, + VarDecl, VarDeclStmt, }; // --------------------------------------------------------------------------- @@ -353,13 +353,18 @@ impl ToRust for Statement { cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),)); } - Statement::If(IfStmt { - condition, - then_branch, + Statement::If { + initial, + else_if, else_branch, - }) => { - cg.add_indentedln(&format!("if {}", condition.get_rust())); - then_branch.to_rust(cg); + } => { + cg.add_indentedln(&format!("if {}", initial.condition.get_rust())); + initial.body.to_rust(cg); + + for else_if_branch in else_if { + cg.add_indentedln(&format!("else if {}", else_if_branch.condition.get_rust())); + else_if_branch.body.to_rust(cg); + } if let Some(else_br) = else_branch { cg.add_indentedln("else"); @@ -367,7 +372,7 @@ impl ToRust for Statement { } } - Statement::While(WhileStmt { condition, body }) => { + Statement::While(StatementBranch { condition, body }) => { cg.add_indentedln(&format!("while {} {{", condition.get_rust())); cg.indent += 1; body.to_rust(cg); diff --git a/test/src/main.mist b/test/src/main.mist index fbc6084..fefc53e 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -11,6 +11,12 @@ void main() { if (call.x == 20) { println("= 20"); + } else if (call.x == 10) { + println("= 10"); + } else if (call.x == 5) { + println("= 5"); + } else { + println("= unknown"); } println!("{call:?}");