Improved for loop

This commit is contained in:
2026-05-07 23:43:22 +02:00
parent c7ada72029
commit 8b409f4ccf
4 changed files with 18 additions and 7 deletions
+2 -1
View File
@@ -164,7 +164,8 @@ pub enum Statement {
body: Box<Statement>, body: Box<Statement>,
}, },
For { For {
pattern: Identifier, mutable: bool,
pattern: Pattern,
iterator: Expression, iterator: Expression,
body: Box<Statement>, body: Box<Statement>,
}, },
+1 -1
View File
@@ -142,7 +142,7 @@ c_for_stmt = {
} }
for_stmt = { for_stmt = {
"for" ~ "(" ~ identifier ~ "in" ~ expr ~ ")" ~ statement "for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement
} }
match_item = { pattern ~ "=>" ~ block } match_item = { pattern ~ "=>" ~ block }
+2 -1
View File
@@ -365,7 +365,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
}, },
Rule::for_stmt => Statement::For { Rule::for_stmt => Statement::For {
pattern: Identifier::from(inner.next().unwrap()), mutable: listen_rule(&mut inner, Rule::mutable),
pattern: Pattern::from(inner.next().unwrap()),
iterator: inner.next().unwrap().into(), iterator: inner.next().unwrap().into(),
body: Box::new(Statement::from(inner.next().unwrap())), body: Box::new(Statement::from(inner.next().unwrap())),
}, },
+13 -4
View File
@@ -541,12 +541,14 @@ impl ToRust for Statement {
} }
Statement::For { Statement::For {
mutable,
pattern, pattern,
iterator, iterator,
body, body,
} => { } => {
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"for {} in {}", "for {}{} in {}",
get_mutable(*mutable),
pattern.get_rust(), pattern.get_rust(),
iterator.get_rust() iterator.get_rust()
)); ));
@@ -590,15 +592,18 @@ impl ToRust for FunctionDecl {
impl GetRust for VarDecl { impl GetRust for VarDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
let mutability = if self.mutable { "mut " } else { "" };
let ty = self let ty = self
.type_ .type_
.as_ref() .as_ref()
.map(|t| format!(": {}", t.get_rust())) .map(|t| format!(": {}", t.get_rust()))
.unwrap_or_default(); .unwrap_or_default();
format!("{}{}{}", mutability, self.name.get_rust(), ty) format!(
"{}{}{}",
get_mutable(self.mutable),
self.name.get_rust(),
ty
)
} }
} }
@@ -709,3 +714,7 @@ pub fn get_static_type_path(path: &Path) -> String {
pub fn get_type_postfixes(postfixes: &[TypePostfix]) -> String { pub fn get_type_postfixes(postfixes: &[TypePostfix]) -> String {
postfixes.iter().map(TypePostfix::get_rust).collect() postfixes.iter().map(TypePostfix::get_rust).collect()
} }
pub fn get_mutable(mutable: bool) -> String {
if mutable { "mut " } else { "" }.to_string()
}