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(),
self.rhs.write_string() match self.operator.node {
) BinaryOperator::Multiply => "*",
} BinaryOperator::Divide => "/",
BinaryOperator::Divide => { BinaryOperator::Modulo => "%",
format!( BinaryOperator::Plus => "+",
"({} / {})", BinaryOperator::Minus => "-",
self.lhs.write_string(), BinaryOperator::ShiftLeft => "<<",
self.rhs.write_string() BinaryOperator::ShiftRight => ">>",
) BinaryOperator::Less => "<",
} BinaryOperator::Greater => ">",
BinaryOperator::Modulo => { BinaryOperator::LessOrEqual => "<=",
format!( BinaryOperator::GreaterOrEqual => ">=",
"({} % {})", BinaryOperator::Equals => "==",
self.lhs.write_string(), BinaryOperator::NotEquals => "!=",
self.rhs.write_string() BinaryOperator::BitwiseAnd => "&",
) BinaryOperator::BitwiseXor => "^",
} BinaryOperator::BitwiseOr => "|",
BinaryOperator::Plus => { BinaryOperator::LogicalAnd => "&&",
format!( BinaryOperator::LogicalOr => "||",
"({} + {})", BinaryOperator::Assign => "=",
self.lhs.write_string(), BinaryOperator::AssignMultiply => "*=",
self.rhs.write_string() BinaryOperator::AssignDivide => "/=",
) BinaryOperator::AssignModulo => "%=",
} BinaryOperator::AssignPlus => "+=",
BinaryOperator::Minus => { BinaryOperator::AssignMinus => "-=",
format!( BinaryOperator::AssignShiftLeft => "<<=",
"({} - {})", BinaryOperator::AssignShiftRight => ">>=",
self.lhs.write_string(), BinaryOperator::AssignBitwiseAnd => "&=",
self.rhs.write_string() BinaryOperator::AssignBitwiseXor => "^=",
) BinaryOperator::AssignBitwiseOr => "|=",
} _ => unreachable!(),
BinaryOperator::ShiftLeft => { },
format!( self.rhs.write_string(),
"({} << {})", ),
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()
)
}
} }
} }
} }