Skip to content

Commit

Permalink
type checker slice type
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgcodevil committed Dec 16, 2024
1 parent b0af1c5 commit b573622
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/type_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,17 @@ fn resolve_access(
return Ok(curr);
}
match access_nodes.get(i).unwrap() {
Node::ArrayAccess { .. } => {
if let Type::Array { elem_type, .. } = curr {
resolve_access(
global_scope,
var_tables,
elem_type.deref().clone(),
i + 1,
access_nodes,
)
} else {
Err("array access to not array variable".to_string())
}
}
Node::ArrayAccess { .. } => match curr {
Type::Array { elem_type, .. } => resolve_access(
global_scope,
var_tables,
elem_type.deref().clone(),
i + 1,
access_nodes,
),
Type::Slice { elem_type, .. } => Ok(elem_type.deref().clone()),
_ => Err("array access to not array variable".to_string()),
},

Node::MemberAccess { member, metadata } => match curr {
Type::Custom(struct_name) => {
Expand Down Expand Up @@ -684,6 +682,16 @@ mod tests {
assert!(check(&program).is_err());
}

#[test]
fn test_access_slice() {
let source_code = r#"
arr:int[] = [1];
arr[0];
"#;
let program = ast::parse(source_code);
check(&program).unwrap();
}

#[test]
fn test_variable_scope_nested_block() {
let source_code = r#"
Expand Down

0 comments on commit b573622

Please sign in to comment.