Postfix refactor in codegen

This commit is contained in:
2026-05-04 20:04:09 +02:00
parent ba24db481c
commit e79b594ab8
+19 -23
View File
@@ -114,26 +114,19 @@ impl GetRust for Expression {
Expression::BoolLiteral(b) => b.to_string(), Expression::BoolLiteral(b) => b.to_string(),
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s), Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
Expression::Fix { initial, postfixes } => { Expression::Fix {
let base = initial.get_rust(); initial,
postfixes.get_rust_with_base(&base) prefixes,
} postfixes,
} => initial.get_rust() + &postfixes.get_rust(),
} }
} }
} }
/// Helper — applies a slice of postfixes onto an already-rendered base string. impl GetRust for Postfix {
trait PostfixChain { fn get_rust(&self) -> String {
fn get_rust_with_base(&self, base: &str) -> String; match self {
} Postfix::FieldAccess(field) => format!(".{}", field),
impl PostfixChain for [Postfix] {
fn get_rust_with_base(&self, base: &str) -> String {
let mut result = base.to_string();
for postfix in self {
result = match postfix {
Postfix::FieldAccess(field) => format!("{}.{}", result, field),
Postfix::Call(args) => { Postfix::Call(args) => {
let args = args let args = args
@@ -141,11 +134,11 @@ impl PostfixChain for [Postfix] {
.map(|a| a.get_rust()) .map(|a| a.get_rust())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
format!("{}({})", result, args) format!("({})", args)
} }
Postfix::MacroCall(inner) => { Postfix::MacroCall(inner) => {
format!("{}!({})", result, inner) format!("!({})", inner)
} }
Postfix::StructCall(fields) => { Postfix::StructCall(fields) => {
@@ -154,11 +147,11 @@ impl PostfixChain for [Postfix] {
.map(|(k, v)| format!("{}: {}", k, v.get_rust())) .map(|(k, v)| format!("{}: {}", k, v.get_rust()))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
format!("{} {{ {} }}", result, fields) format!("{{ {} }}", fields)
} }
Postfix::Index(idx) => { Postfix::Index(idx) => {
format!("{}[{}]", result, idx.get_rust()) format!("[{}]", idx.get_rust())
} }
Postfix::Binary(op, rhs) => { Postfix::Binary(op, rhs) => {
@@ -175,12 +168,15 @@ impl PostfixChain for [Postfix] {
BinaryOp::LessThanOrEqual => "<=", BinaryOp::LessThanOrEqual => "<=",
BinaryOp::GreaterThanOrEqual => ">=", BinaryOp::GreaterThanOrEqual => ">=",
}; };
format!("{} {} {}", result, op_str, rhs.get_rust()) format!("{} {}", op_str, rhs.get_rust())
} }
};
} }
}
}
result impl GetRust for [Postfix] {
fn get_rust(&self) -> String {
self.iter().map(Postfix::get_rust).collect()
} }
} }