This commit is contained in:
2026-05-24 12:29:42 +02:00
parent fde41b9a22
commit 1c2cc94cb9
5 changed files with 56 additions and 15 deletions
+31 -13
View File
@@ -85,7 +85,7 @@ impl GenRust for Expression {
prefixes, prefixes,
postfixes, postfixes,
} => { } => {
cg.add(&prefixes.get_rust()); prefixes.gen_rust(ctx, cg);
initial.gen_rust(ctx, cg); initial.gen_rust(ctx, cg);
cg.add(&Some(prefixes).get_rust()); cg.add(&Some(prefixes).get_rust());
for postfix in postfixes { for postfix in postfixes {
@@ -110,23 +110,41 @@ impl GenRust for Expression {
} }
} }
impl GetRust for Prefix { impl GenRust for Prefix {
fn get_rust(&self) -> String { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Self::Deref => "*", Self::Deref => cg.add("*"),
Self::Ref => "&", Self::Ref => cg.add("&"),
Self::RefMut => "&mut ", Self::RefMut => cg.add("&mut "),
Self::Not => "!", Self::Not => cg.add("!"),
Self::New(_) => "", Self::New(_) => cg.add(""),
Self::Neg => "-", Self::Neg => cg.add("-"),
Self::Closure(ty, args) => {
cg.add("|");
for (i, arg) in args.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
arg.gen_rust(ctx, cg);
}
cg.add("| ");
if let Some(ty) = ty {
cg.add("-> ");
cg.add(&ty.get_rust());
cg.add(" ");
}
}
} }
.to_string()
} }
} }
impl GetRust for Vec<Prefix> { impl GenRust for Vec<Prefix> {
fn get_rust(&self) -> String { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
self.into_iter().map(Prefix::get_rust).collect() for prefix in self {
prefix.gen_rust(ctx, cg);
}
} }
} }
+1
View File
@@ -62,6 +62,7 @@ pub enum Prefix {
New(Option<Generics>), New(Option<Generics>),
Not, Not,
Neg, Neg,
Closure(Option<TypeExpr>, Vec<VarDecl>),
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+8 -1
View File
@@ -436,6 +436,12 @@ match_stmt = {
// EXPRESSIONS // EXPRESSIONS
// ====================================================== // ======================================================
// ------------------------------------------------------
// CLOSURES
// ------------------------------------------------------
closure = { type_expr? ~ "|" ~ param_list ~ "|" }
// ------------------------------------------------------ // ------------------------------------------------------
// TUPLES & ARRAYS // TUPLES & ARRAYS
// ------------------------------------------------------ // ------------------------------------------------------
@@ -549,7 +555,8 @@ not_px = { "!" }
neg_px = { "-" } neg_px = { "-" }
prefix = { prefix = {
deref_px closure
| deref_px
| mut_ref_px | mut_ref_px
| ref_px | ref_px
| new_px | new_px
+11
View File
@@ -3,6 +3,7 @@ use crate::{
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, GetLength, IntoErr, collect_recovered, collect_recovered_map}, error::{AstError, AstResult, GetLength, IntoErr, collect_recovered, collect_recovered_map},
parser::consume_rule,
}; };
use pest::pratt_parser::PrattParser; use pest::pratt_parser::PrattParser;
use std::sync::OnceLock; use std::sync::OnceLock;
@@ -104,6 +105,16 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
), ),
Rule::not_px => Self::Not, Rule::not_px => Self::Not,
Rule::neg_px => Self::Neg, Rule::neg_px => Self::Neg,
Rule::closure => {
let mut inner = pair.into_inner();
return ast_expr!(Self::Closure(
consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose(),
collect_recovered(inner.next().unwrap().into_inner())
));
}
_ => return AstError::bug_unimplemented(pair), _ => return AstError::bug_unimplemented(pair),
}) })
+5 -1
View File
@@ -9,4 +9,8 @@ void main() {
app_instance.boot(); app_instance.boot();
app_instance.run(); app_instance.run();
} }
void do_stuff() {
vec!(1, 2, 3).into_iter().map(Option<i32> |var v| {Some(v)}).collect::<Vec<_>>();
}