UnaryOperatorExpression에도 괄호를 무조건 치도록 수정
This commit is contained in:
static
2025-03-11 08:24:50 +00:00
parent 873987829f
commit 2e238b1163

View File

@@ -660,17 +660,18 @@ impl WriteString for AlignOf {
impl WriteString for UnaryOperatorExpression { impl WriteString for UnaryOperatorExpression {
fn write_string(&self) -> String { fn write_string(&self) -> String {
let operand = self.operand.write_string();
match self.operator.node { match self.operator.node {
UnaryOperator::PostIncrement => format!("{}++", self.operand.write_string()), UnaryOperator::PostIncrement => format!("({operand}++)"),
UnaryOperator::PostDecrement => format!("{}--", self.operand.write_string()), UnaryOperator::PostDecrement => format!("({operand}--)"),
UnaryOperator::PreIncrement => format!("++{}", self.operand.write_string()), UnaryOperator::PreIncrement => format!("(++{operand})"),
UnaryOperator::PreDecrement => format!("--{}", self.operand.write_string()), UnaryOperator::PreDecrement => format!("(--{operand})"),
UnaryOperator::Address => format!("&{}", self.operand.write_string()), UnaryOperator::Address => format!("(&{operand})"),
UnaryOperator::Indirection => format!("*{}", self.operand.write_string()), UnaryOperator::Indirection => format!("(*{operand})"),
UnaryOperator::Plus => format!("+{}", self.operand.write_string()), UnaryOperator::Plus => format!("(+{operand})"),
UnaryOperator::Minus => format!("-{}", self.operand.write_string()), UnaryOperator::Minus => format!("(-{operand})"),
UnaryOperator::Complement => format!("~{}", self.operand.write_string()), UnaryOperator::Complement => format!("(~{operand})"),
UnaryOperator::Negate => format!("!{}", self.operand.write_string()), UnaryOperator::Negate => format!("(!{operand})"),
} }
} }
} }
@@ -691,209 +692,43 @@ impl WriteString for BinaryOperatorExpression {
BinaryOperator::Index => { BinaryOperator::Index => {
format!("{}[{}]", self.lhs.write_string(), self.rhs.write_string()) format!("{}[{}]", self.lhs.write_string(), self.rhs.write_string())
} }
BinaryOperator::Multiply => { _ => format!(
format!( "({} {} {})",
"({} * {})", self.lhs.write_string(),
self.lhs.write_string(), match self.operator.node {
self.rhs.write_string() BinaryOperator::Multiply => "*",
) BinaryOperator::Divide => "/",
} BinaryOperator::Modulo => "%",
BinaryOperator::Divide => { BinaryOperator::Plus => "+",
format!( BinaryOperator::Minus => "-",
"({} / {})", BinaryOperator::ShiftLeft => "<<",
self.lhs.write_string(), BinaryOperator::ShiftRight => ">>",
self.rhs.write_string() BinaryOperator::Less => "<",
) BinaryOperator::Greater => ">",
} BinaryOperator::LessOrEqual => "<=",
BinaryOperator::Modulo => { BinaryOperator::GreaterOrEqual => ">=",
format!( BinaryOperator::Equals => "==",
"({} % {})", BinaryOperator::NotEquals => "!=",
self.lhs.write_string(), BinaryOperator::BitwiseAnd => "&",
self.rhs.write_string() BinaryOperator::BitwiseXor => "^",
) BinaryOperator::BitwiseOr => "|",
} BinaryOperator::LogicalAnd => "&&",
BinaryOperator::Plus => { BinaryOperator::LogicalOr => "||",
format!( BinaryOperator::Assign => "=",
"({} + {})", BinaryOperator::AssignMultiply => "*=",
self.lhs.write_string(), BinaryOperator::AssignDivide => "/=",
self.rhs.write_string() BinaryOperator::AssignModulo => "%=",
) BinaryOperator::AssignPlus => "+=",
} BinaryOperator::AssignMinus => "-=",
BinaryOperator::Minus => { BinaryOperator::AssignShiftLeft => "<<=",
format!( BinaryOperator::AssignShiftRight => ">>=",
"({} - {})", BinaryOperator::AssignBitwiseAnd => "&=",
self.lhs.write_string(), BinaryOperator::AssignBitwiseXor => "^=",
self.rhs.write_string() BinaryOperator::AssignBitwiseOr => "|=",
) _ => unreachable!(),
} },
BinaryOperator::ShiftLeft => { self.rhs.write_string(),
format!( ),
"({} << {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::ShiftRight => {
format!(
"({} >> {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::Less => {
format!(
"({} < {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::Greater => {
format!(
"({} > {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::LessOrEqual => {
format!(
"({} <= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::GreaterOrEqual => {
format!(
"({} >= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::Equals => {
format!(
"({} == {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::NotEquals => {
format!(
"({} != {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::BitwiseAnd => {
format!(
"({} & {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::BitwiseXor => {
format!(
"({} ^ {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::BitwiseOr => {
format!(
"({} | {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::LogicalAnd => {
format!(
"({} && {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::LogicalOr => {
format!(
"({} || {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::Assign => {
format!(
"({} = {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignMultiply => {
format!(
"({} *= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignDivide => {
format!(
"({} /= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignModulo => {
format!(
"({} %= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignPlus => {
format!(
"({} += {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignMinus => {
format!(
"({} -= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignShiftLeft => {
format!(
"({} <<= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignShiftRight => {
format!(
"({} >>= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignBitwiseAnd => {
format!(
"({} &= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignBitwiseXor => {
format!(
"({} ^= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
BinaryOperator::AssignBitwiseOr => {
format!(
"({} |= {})",
self.lhs.write_string(),
self.rhs.write_string()
)
}
} }
} }
} }