Skip to content

Commit e335d89

Browse files
committed
feat: added enum declaration parsing
1 parent c7d5305 commit e335d89

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
2+
use astoir_hir::{ctx::HIRContext, nodes::{HIRNode, HIRNodeKind}};
3+
use compiler_typing::{enums::{RawEnumTypeContainer}, raw::RawType};
4+
use diagnostics::{DiagnosticResult, MaybeDiagnostic};
5+
6+
use crate::types::{lower_ast_type_struct};
7+
8+
pub fn lower_ast_enum_entry(context: &mut HIRContext, node: Box<ASTTreeNode>, container: &mut RawEnumTypeContainer) -> MaybeDiagnostic {
9+
if let ASTTreeNodeKind::EnumEntryDeclaration { name, fields } = node.kind.clone() {
10+
let mut hir_fields = vec![];
11+
12+
for f in fields {
13+
if let ASTTreeNodeKind::StructFieldMember { name, member_type } = f.kind {
14+
let t = lower_ast_type_struct(context, member_type, container, &*node)?;
15+
16+
hir_fields.push((name.hash, t))
17+
}
18+
19+
panic!("Invalid field node type!");
20+
}
21+
22+
container.append_entry(name, hir_fields);
23+
}
24+
25+
panic!("Invalid node")
26+
}
27+
28+
pub fn lower_ast_enum(context: &mut HIRContext, node: Box<ASTTreeNode>) -> DiagnosticResult<Box<HIRNode>> {
29+
if let ASTTreeNodeKind::EnumDeclaration { name, entries, functions, type_params } = node.kind.clone() {
30+
let mut container = RawEnumTypeContainer::new(context.type_storage.types.vals.len(), type_params);
31+
32+
for entry in entries {
33+
lower_ast_enum_entry(context, entry, &mut container)?;
34+
}
35+
36+
let ind = context.type_storage.append_with_hash(name.hash, RawType::Enum(container.clone()))?;
37+
38+
return Ok(Box::new(HIRNode::new(HIRNodeKind::EnumDeclaration { type_name: ind, container }, &node.start, &node.end)))
39+
}
40+
41+
panic!("Invalid node")
42+
}

compiler/astoir_hir_lowering/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ast::{ctx::ParserCtx, tree::{ASTTreeNode, ASTTreeNodeKind}};
22
use astoir_hir::{ctx::{HIRBranchedContext, HIRContext}, nodes::{HIRNode, HIRNodeKind}};
33
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, move_current_diagnostic_pos};
44

5-
use crate::{arrays::lower_ast_array_modify, control::{lower_ast_for_block, lower_ast_if_statement, lower_ast_while_block}, func::{lower_ast_function_call, lower_ast_function_declaration, lower_ast_shadow_function_declaration}, math::lower_ast_math_operation, structs::lower_ast_struct_declaration, values::lower_ast_value, var::{lower_ast_variable_assign, lower_ast_variable_declaration}};
5+
use crate::{arrays::lower_ast_array_modify, control::{lower_ast_for_block, lower_ast_if_statement, lower_ast_while_block}, enums::lower_ast_enum, func::{lower_ast_function_call, lower_ast_function_declaration, lower_ast_shadow_function_declaration}, math::lower_ast_math_operation, structs::lower_ast_struct_declaration, values::lower_ast_value, var::{lower_ast_variable_assign, lower_ast_variable_declaration}};
66

77
pub mod literals;
88
pub mod var;
@@ -93,6 +93,12 @@ pub fn lower_ast_toplevel(context: &mut HIRContext, node: Box<ASTTreeNode>) -> D
9393
return Ok(true)
9494
},
9595

96+
ASTTreeNodeKind::EnumDeclaration { .. } => {
97+
lower_ast_enum(context, node)?;
98+
99+
return Ok(true)
100+
}
101+
96102
_ => panic!("Invalid node type")
97103
}
98104
}

compiler/astoir_hir_lowering/src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ast::types::ASTType;
22
use astoir_hir::ctx::HIRContext;
3-
use compiler_typing::{raw::RawType, references::TypeReference, structs::RawStructTypeContainer, tree::Type};
3+
use compiler_typing::{TypeParamType, raw::RawType, references::TypeReference, structs::RawStructTypeContainer, tree::Type};
44
use compiler_utils::hash::HashedString;
55
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::{make_cannot_find_type, make_diff_size_specifiers, make_req_type_kind}};
66

@@ -59,12 +59,12 @@ pub fn lower_ast_type<K: DiagnosticSpanOrigin>(context: &mut HIRContext, t: ASTT
5959
};
6060
}
6161

62-
pub fn lower_ast_type_struct<K: DiagnosticSpanOrigin>(context: &mut HIRContext, t: ASTType, struct_container: &RawStructTypeContainer, origin: &K) -> DiagnosticResult<TypeReference> {
62+
pub fn lower_ast_type_struct<K: DiagnosticSpanOrigin, T: TypeParamType>(context: &mut HIRContext, t: ASTType, container: &T, origin: &K) -> DiagnosticResult<TypeReference> {
6363
if let ASTType::Generic(id, _, _, _) = &t {
6464
let key = HashedString::new(id.clone());
6565

66-
if struct_container.type_params.contains_key(&key) {
67-
return Ok(TypeReference::Unresolved(struct_container.type_params[&key]));
66+
if container.has_type_param(&key) {
67+
return Ok(TypeReference::Unresolved(container.get_type_param_ind(&key)));
6868
}
6969
}
7070

0 commit comments

Comments
 (0)