diff --git a/parser/src/ast.rs b/parser/src/ast.rs index a623e18..6cbbd92 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -106,7 +106,12 @@ pub enum Statement { else_branch: Option>, }, While(StatementBranch), - For(ForStmt), + CStyleFor { + init: Box, + condition: Box, + update: Box, + body: Box, + }, Return(Option), Break, @@ -138,14 +143,6 @@ pub struct StatementBranch { 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 { Literal(Literal), diff --git a/src/codegen.rs b/src/codegen.rs index fec5582..58dbcfb 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -390,8 +390,31 @@ impl ToRust for Statement { cg.ensure_brackets(body); } - Statement::For { .. } => { - cg.add_indentedln("// TODO: transform into iterator-based loop"); + Statement::CStyleFor { + init, + condition, + update, + body, + } => { + cg.add_indentedln("{"); + cg.indent += 1; + + init.to_rust(cg); + + cg.add_indentedln(&format!("while {}", condition.get_rust())); + + cg.add_indentedln("{"); + cg.indent += 1; + + cg.ensure_brackets(body); + + update.to_rust(cg); + + cg.indent -= 1; + cg.add_indentedln("}"); + + cg.indent -= 1; + cg.add_indentedln("}"); } Statement::Return(expr) => { diff --git a/test/src/main.mist b/test/src/main.mist index 37dbc73..bd51952 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,28 +1,3 @@ -#![allow(unused_mut)] - -#[derive(Debug)] -public struct ApiCall { - i32 x, - i32 y, -} - void main() { - ApiCall mut call = ApiCall { x: 10, y: 12 }; - - if (call.x == 20) { - println!("= 20"); - } else if (call.x == 10) { - println!("= 10"); - } else if (call.x == 5) { - println!("= 5"); - } else { - println!("= unknown"); - } - - while (call.x <= 40) { - call.x = call.x - 1; - println!("while"); - } - - println!("{call:?}"); + }