diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 7478c7a..5256592 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -56,53 +56,55 @@ pub enum Postfix { #[derive(Debug, Clone, Serialize)] pub enum Statement { - // expr; Expression(Expression), - - // { ... } Block(Block), - // let/const/var x = ... - VarDecl { - mutable: bool, - name: String, - init: Option, - type_: Option, - }, + VarDecl(VarDeclStmt), + VarAssign(VarAssignStmt), + If(IfStmt), + While(WhileStmt), + For(ForStmt), - VarAssign { - target: Expression, - value: Expression, - }, - - // if (...) stmt else stmt - If { - condition: Expression, - then_branch: Box, - else_branch: Option>, - }, - - // while (...) stmt - While { - condition: Expression, - body: Box, - }, - - // for (...) stmt - For { - init: (bool, String, Option), - condition: Option, - update: Option>, - body: Box, - }, - - // return expr?; Return(Option), - Break, Continue, } +#[derive(Debug, Clone, Serialize)] +pub struct VarDeclStmt { + pub mutable: bool, + pub name: String, + pub init: Option, + pub type_: Option, +} + +#[derive(Debug, Clone, Serialize)] +pub struct VarAssignStmt { + pub target: Expression, + pub value: Expression, +} + +#[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 condition: Expression, + pub body: Box, +} + +#[derive(Debug, Clone, Serialize)] +pub struct ForStmt { + pub init: (bool, String, Option), + pub condition: Option, + pub update: Option>, + pub body: Box, +} + #[derive(Debug, Clone, Serialize)] pub enum Expression { Identifier(String), diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 54247b6..3ab17af 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -197,12 +197,12 @@ impl From> for Statement { let name = var_decl.next().unwrap().as_str().to_string(); let init = inner.next().map(Expression::from); - Statement::VarDecl { + Statement::VarDecl(VarDeclStmt { mutable, name: name.as_str().to_string(), init, type_, - } + }) } Rule::return_stmt => { @@ -225,11 +225,11 @@ impl From> for Statement { let else_branch = inner.next().map(Statement::from); - Statement::If { + Statement::If(IfStmt { condition, then_branch: Box::new(then_branch), else_branch: else_branch.map(Box::new), - } + }) } Rule::while_stmt => { @@ -238,10 +238,10 @@ impl From> for Statement { let condition = Expression::from(inner.next().unwrap()); let body = Statement::from(inner.next().unwrap()); - Statement::While { + Statement::While(WhileStmt { condition, body: Box::new(body), - } + }) } _ => unimplemented!( diff --git a/src/codegen.rs b/src/codegen.rs index 04dcfce..7c8330f 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,4 +1,7 @@ -use parser::ast::{BinaryOp, Block, Expression, Postfix, Statement, TopLevel, TypeExpr}; +use parser::ast::{ + BinaryOp, Block, Expression, IfStmt, Postfix, Statement, TopLevel, TypeExpr, VarAssignStmt, + VarDeclStmt, WhileStmt, +}; pub struct RustCodegen { output: String, @@ -128,12 +131,12 @@ impl RustCodegen { self.add_indentedln("}"); } - Statement::VarDecl { + Statement::VarDecl(VarDeclStmt { mutable, name, init, type_, - } => { + }) => { let mutability = if *mutable { "mut " } else { "" }; let ty = type_ @@ -149,7 +152,7 @@ impl RustCodegen { self.add_indentedln(&format!("let {}{}{}{};", mutability, name, ty, init)); } - Statement::VarAssign { target, value } => { + Statement::VarAssign(VarAssignStmt { target, value }) => { self.add_indentedln(&format!( "{} = {};", self.generate_expression(target), @@ -157,11 +160,11 @@ impl RustCodegen { )); } - Statement::If { + Statement::If(IfStmt { condition, then_branch, else_branch, - } => { + }) => { self.add_indentedln(&format!("if {} {{", self.generate_expression(condition))); self.indent += 1; @@ -179,7 +182,7 @@ impl RustCodegen { } } - Statement::While { condition, body } => { + Statement::While(WhileStmt { condition, body }) => { self.add_indentedln(&format!("while {} {{", self.generate_expression(condition))); self.indent += 1;