Skip to content

Commit

Permalink
refactor(#28): parser now passes even more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: fami-fish <[email protected]>
  • Loading branch information
fami-fish authored Aug 10, 2024
1 parent 7fb479c commit 384bee7
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 156 deletions.
21 changes: 14 additions & 7 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub enum LabelAttribute {
pub enum Type {
Size(usize),
// NOTE: a size of 0 represents an array of undetermined length e.g [1:]
Heap { is_pointer: bool, contents: Vec<(Type, Option<usize>)> },
Heap { is_pointer: bool, contents: Vec<Type> },
Array { inner: Box<Type>, elems: Option<usize> },
Struct(String),
Register { inner: Option<Box<Type>>, ident: usize },
}
Expand All @@ -50,15 +51,21 @@ impl Display for Type {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Size(s) => write!(f, "{s}")?,
Self::Array { inner, elems } => {
write!(f, "{inner}")?;
if elems.is_none() {
return Ok(());
};

write!(f, ":")?;
if elems.unwrap() != 0 {
write!(f, "{}", elems.unwrap())?;
}
},
Self::Heap { is_pointer, contents } => {
write!(f, "{}", if *is_pointer { "[" } else { "{" })?;
for (i, (t, elems)) in contents.iter().enumerate() {
for (i, t) in contents.iter().enumerate() {
write!(f, "{t}")?;
match elems {
Some(0) => write!(f, ":")?,
Some(size) => write!(f, ":{size}")?,
None => {},
};

if i != contents.len() - 1 {
write!(f, ", ")?;
Expand Down
Loading

0 comments on commit 384bee7

Please sign in to comment.