Skip to content

Commit

Permalink
Fix array element type bug reported by calroc in #16
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Mar 10, 2023
1 parent 5037eee commit 4207be2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ncc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,27 @@ impl Expr
}
else
{
let first_type = exprs[0].eval_type()?;
let mut elem_type = exprs[0].eval_type()?;

for expr in &exprs[1..] {
let expr_type = expr.eval_type()?;
if !first_type.eq(&expr_type) {
return ParseError::msg_only("array element types do not match");

match (&elem_type, &expr_type) {
(Int(m), Int(n)) => {
elem_type = Type::Int(max(*m, *n))
}

_ => {
if !elem_type.eq(&expr_type) {
return ParseError::msg_only("array element types do not match");
}
}
}

}

Ok(Array {
elem_type: Box::new(first_type),
elem_type: Box::new(elem_type),
size_expr: Box::new(Expr::Int(exprs.len() as i128))
})
}
Expand Down
3 changes: 3 additions & 0 deletions ncc/tests/arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ uint8_t bytes2d[2][2] = { {0, 1}, {2, 3} };

uint8_t array2d[600][800];

// Integer literals of type long and int in the same array literal
u64 arr_int_long[2] = { 0x7fff8beb, 0x8000e82a };

int main()
{
assert(int_array[0] == 7);
Expand Down

0 comments on commit 4207be2

Please sign in to comment.