From 2e238b1163042186ca9c6c5b10fc0e08e63742ac Mon Sep 17 00:00:00 2001 From: static Date: Tue, 11 Mar 2025 08:24:50 +0000 Subject: [PATCH] HW1 (2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UnaryOperatorExpression에도 괄호를 무조건 치도록 수정 --- src/c/write_c.rs | 261 +++++++++-------------------------------------- 1 file changed, 48 insertions(+), 213 deletions(-) diff --git a/src/c/write_c.rs b/src/c/write_c.rs index 0e118d0..be6025e 100644 --- a/src/c/write_c.rs +++ b/src/c/write_c.rs @@ -660,17 +660,18 @@ impl WriteString for AlignOf { impl WriteString for UnaryOperatorExpression { fn write_string(&self) -> String { + let operand = self.operand.write_string(); match self.operator.node { - UnaryOperator::PostIncrement => format!("{}++", self.operand.write_string()), - UnaryOperator::PostDecrement => format!("{}--", self.operand.write_string()), - UnaryOperator::PreIncrement => format!("++{}", self.operand.write_string()), - UnaryOperator::PreDecrement => format!("--{}", self.operand.write_string()), - UnaryOperator::Address => format!("&{}", self.operand.write_string()), - UnaryOperator::Indirection => format!("*{}", self.operand.write_string()), - UnaryOperator::Plus => format!("+{}", self.operand.write_string()), - UnaryOperator::Minus => format!("-{}", self.operand.write_string()), - UnaryOperator::Complement => format!("~{}", self.operand.write_string()), - UnaryOperator::Negate => format!("!{}", self.operand.write_string()), + UnaryOperator::PostIncrement => format!("({operand}++)"), + UnaryOperator::PostDecrement => format!("({operand}--)"), + UnaryOperator::PreIncrement => format!("(++{operand})"), + UnaryOperator::PreDecrement => format!("(--{operand})"), + UnaryOperator::Address => format!("(&{operand})"), + UnaryOperator::Indirection => format!("(*{operand})"), + UnaryOperator::Plus => format!("(+{operand})"), + UnaryOperator::Minus => format!("(-{operand})"), + UnaryOperator::Complement => format!("(~{operand})"), + UnaryOperator::Negate => format!("(!{operand})"), } } } @@ -691,209 +692,43 @@ impl WriteString for BinaryOperatorExpression { BinaryOperator::Index => { format!("{}[{}]", self.lhs.write_string(), self.rhs.write_string()) } - BinaryOperator::Multiply => { - format!( - "({} * {})", - self.lhs.write_string(), - self.rhs.write_string() - ) - } - BinaryOperator::Divide => { - format!( - "({} / {})", - self.lhs.write_string(), - self.rhs.write_string() - ) - } - BinaryOperator::Modulo => { - format!( - "({} % {})", - self.lhs.write_string(), - self.rhs.write_string() - ) - } - BinaryOperator::Plus => { - format!( - "({} + {})", - self.lhs.write_string(), - self.rhs.write_string() - ) - } - BinaryOperator::Minus => { - format!( - "({} - {})", - self.lhs.write_string(), - self.rhs.write_string() - ) - } - BinaryOperator::ShiftLeft => { - 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() - ) - } + _ => format!( + "({} {} {})", + self.lhs.write_string(), + match self.operator.node { + BinaryOperator::Multiply => "*", + BinaryOperator::Divide => "/", + BinaryOperator::Modulo => "%", + BinaryOperator::Plus => "+", + BinaryOperator::Minus => "-", + BinaryOperator::ShiftLeft => "<<", + BinaryOperator::ShiftRight => ">>", + BinaryOperator::Less => "<", + BinaryOperator::Greater => ">", + BinaryOperator::LessOrEqual => "<=", + BinaryOperator::GreaterOrEqual => ">=", + BinaryOperator::Equals => "==", + BinaryOperator::NotEquals => "!=", + BinaryOperator::BitwiseAnd => "&", + BinaryOperator::BitwiseXor => "^", + BinaryOperator::BitwiseOr => "|", + BinaryOperator::LogicalAnd => "&&", + BinaryOperator::LogicalOr => "||", + BinaryOperator::Assign => "=", + BinaryOperator::AssignMultiply => "*=", + BinaryOperator::AssignDivide => "/=", + BinaryOperator::AssignModulo => "%=", + BinaryOperator::AssignPlus => "+=", + BinaryOperator::AssignMinus => "-=", + BinaryOperator::AssignShiftLeft => "<<=", + BinaryOperator::AssignShiftRight => ">>=", + BinaryOperator::AssignBitwiseAnd => "&=", + BinaryOperator::AssignBitwiseXor => "^=", + BinaryOperator::AssignBitwiseOr => "|=", + _ => unreachable!(), + }, + self.rhs.write_string(), + ), } } }