Working soft return

This commit is contained in:
2026-05-21 07:59:00 +02:00
parent 70e6eb6276
commit 7688599504
5 changed files with 25 additions and 5 deletions
+4
View File
@@ -9,5 +9,9 @@ void main() {
println!("Yes!"); println!("Yes!");
} }
i32 y = if (x == 3) { 1 } else { 0 };
println!("hello, world {x}"); println!("hello, world {x}");
println!("{y}")
} }
+4 -1
View File
@@ -3,7 +3,10 @@ use serde::Serialize;
use super::*; use super::*;
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct Block(pub Vec<Spanned<Expression>>); pub struct Block(
pub Vec<Spanned<Expression>>,
pub Option<Spanned<Expression>>,
);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Statement { pub enum Statement {
+7 -2
View File
@@ -339,7 +339,11 @@ top_level = {
// ====================================================== // ======================================================
block = { block = {
"{" ~ (statement | unexpected_statement)* ~ "}" "{" ~ statement_list ~ expr? ~ "}"
}
statement_list = {
(statement | (!expr ~ unexpected_statement))*
} }
statement = _{ statement = _{
@@ -472,7 +476,8 @@ expr_path = {
} }
primary = { primary = {
basic_stmt control_flow
| basic_stmt
| literal | literal
| expr_path | expr_path
| tuple | tuple
+6 -2
View File
@@ -10,8 +10,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
type Error = AstError<'a, Self>; type Error = AstError<'a, Self>;
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 mut inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::block => { ast_ensure!(pair, Rule::block => {
ast_expr!(Block(collect_recovered(pair.into_inner()))) ast_expr!(Block(collect_recovered(inner.next().unwrap().into_inner()), inner.next().map(Spanned::try_from).transpose()))
}) })
} }
} }
@@ -39,7 +41,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
match rule { match rule {
Rule::statement | Rule::basic_stmt | Rule::control_flow => Statement::try_from(inner.next().unwrap()), Rule::statement | Rule::basic_stmt | Rule::control_flow => {
Statement::try_from(inner.next().unwrap())
}
Rule::block => ast_expr!(Statement::Block(pair.try_into())), Rule::block => ast_expr!(Statement::Block(pair.try_into())),
+4
View File
@@ -326,6 +326,10 @@ impl ToRust for Block {
for stmt in self.0 { for stmt in self.0 {
cg.add_indentedln(&(stmt.get_rust() + ";")); cg.add_indentedln(&(stmt.get_rust() + ";"));
} }
if let Some(soft_return) = self.1 {
cg.add_indentedln(&soft_return.get_rust());
}
} }
} }