Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tahadostifam committed Feb 9, 2025
1 parent 664dd7b commit faa5d74
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions ast/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ impl fmt::Display for Expression {
write!(f, "[{}]", array_items_to_string(array.clone()))
}
Expression::ArrayIndex(array_index) => {
todo!()
write!(f, "{}", array_index.identifier)?;
for item in &array_index.dimensions {
write!(f, "[{}]", item)?;
}
write!(f, "")
}
Expression::Assignment(assignment) => write!(f, "{} = {}", assignment.identifier, assignment.expr),
Expression::ArrayIndexAssign(array_index_assign) => {
Expand All @@ -127,8 +131,36 @@ impl fmt::Display for Expression {
}
Expression::AddressOf(expression) => write!(f, "&({})", expression),
Expression::Dereference(expression) => write!(f, "(*{})", expression),
Expression::StructInit(struct_init) => todo!(),
Expression::StructFieldAccess(struct_field_access) => todo!(),
Expression::StructInit(struct_init) => {
write!(f, "struct {} {{", struct_init.name)?;
for field in &struct_init.field_inits {
write!(f, "{}: {};", field.name, field.value)?;
}
write!(f, "}}")
}
Expression::StructFieldAccess(struct_field_access) => {
write!(f, "{}", struct_field_access.expr)?;

for item in &struct_field_access.chains {
if let Some(field_access) = item.field_access.clone() {
write!(f, ".{}", field_access.identifier.name)?;
}

if let Some(method_call) = item.method_call.clone() {
write!(f, ".{}(", method_call.function_name.name)?;

for (idx, arg) in method_call.arguments.iter().enumerate() {
if idx == method_call.arguments.len() - 1 {
write!(f, "{})", arg)?;
} else{
write!(f, "{}, ", arg)?;
}
}
}
}

write!(f, "")
}
}
}
}
Expand Down

0 comments on commit faa5d74

Please sign in to comment.