Refactored for loops
This commit is contained in:
+6
-9
@@ -106,7 +106,12 @@ pub enum Statement {
|
||||
else_branch: Option<Box<Statement>>,
|
||||
},
|
||||
While(StatementBranch),
|
||||
For(ForStmt),
|
||||
CStyleFor {
|
||||
init: Box<Statement>,
|
||||
condition: Box<Expression>,
|
||||
update: Box<Statement>,
|
||||
body: Box<Statement>,
|
||||
},
|
||||
|
||||
Return(Option<Expression>),
|
||||
Break,
|
||||
@@ -138,14 +143,6 @@ pub struct StatementBranch {
|
||||
pub body: Box<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ForStmt {
|
||||
pub init: (bool, String, Option<Expression>),
|
||||
pub condition: Option<Expression>,
|
||||
pub update: Option<Box<Statement>>,
|
||||
pub body: Box<Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Expression {
|
||||
Literal(Literal),
|
||||
|
||||
+25
-2
@@ -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) => {
|
||||
|
||||
+1
-26
@@ -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:?}");
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user