Postfix refactor in codegen
This commit is contained in:
+19
-23
@@ -114,26 +114,19 @@ impl GetRust for Expression {
|
||||
Expression::BoolLiteral(b) => b.to_string(),
|
||||
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
|
||||
|
||||
Expression::Fix { initial, postfixes } => {
|
||||
let base = initial.get_rust();
|
||||
postfixes.get_rust_with_base(&base)
|
||||
}
|
||||
Expression::Fix {
|
||||
initial,
|
||||
prefixes,
|
||||
postfixes,
|
||||
} => initial.get_rust() + &postfixes.get_rust(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper — applies a slice of postfixes onto an already-rendered base string.
|
||||
trait PostfixChain {
|
||||
fn get_rust_with_base(&self, base: &str) -> String;
|
||||
}
|
||||
|
||||
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),
|
||||
impl GetRust for Postfix {
|
||||
fn get_rust(&self) -> String {
|
||||
match self {
|
||||
Postfix::FieldAccess(field) => format!(".{}", field),
|
||||
|
||||
Postfix::Call(args) => {
|
||||
let args = args
|
||||
@@ -141,11 +134,11 @@ impl PostfixChain for [Postfix] {
|
||||
.map(|a| a.get_rust())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{}({})", result, args)
|
||||
format!("({})", args)
|
||||
}
|
||||
|
||||
Postfix::MacroCall(inner) => {
|
||||
format!("{}!({})", result, inner)
|
||||
format!("!({})", inner)
|
||||
}
|
||||
|
||||
Postfix::StructCall(fields) => {
|
||||
@@ -154,11 +147,11 @@ impl PostfixChain for [Postfix] {
|
||||
.map(|(k, v)| format!("{}: {}", k, v.get_rust()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{} {{ {} }}", result, fields)
|
||||
format!("{{ {} }}", fields)
|
||||
}
|
||||
|
||||
Postfix::Index(idx) => {
|
||||
format!("{}[{}]", result, idx.get_rust())
|
||||
format!("[{}]", idx.get_rust())
|
||||
}
|
||||
|
||||
Postfix::Binary(op, rhs) => {
|
||||
@@ -175,12 +168,15 @@ impl PostfixChain for [Postfix] {
|
||||
BinaryOp::LessThanOrEqual => "<=",
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user