Skip to content

Commit d46e717

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

7 files changed

Lines changed: 33 additions & 10 deletions

File tree

compiler/ast/src/tree.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl ASTTreeNodeKind {
9191
}
9292

9393
pub fn is_tree_permissible(&self) -> bool {
94-
return matches!(self, ASTTreeNodeKind::FunctionDeclaration { .. } | ASTTreeNodeKind::StaticVariableDeclaration { .. } | ASTTreeNodeKind::ShadowFunctionDeclaration { .. }| ASTTreeNodeKind::StructLayoutDeclaration { .. })
94+
return matches!(self, ASTTreeNodeKind::FunctionDeclaration { .. } | ASTTreeNodeKind::EnumDeclaration { .. } | ASTTreeNodeKind::StaticVariableDeclaration { .. } | ASTTreeNodeKind::ShadowFunctionDeclaration { .. }| ASTTreeNodeKind::StructLayoutDeclaration { .. })
9595
}
9696

9797
pub fn get_tree_name(&self) -> Option<HashedString> {
@@ -116,6 +116,10 @@ impl ASTTreeNodeKind {
116116
return Some(HashedString::new(var_name.val.to_string()));
117117
},
118118

119+
ASTTreeNodeKind::EnumDeclaration { name, entries: _, functions: _, type_params: _ } => {
120+
return Some(name.clone())
121+
}
122+
119123
_ => return None
120124
}
121125
}

compiler/ast_parser/src/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ pub fn parse_type_parameters_declaration(tokens: &Vec<LexerToken>, ind: &mut usi
206206

207207
*ind += 1;
208208

209-
if tokens[*ind].tok_type == LexerTokenType::AngelBracketClose {
209+
if tokens[*ind].is_angel_bracket_close() {
210210
break;
211211
}
212212

213213
tokens[*ind].expects(LexerTokenType::Comma)?;
214214
*ind += 1;
215215
}
216216

217+
*ind += 1;
218+
217219
return Ok(container);
218220
}

compiler/astoir/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ pub fn run_astoir_hir(ctx: ParserCtx) -> DiagnosticResult<HIRContext> {
1616
}
1717

1818
pub fn run_astoir_mir(ctx: ParserCtx) -> DiagnosticResult<MIRContext> {
19-
return lower_hir(run_astoir_hir(ctx)?);
19+
let hir = run_astoir_hir(ctx)?;
20+
21+
return lower_hir(hir);
2022
}

compiler/astoir_hir_lowering/src/enums.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
22
use astoir_hir::{ctx::HIRContext, nodes::{HIRNode, HIRNodeKind}};
33
use compiler_typing::{enums::{RawEnumTypeContainer}, raw::RawType};
4-
use diagnostics::{DiagnosticResult, MaybeDiagnostic};
4+
use diagnostics::{DiagnosticResult, MaybeDiagnostic, builders::make_already_in_scope};
55

66
use crate::types::{lower_ast_type_struct};
77

@@ -13,15 +13,18 @@ pub fn lower_ast_enum_entry(context: &mut HIRContext, node: Box<ASTTreeNode>, co
1313
if let ASTTreeNodeKind::StructFieldMember { name, member_type } = f.kind {
1414
let t = lower_ast_type_struct(context, member_type, container, &*node)?;
1515

16-
hir_fields.push((name.hash, t))
16+
hir_fields.push((name.hash, t));
17+
continue;
1718
}
1819

1920
panic!("Invalid field node type!");
2021
}
2122

2223
container.append_entry(name, hir_fields);
24+
return Ok(())
2325
}
2426

27+
2528
panic!("Invalid node")
2629
}
2730

@@ -33,7 +36,10 @@ pub fn lower_ast_enum(context: &mut HIRContext, node: Box<ASTTreeNode>) -> Diagn
3336
lower_ast_enum_entry(context, entry, &mut container)?;
3437
}
3538

36-
let ind = context.type_storage.append_with_hash(name.hash, RawType::Enum(container.clone()))?;
39+
let ind = match context.type_storage.append_with_hash(name.hash, RawType::Enum(container.clone())) {
40+
Ok(v) => v,
41+
Err(_) => return Err(make_already_in_scope(&*node, &name.val).into())
42+
};
3743

3844
return Ok(Box::new(HIRNode::new(HIRNodeKind::EnumDeclaration { type_name: ind, container }, &node.start, &node.end)))
3945
}

compiler/astoir_hir_lowering/src/types.rs

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

77
pub fn lower_ast_type<K: DiagnosticSpanOrigin>(context: &mut HIRContext, t: ASTType, origin: &K) -> DiagnosticResult<Type> {
88
return match t {
@@ -24,7 +24,7 @@ pub fn lower_ast_type<K: DiagnosticSpanOrigin>(context: &mut HIRContext, t: ASTT
2424
}
2525

2626
if t.get_type_params_count(&context.type_storage) != type_params.len() {
27-
return Err(make_diff_size_specifiers(origin, &type_params.len(), &t.get_type_params_count(&context.type_storage)).into())
27+
return Err(make_diff_type_specifiers(origin, &type_params.len(), &t.get_type_params_count(&context.type_storage)).into())
2828
}
2929

3030
let mut t_params = vec![];

compiler/compiler_typing/src/raw.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ impl Hash for RawType {
309309
for function in &b.functions.vals {
310310
hasher.write_usize(*function);
311311
}
312+
},
313+
314+
RawType::Enum(container) => {
315+
hasher.write_usize(7);
316+
hasher.write_usize(container.self_ref);
312317
}
313318

314319
_ => panic!("Unhashable type {:#?}", self)

compiler/diagnostics/src/builders.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fmt::Display};
22

3-
use crate::{DiagnosticSpanOrigin, diagnostic::{Diagnostic, Level, Span, SpanKind, SpanPosition}, errors::{ALREADY_IN_SCOPE, ASSIGN_DIFF_TYPE_IR, BOUND_MISSING, DIFF_SIZE_SPECIFIERS, ENUM_PARENT_FIELDS, ERA_NOT_EXIST, EXPECTED_FREE, EXPECTED_TOKEN, EXPECTED_TYPE, FIELD_MISSING, FIELD_STRUCT_INIT, FIND_TYPE, FIND_TYPE_FIELD, FIND_TYPE_FUNCTION, FIND_VAR, FUNC_MISSING, INDEX_USAGE, INVALID_POINTING, INVALID_TYPE_REQ, IR_CAST, IR_INSTRUCTION_HELD_VAL, MATH_OPERATION_ASSIGNS, TRAIT_MISSING, TYPE_NOT_PART, UNEXPECTED_TOKEN, VARIABLE_UNINIT}, get_current_diagnostic_pos, warnings::UNUSED_VAR};
3+
use crate::{DiagnosticSpanOrigin, diagnostic::{Diagnostic, Level, Span, SpanKind, SpanPosition}, errors::{ALREADY_IN_SCOPE, ASSIGN_DIFF_TYPE_IR, BOUND_MISSING, DIFF_SIZE_SPECIFIERS, DIFF_TYPE_SPECIFIERS, ENUM_PARENT_FIELDS, ERA_NOT_EXIST, EXPECTED_FREE, EXPECTED_TOKEN, EXPECTED_TYPE, FIELD_MISSING, FIELD_STRUCT_INIT, FIND_TYPE, FIND_TYPE_FIELD, FIND_TYPE_FUNCTION, FIND_VAR, FUNC_MISSING, INDEX_USAGE, INVALID_POINTING, INVALID_TYPE_REQ, IR_CAST, IR_INSTRUCTION_HELD_VAL, MATH_OPERATION_ASSIGNS, TRAIT_MISSING, TYPE_NOT_PART, UNEXPECTED_TOKEN, VARIABLE_UNINIT}, get_current_diagnostic_pos, warnings::UNUSED_VAR};
44

55
pub fn make_expected_simple_error<K: DiagnosticSpanOrigin, E: Display, G: Display>(origin: &K, expected: &E, got: &G) -> Diagnostic {
66
origin.make_simple_diagnostic(EXPECTED_TOKEN.0, Level::Error, format!("expected {} but got {}", expected, got), None, vec![], vec![], vec![])
@@ -61,7 +61,11 @@ pub fn make_cannot_find_type_field<F: Display, T: Display>(field: &F, t: &T) ->
6161
}
6262

6363
pub fn make_diff_size_specifiers<K: DiagnosticSpanOrigin, A: Display, B: Display>(origin: &K, a: &A, b: &B) -> Diagnostic {
64-
origin.make_simple_diagnostic(DIFF_SIZE_SPECIFIERS.0, Level::Error, format!("expected {} size specifiers on this type, got {}", a, b), None, vec![], vec![], vec![])
64+
origin.make_simple_diagnostic(DIFF_SIZE_SPECIFIERS.0, Level::Error, format!("expected {} size specifiers on this type, got {}", b, a), None, vec![], vec![], vec![])
65+
}
66+
67+
pub fn make_diff_type_specifiers<K: DiagnosticSpanOrigin, A: Display, B: Display>(origin: &K, a: &A, b: &B) -> Diagnostic {
68+
origin.make_simple_diagnostic(DIFF_TYPE_SPECIFIERS.0, Level::Error, format!("expected {} type specifiers on this type, got {}", b, a), None, vec![], vec![], vec![])
6569
}
6670

6771
pub fn make_diff_type<K: DiagnosticSpanOrigin, V: Display, T: Display, A: Display>(origin: &K, var_name: &V, req_type: &T, got: &A, var_origin: &K) -> Diagnostic {

0 commit comments

Comments
 (0)