Skip to content

Commit

Permalink
Finished adding namespace support for enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Sep 25, 2024
1 parent cf0b523 commit 3b75a89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
ast::{CInteger, IntegerBits, IntegerRigidity},
cli::BuildOptions,
ir::{self, BasicBlocks, Global, Literal, OverflowOperator, Value, ValueReference},
name::ResolvedName,
resolved::{
self, Destination, DestinationKind, Expr, ExprKind, FloatOrInteger, FloatSize, Member,
NumericMode, SignOrIndeterminate, StmtKind, StructureLiteral, UnaryMathOperation,
Expand Down Expand Up @@ -861,7 +860,7 @@ fn lower_expr(
ExprKind::EnumMemberLiteral(enum_member_literal) => {
let enum_definition = resolved_ast
.enums
.get(&ResolvedName::new(&enum_member_literal.enum_name))
.get(&enum_member_literal.enum_name)
.expect("referenced enum to exist for enum member literal");

let member = enum_definition
Expand Down
5 changes: 4 additions & 1 deletion src/parser/parse_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
ast::{Enum, EnumMember, Named},
inflow::Inflow,
name::Name,
parser::annotation::AnnotationKind,
token::{Token, TokenKind},
};
use indexmap::IndexMap;
Expand All @@ -13,12 +14,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
let source = self.source_here();
assert!(self.input.advance().is_enum_keyword());

let mut namespace = None;
let name = self.parse_identifier(Some("for enum name after 'enum' keyword"))?;
self.ignore_newlines();

#[allow(clippy::never_loop, clippy::match_single_binding)]
for annotation in annotations {
match annotation.kind {
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
_ => return Err(self.unexpected_annotation(&annotation, Some("for enum"))),
}
}
Expand Down Expand Up @@ -55,7 +58,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.parse_token(TokenKind::CloseParen, Some("to close enum body"))?;

Ok(Named::<Enum> {
name: Name::plain(name),
name: Name::new(namespace, name),
value: Enum {
backing_type: None,
members,
Expand Down
7 changes: 7 additions & 0 deletions src/resolve/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ pub enum ResolveErrorKind {
CannotPerformOnUnspecializedInteger {
operation: String,
},
StaticMemberOfTypeDoesNotExist {
ty: String,
member: String,
},
Other {
message: String,
},
Expand Down Expand Up @@ -405,6 +409,9 @@ impl Display for ResolveErrorKind {
ResolveErrorKind::CannotPerformOnUnspecializedInteger { operation } => {
write!(f, "Cannot {operation} unspecialized integers")?;
}
ResolveErrorKind::StaticMemberOfTypeDoesNotExist { ty, member } => {
write!(f, "Static member '{member}' does not exist on type '{ty}'")?;
}
ResolveErrorKind::Other { message } => {
write!(f, "{}", message)?;
}
Expand Down
25 changes: 21 additions & 4 deletions src/resolve/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,31 @@ pub fn resolve_expr(
resolved::Expr::new(resolved::ExprKind::BooleanLiteral(*value), source),
)),
ast::ExprKind::EnumMemberLiteral(enum_member_literal) => {
let resolved_type =
resolved::TypeKind::Enum(ResolvedName::new(&enum_member_literal.enum_name))
.at(ast_expr.source);
let resolved_type = resolve_type(
ctx.type_search_ctx,
&ast::TypeKind::Named(enum_member_literal.enum_name.clone())
.at(enum_member_literal.source),
&mut Default::default(),
)?;

let TypeKind::Enum(resolved_name) = &resolved_type.kind else {
return Err(ResolveErrorKind::StaticMemberOfTypeDoesNotExist {
ty: enum_member_literal.enum_name.to_string(),
member: enum_member_literal.variant_name.clone(),
}
.at(source));
};

let resolved_name = resolved_name.clone();

Ok(TypedExpr::new(
resolved_type,
resolved::Expr::new(
resolved::ExprKind::EnumMemberLiteral(enum_member_literal.clone()),
resolved::ExprKind::EnumMemberLiteral(Box::new(resolved::EnumMemberLiteral {
enum_name: resolved_name,
variant_name: enum_member_literal.variant_name.clone(),
source,
})),
source,
),
))
Expand Down
9 changes: 8 additions & 1 deletion src/resolved/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod variable_storage;

pub use self::variable_storage::VariableStorageKey;
pub use crate::ast::{
CInteger, EnumMember, EnumMemberLiteral, FloatSize, IntegerBits, IntegerKnown, IntegerSign,
CInteger, EnumMember, FloatSize, IntegerBits, IntegerKnown, IntegerSign,
ShortCircuitingBinaryOperator, UnaryMathOperator,
};
use crate::{
Expand Down Expand Up @@ -488,6 +488,13 @@ pub struct ArrayAccess {
pub index: Expr,
}

#[derive(Clone, Debug)]
pub struct EnumMemberLiteral {
pub enum_name: ResolvedName,
pub variant_name: String,
pub source: Source,
}

#[derive(Clone, Debug)]
pub struct Branch {
pub condition: TypedExpr,
Expand Down

0 comments on commit 3b75a89

Please sign in to comment.