Soft returning refactor

This commit is contained in:
2026-05-24 13:20:08 +02:00
parent f4fe38140a
commit 63cc20107c
8 changed files with 61 additions and 21 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ impl GenRust for Expression {
if ensure_semicolon { if ensure_semicolon {
ctx.expr_ensure_semicolon = true; ctx.expr_ensure_semicolon = true;
if !self.is_block() { if !self.is_soft_return() {
cg.add(";"); cg.add(";");
} }
} }
+7
View File
@@ -94,6 +94,13 @@ impl RustCodegen {
} }
} }
} }
pub fn ensure_brackets_body(&mut self, ctx: &mut Context, expr: &StatementBody) {
match expr {
StatementBody::Statement(expr) => self.ensure_brackets_expr(ctx, expr),
StatementBody::Expression(expr) => self.ensure_brackets_expr(ctx, expr),
}
}
} }
impl GenRust for Attribute { impl GenRust for Attribute {
+18 -6
View File
@@ -26,6 +26,18 @@ impl GenRust for Block {
} }
} }
impl GenRust for StatementBody {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self {
Self::Expression(expr) => expr.gen_rust(ctx, cg),
Self::Statement(stmt) => {
stmt.gen_rust(ctx, cg);
cg.add(";");
}
}
}
}
impl GenRust for Statement { impl GenRust for Statement {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
@@ -76,18 +88,18 @@ impl GenRust for Statement {
cg.add("if "); cg.add("if ");
initial.condition.gen_rust(ctx, cg); initial.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets_expr(ctx, &initial.body); cg.ensure_brackets_body(ctx, &initial.body);
for else_if_branch in else_if { for else_if_branch in else_if {
cg.add("else if"); cg.add("else if");
else_if_branch.condition.gen_rust(ctx, cg); else_if_branch.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets_expr(ctx, &else_if_branch.body); cg.ensure_brackets_body(ctx, &else_if_branch.body);
} }
if let Some(else_br) = else_branch { if let Some(else_br) = else_branch {
cg.add(" else "); cg.add(" else ");
cg.ensure_brackets_expr(ctx, else_br); cg.ensure_brackets_body(ctx, else_br);
} }
} }
@@ -95,12 +107,12 @@ impl GenRust for Statement {
cg.add("while "); cg.add("while ");
condition.gen_rust(ctx, cg); condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets_expr(ctx, body); cg.ensure_brackets_body(ctx, body);
} }
Statement::Loop(body) => { Statement::Loop(body) => {
cg.add("loop "); cg.add("loop ");
cg.ensure_brackets_expr(ctx, body); cg.ensure_brackets_body(ctx, body);
} }
Statement::CStyleFor { Statement::CStyleFor {
@@ -156,7 +168,7 @@ impl GenRust for Statement {
pattern.gen_rust(ctx, cg); pattern.gen_rust(ctx, cg);
cg.add(" in "); cg.add(" in ");
iterator.gen_rust(ctx, cg); iterator.gen_rust(ctx, cg);
cg.ensure_brackets_expr(ctx, body); cg.ensure_brackets_body(ctx, body);
} }
Statement::Return(expr) => { Statement::Return(expr) => {
+2 -2
View File
@@ -84,9 +84,9 @@ pub enum Generic {
} }
impl Expression { impl Expression {
pub fn is_block(&self) -> bool { pub fn is_soft_return(&self) -> bool {
if let Expression::Statement(stmt) = self { if let Expression::Statement(stmt) = self {
stmt.is_block() stmt.is_soft_return()
} else { } else {
false false
} }
+10 -10
View File
@@ -10,7 +10,7 @@ pub struct Block(
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum StatementBody { pub enum StatementBody {
Statement(Box<Statement>), Statement(Expression),
Expression(Expression), Expression(Expression),
} }
@@ -64,12 +64,12 @@ pub struct StatementBranch {
} }
impl Statement { impl Statement {
pub fn is_block(&self) -> bool { pub fn is_soft_return(&self) -> bool {
match self { match self {
Self::Block(_) | Self::Match(_, _) => true, Self::Block(_) | Self::Match(_, _) => true,
Self::While(branch) => branch.body.is_block(), Self::While(branch) => branch.body.is_soft_return(),
Self::For { body, .. } | Self::Loop(body) | Self::CStyleFor { body, .. } => { Self::For { body, .. } | Self::Loop(body) | Self::CStyleFor { body, .. } => {
body.is_block() body.is_soft_return()
} }
Self::If { Self::If {
initial, initial,
@@ -78,13 +78,13 @@ impl Statement {
} => { } => {
else_branch else_branch
.as_ref() .as_ref()
.map(|v| v.is_block()) .map(|v| v.is_soft_return())
.unwrap_or_default() .unwrap_or_default()
|| else_if || else_if
.last() .last()
.map(|b| b.body.is_block()) .map(|b| b.body.is_soft_return())
.unwrap_or_default() .unwrap_or_default()
|| initial.body.is_block() || initial.body.is_soft_return()
} }
_ => false, _ => false,
} }
@@ -92,10 +92,10 @@ impl Statement {
} }
impl StatementBody { impl StatementBody {
pub fn is_block(&self) -> bool { pub fn is_soft_return(&self) -> bool {
match self { match self {
Self::Expression(_) => false, Self::Expression(_) => true,
_ => true, _ => false,
} }
} }
} }
+10 -1
View File
@@ -13,7 +13,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule(); let rule = pair.as_rule();
let inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
match rule { match rule {
Rule::expr => { Rule::expr => {
@@ -79,6 +79,15 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())), Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())), Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())), Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::statement_wrapper => {
let i = inner.next().unwrap();
match i.as_rule() {
Rule::expr => i.try_into(),
_ => ast_expr!(Expression::Statement(
i.try_into().get_map(Box::new).map(Box::new)
)),
}
}
Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!( Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!(
Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new)) Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
), ),
+1 -1
View File
@@ -29,7 +29,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBody {
match i.as_rule() { match i.as_rule() {
Rule::expr => ast_expr!(StatementBody::Expression(i.try_into())), Rule::expr => ast_expr!(StatementBody::Expression(i.try_into())),
Rule::statement_wrapper => ast_expr!(StatementBody::Statement(i.try_into().map(Box::new).get_map(Box::new))), Rule::statement_wrapper => ast_expr!(StatementBody::Statement(i.try_into())),
_ => AstError::bug_unimplemented(i), _ => AstError::bug_unimplemented(i),
} }
}) })
+12
View File
@@ -13,4 +13,16 @@ void main() {
void do_stuff() { void do_stuff() {
vec!(1, 2, 3).into_iter().map(Option<i32> |var v| {Some(v)}).collect::<Vec<_>>(); vec!(1, 2, 3).into_iter().map(Option<i32> |var v| {Some(v)}).collect::<Vec<_>>();
if (43=69) testing1();
}
i32 testing1() {
i32 v = if (0 == 0) {
0
} else {
1
};
v
} }