mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-18 00:18:47 +00:00
HW1 (3)
코드 정리
This commit is contained in:
@@ -50,7 +50,7 @@ impl WriteString for Declaration {
|
|||||||
format!(
|
format!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
self.specifiers.write_string(),
|
self.specifiers.write_string(),
|
||||||
self.declarators.write_string()
|
self.declarators.iter().map(|d| d.write_string()).join(", ")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,12 +174,6 @@ impl WriteString for FunctionSpecifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WriteString for Vec<Node<InitDeclarator>> {
|
|
||||||
fn write_string(&self) -> String {
|
|
||||||
self.iter().map(|d| d.write_string()).join(", ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WriteString for InitDeclarator {
|
impl WriteString for InitDeclarator {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
@@ -196,51 +190,42 @@ impl WriteString for InitDeclarator {
|
|||||||
impl WriteString for Declarator {
|
impl WriteString for Declarator {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
match &self.kind.node {
|
match &self.kind.node {
|
||||||
DeclaratorKind::Abstract => {
|
DeclaratorKind::Abstract => self.derived.iter().map(|d| d.write_string()).join(" "),
|
||||||
// println!("DEBUG: ABSTRACT {}", self.derived.write_string());
|
|
||||||
self.derived.write_string()
|
|
||||||
}
|
|
||||||
DeclaratorKind::Identifier(id) => {
|
DeclaratorKind::Identifier(id) => {
|
||||||
// println!(
|
|
||||||
// "DEBUG: IDENTIFIER({}) {}",
|
|
||||||
// id.write_string(),
|
|
||||||
// self.derived.write_string()
|
|
||||||
// );
|
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let mut id_used = false;
|
let mut id_used = false;
|
||||||
for declarator in &self.derived {
|
for declarator in &self.derived {
|
||||||
if let DerivedDeclarator::Pointer(_) = declarator.node {
|
if let DerivedDeclarator::Pointer(_) = declarator.node {
|
||||||
result += format!(" {}", declarator.write_string()).as_str();
|
result += &format!(" {}", declarator.write_string());
|
||||||
} else {
|
} else {
|
||||||
if !id_used {
|
if !id_used {
|
||||||
id_used = true;
|
id_used = true;
|
||||||
result += format!(" {}", id.write_string()).as_str();
|
result += &id.write_string();
|
||||||
}
|
}
|
||||||
result += format!(" {}", declarator.write_string()).as_str();
|
result += &format!(" {}", declarator.write_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !id_used {
|
if !id_used {
|
||||||
result += format!(" {}", id.write_string()).as_str();
|
result += &id.write_string();
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
DeclaratorKind::Declarator(decl) => {
|
DeclaratorKind::Declarator(decl) => {
|
||||||
// println!("DEBUG: DECLARATOR {}", self.derived.write_string());
|
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
let mut id_used = false;
|
let mut decl_used = false;
|
||||||
for declarator in &self.derived {
|
for declarator in &self.derived {
|
||||||
if let DerivedDeclarator::Pointer(_) = declarator.node {
|
if let DerivedDeclarator::Pointer(_) = declarator.node {
|
||||||
result += format!(" {}", declarator.write_string()).as_str();
|
result += &format!(" {}", declarator.write_string());
|
||||||
} else {
|
} else {
|
||||||
if !id_used {
|
if !decl_used {
|
||||||
id_used = true;
|
decl_used = true;
|
||||||
result += format!("({})", decl.write_string()).as_str();
|
result += &format!("({})", decl.write_string());
|
||||||
}
|
}
|
||||||
result += format!(" {}", declarator.write_string()).as_str();
|
result += &format!(" {}", declarator.write_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !id_used {
|
if !decl_used {
|
||||||
result += format!("({})", decl.write_string()).as_str();
|
result += &format!("({})", decl.write_string());
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
@@ -248,12 +233,6 @@ impl WriteString for Declarator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WriteString for Vec<Node<DerivedDeclarator>> {
|
|
||||||
fn write_string(&self) -> String {
|
|
||||||
self.iter().map(|d| d.write_string()).join(" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WriteString for DerivedDeclarator {
|
impl WriteString for DerivedDeclarator {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
@@ -298,15 +277,15 @@ impl WriteString for ArrayDeclarator {
|
|||||||
|
|
||||||
impl WriteString for FunctionDeclarator {
|
impl WriteString for FunctionDeclarator {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
let ellipsis = if let Ellipsis::Some = self.ellipsis {
|
self.parameters
|
||||||
String::from(", ...")
|
.iter()
|
||||||
|
.map(|p| p.write_string())
|
||||||
|
.chain(if let Ellipsis::Some = self.ellipsis {
|
||||||
|
Some("...".to_string())
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
None
|
||||||
};
|
})
|
||||||
format!(
|
.join(", ")
|
||||||
"{}{ellipsis}",
|
|
||||||
self.parameters.iter().map(|p| p.write_string()).join(", ")
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,15 +325,13 @@ impl WriteLine for Statement {
|
|||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "{{");
|
writeln!(write, "{{");
|
||||||
blks.iter()
|
blks.iter()
|
||||||
.try_for_each(|b| b.write_line(indent + 1, write))?;
|
.try_for_each(|b| b.write_line(indent + 1, write));
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "}}");
|
writeln!(write, "}}")
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
Self::Expression(expr) => {
|
Self::Expression(expr) => {
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "{};", expr.write_string());
|
writeln!(write, "{};", expr.write_string())
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
Self::If(if_stat) => if_stat.write_line(indent, write),
|
Self::If(if_stat) => if_stat.write_line(indent, write),
|
||||||
Self::Switch(switch_stat) => switch_stat.write_line(indent, write),
|
Self::Switch(switch_stat) => switch_stat.write_line(indent, write),
|
||||||
@@ -377,12 +354,7 @@ impl WriteLine for Statement {
|
|||||||
expr.as_ref().map_or(String::from(""), |e| e.write_string())
|
expr.as_ref().map_or(String::from(""), |e| e.write_string())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Self::Asm(_) => panic!("Extension"),
|
_ => todo!(),
|
||||||
_ => {
|
|
||||||
write_indent(indent, write);
|
|
||||||
writeln!(write, "TODO");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,8 +362,8 @@ impl WriteLine for Statement {
|
|||||||
impl WriteLine for LabeledStatement {
|
impl WriteLine for LabeledStatement {
|
||||||
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "{}\n", self.label.write_string());
|
writeln!(write, "{}", self.label.write_string());
|
||||||
self.statement.write_line(indent + 1, write)
|
self.statement.write_line(indent, write)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -420,11 +392,11 @@ impl WriteLine for IfStatement {
|
|||||||
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "if ({})", self.condition.write_string());
|
writeln!(write, "if ({})", self.condition.write_string());
|
||||||
self.then_statement.write_line(indent, write)?;
|
self.then_statement.write_line(indent, write);
|
||||||
if let Some(else_statement) = &self.else_statement {
|
if let Some(else_statement) = &self.else_statement {
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "else");
|
writeln!(write, "else");
|
||||||
else_statement.write_line(indent, write)?;
|
else_statement.write_line(indent, write);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -450,7 +422,7 @@ impl WriteLine for DoWhileStatement {
|
|||||||
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "do");
|
writeln!(write, "do");
|
||||||
self.statement.write_line(indent, write)?;
|
self.statement.write_line(indent, write);
|
||||||
write_indent(indent, write);
|
write_indent(indent, write);
|
||||||
writeln!(write, "while ({});", self.expression.write_string())
|
writeln!(write, "while ({});", self.expression.write_string())
|
||||||
}
|
}
|
||||||
@@ -603,7 +575,7 @@ impl WriteString for FloatSuffix {
|
|||||||
impl WriteString for MemberExpression {
|
impl WriteString for MemberExpression {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
"({}{}{})",
|
"{}{}{}",
|
||||||
self.expression.write_string(),
|
self.expression.write_string(),
|
||||||
match self.operator.node {
|
match self.operator.node {
|
||||||
MemberOperator::Direct => ".",
|
MemberOperator::Direct => ".",
|
||||||
@@ -679,7 +651,7 @@ impl WriteString for UnaryOperatorExpression {
|
|||||||
impl WriteString for CastExpression {
|
impl WriteString for CastExpression {
|
||||||
fn write_string(&self) -> String {
|
fn write_string(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
"({})({})",
|
"({}){}",
|
||||||
self.type_name.write_string(),
|
self.type_name.write_string(),
|
||||||
self.expression.write_string()
|
self.expression.write_string()
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user