Skip to content

Commit

Permalink
Started working on namespace resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Sep 19, 2024
1 parent 009fe49 commit cc8e1b9
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 169 deletions.
11 changes: 6 additions & 5 deletions src/ast/datatype/kind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
AnonymousEnum, AnonymousStruct, AnoymousUnion, CInteger, FixedArray, FloatSize,
FunctionPointer, IntegerBits, IntegerSign, Type,
};
use crate::source_files::Source;
use crate::{name::Name, source_files::Source};

#[derive(Clone, Debug)]
pub enum TypeKind {
Expand All @@ -16,7 +16,7 @@ pub enum TypeKind {
Pointer(Box<Type>),
FixedArray(Box<FixedArray>),
Void,
Named(String),
Named(Name),
AnonymousStruct(AnonymousStruct),
AnonymousUnion(AnoymousUnion),
AnonymousEnum(AnonymousEnum),
Expand All @@ -30,9 +30,10 @@ impl TypeKind {

pub fn allow_indirect_undefined(&self) -> bool {
if let TypeKind::Named(name) = self {
if name.starts_with("struct<")
|| name.starts_with("union<")
|| name.starts_with("enum<")
let basename = &name.basename;
if basename.starts_with("struct<")
|| basename.starts_with("union<")
|| basename.starts_with("enum<")
{
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions src/ast/expr/call.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::Expr;
use crate::ast::{CompileTimeArgument, Type};
use crate::{
ast::{CompileTimeArgument, Type},
name::Name,
};

#[derive(Clone, Debug)]
pub struct Call {
pub function_name: String,
pub function_name: Name,
pub arguments: Vec<Expr>,
pub expected_to_return: Option<Type>,
pub generics: Vec<CompileTimeArgument>,
Expand Down
7 changes: 4 additions & 3 deletions src/c/translation/types/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
MemberDeclaration, MemberDeclarator, ParseError,
},
diagnostics::Diagnostics,
name::Name,
};
use indexmap::IndexMap;
use std::collections::HashMap;
Expand All @@ -30,8 +31,8 @@ pub fn make_composite(
})?;

return Ok(match &composite.kind {
CompositeKind::Struct => TypeKind::Named(format!("struct<{}>", name)),
CompositeKind::Union => TypeKind::Named(format!("union<{}>", name)),
CompositeKind::Struct => TypeKind::Named(Name::plain(format!("struct<{}>", name))),
CompositeKind::Union => TypeKind::Named(Name::plain(format!("union<{}>", name))),
});
};

Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn make_composite(
source: composite.source,
});

Ok(TypeKind::Named(name))
Ok(TypeKind::Named(Name::plain(name)))
} else {
let anonymous_struct = AnonymousStruct { fields, is_packed };

Expand Down
6 changes: 5 additions & 1 deletion src/c/translation/types/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
translation::eval::evaluate_to_const_integer,
},
index_map_ext::IndexMapExt,
name::Name,
};
use indexmap::IndexMap;
use num_bigint::BigInt;
Expand Down Expand Up @@ -91,7 +92,10 @@ pub fn make_anonymous_enum(
todo!("support enum type specifiers")
}

Ok(TypeKind::Named(format!("enum<{}>", named.name)))
Ok(TypeKind::Named(Name::plain(format!(
"enum<{}>",
named.name
))))
}
}
}
9 changes: 5 additions & 4 deletions src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
Interpreter, InterpreterError,
},
ir::{self, InterpreterSyscallKind},
name::Name,
resolved,
source_files::Source,
tag::Tag,
Expand Down Expand Up @@ -56,7 +57,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {

// Call to function we actually care about
let call = ExprKind::Call(Box::new(Call {
function_name: "main".into(),
function_name: Name::plain("main"),
arguments: vec![],
expected_to_return: Some(void.clone()),
generics: vec![],
Expand Down Expand Up @@ -103,7 +104,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
fields: IndexMap::from_iter([(
"kind".into(),
Field {
ast_type: TypeKind::Named("ProjectKind".into()).at(source),
ast_type: TypeKind::Named(Name::plain("ProjectKind")).at(source),
privacy: Privacy::Public,
source,
},
Expand Down Expand Up @@ -149,7 +150,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
Parameter::new("name".into(), ptr_char.clone()),
Parameter::new(
"project".into(),
TypeKind::Named("Project".into()).at(source),
TypeKind::Named(Name::plain("Project")).at(source),
),
],
is_cstyle_vararg: false,
Expand All @@ -164,7 +165,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
ExprKind::Variable("name".into()).at(source),
),
(
TypeKind::Named("ProjectKind".into()).at(source),
TypeKind::Named(Name::plain("ProjectKind")).at(source),
ExprKind::Member(
Box::new(ExprKind::Variable("project".into()).at(source)),
"kind".into(),
Expand Down
5 changes: 3 additions & 2 deletions src/lexer/identifier_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
name::Name,
source_files::Source,
token::{NamespacedIndentifier, Token, TokenKind},
token::{Token, TokenKind},
};

pub struct IdentifierState {
Expand All @@ -17,7 +18,7 @@ impl IdentifierState {
let basename = identifier.split_off(last_slash + 1);
let namespace = identifier;

return TokenKind::NamespacedIdentifier(NamespacedIndentifier {
return TokenKind::NamespacedIdentifier(Name {
namespace,
basename,
})
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl<T: Text + Send> Lexer<T> {
}
',' => Has(TokenKind::Comma.at(source)),
':' if self.characters.eat('=') => Has(TokenKind::DeclareAssign.at(source)),
':' if self.characters.eat(':') => Has(TokenKind::Namespace.at(source)),
':' if self.characters.eat(':') => Has(TokenKind::StaticMember.at(source)),
':' => Has(TokenKind::Colon.at(source)),
'#' => Has(TokenKind::Hash.at(source)),
'\"' => {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod llvm_backend;
mod logic;
mod look_ahead;
mod lower;
mod name;
mod parser;
mod path;
mod pragma_section;
Expand Down
2 changes: 2 additions & 0 deletions src/parser/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum AnnotationKind {
ThreadLocal,
Packed,
AbideAbi,
Namespace(String),
}

impl AnnotationKind {
Expand All @@ -34,6 +35,7 @@ impl Display for AnnotationKind {
Self::ThreadLocal => "thread_local",
Self::Packed => "packed",
Self::AbideAbi => "abide_abi",
Self::Namespace(_) => "namespace",
})
}
}
4 changes: 4 additions & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum ParseErrorKind {
word_for_nth: String,
},
GenericsNotSupportedHere,
NamespaceNotAllowedHere,
Other {
message: String,
},
Expand Down Expand Up @@ -207,6 +208,9 @@ impl Display for ParseErrorKind {
ParseErrorKind::GenericsNotSupportedHere => {
write!(f, "Generics not supported here")?;
}
ParseErrorKind::NamespaceNotAllowedHere => {
write!(f, "Namespace not allowed here")?;
}
ParseErrorKind::Other { message } | ParseErrorKind::Lexical { message } => {
write!(f, "{}", message)?;
}
Expand Down
13 changes: 9 additions & 4 deletions src/parser/parse_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
let (annotation_name, source) =
self.parse_identifier_keep_location(Some("for annotation name"))?;

self.parse_token(TokenKind::CloseBracket, Some("to close annotation body"))?;

Ok(match annotation_name.as_str() {
let annotation = match annotation_name.as_str() {
"foreign" => AnnotationKind::Foreign,
"thread_local" => AnnotationKind::ThreadLocal,
"packed" => AnnotationKind::Packed,
"abide_abi" => AnnotationKind::AbideAbi,
"namespace" => {
let namespace = self.parse_identifier(Some("for namespace name"))?;
AnnotationKind::Namespace(namespace)
}
_ => {
return Err(ParseErrorKind::UnrecognizedAnnotation {
name: annotation_name,
}
.at(source))
}
}
.at(source))
.at(source);

self.parse_token(TokenKind::CloseBracket, Some("to close annotation body"))?;
Ok(annotation)
}
}
9 changes: 6 additions & 3 deletions src/parser/parse_expr/post/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::Parser;
use crate::{
ast::{Expr, ExprKind},
inflow::Inflow,
parser::error::ParseError,
parser::{error::ParseError, parse_util::into_plain_name},
token::{Token, TokenKind},
};

Expand All @@ -12,13 +12,16 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
// ^

let source = self.parse_token(TokenKind::Member, Some("for member expression"))?;
let member_name = self.parse_identifier(Some("for member name"))?;
let member_name = self.parse_name(Some("for member name"))?;

if self.input.peek_is(TokenKind::OpenParen) || self.input.peek_is(TokenKind::OpenAngle) {
let generics = self.parse_generics()?;
self.parse_call_with(member_name, generics, vec![subject], source)
} else {
Ok(ExprKind::Member(Box::new(subject), member_name).at(source))
Ok(
ExprKind::Member(Box::new(subject), into_plain_name(member_name, source)?)
.at(source),
)
}
}
}
5 changes: 3 additions & 2 deletions src/parser/parse_expr/primary/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::Parser;
use crate::{
ast::{Call, CompileTimeArgument, Expr, ExprKind},
inflow::Inflow,
name::Name,
parser::error::ParseError,
source_files::Source,
token::{Token, TokenKind},
Expand All @@ -10,7 +11,7 @@ use crate::{
impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_call(
&mut self,
function_name: String,
function_name: Name,
generics: Vec<CompileTimeArgument>,
source: Source,
) -> Result<Expr, ParseError> {
Expand All @@ -22,7 +23,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {

pub fn parse_call_with(
&mut self,
function_name: String,
function_name: Name,
generics: Vec<CompileTimeArgument>,
prefix_args: Vec<Expr>,
source: Source,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse_expr/primary/enum_member_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
// EnumName::EnumVariant
// ^

self.parse_token(TokenKind::Namespace, Some("for enum member literal"))?;
self.parse_token(TokenKind::StaticMember, Some("for enum member literal"))?;

let variant_source = self.source_here();
let variant_name = self
Expand Down
21 changes: 14 additions & 7 deletions src/parser/parse_expr/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
UnaryOperator, While,
},
inflow::Inflow,
parser::{array_last, error::ParseErrorKind},
parser::{array_last, error::ParseErrorKind, parse_util::into_plain_name},
token::{StringLiteral, StringModifier, Token, TokenKind},
};
use std::ffi::CString;
Expand Down Expand Up @@ -69,19 +69,20 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
TokenKind::StructKeyword | TokenKind::UnionKeyword | TokenKind::EnumKeyword => {
self.parse_structure_literal()
}
TokenKind::Identifier(_) => {
TokenKind::Identifier(_) | TokenKind::NamespacedIdentifier(_) => {
// TODO: CLEANUP: This should be cleaned up once we have proper
// namespaces and generic parsing that applies to all cases

let name = self.input.eat_identifier().unwrap();
let name = self.parse_name(None::<&str>).unwrap();
let generics = self.parse_generics()?;

match self.input.peek().kind {
TokenKind::Namespace => {
TokenKind::StaticMember => {
if !generics.is_empty() {
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
}

let name = into_plain_name(name, source)?;
self.parse_enum_member_literal(name, source)
}
TokenKind::OpenCurly => {
Expand All @@ -101,7 +102,10 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.parse_type_from_parts(name, generics, source)?;
self.parse_structure_literal_with(ast_type)
}
_ => Ok(Expr::new(ExprKind::Variable(name), source)),
_ => Ok(Expr::new(
ExprKind::Variable(into_plain_name(name, source)?),
source,
)),
}
}
}
Expand All @@ -111,14 +115,17 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
}

self.parse_declare_assign(name, source)
self.parse_declare_assign(into_plain_name(name, source)?, source)
}
_ => {
if !generics.is_empty() {
return Err(ParseErrorKind::GenericsNotSupportedHere.at(source));
}

Ok(Expr::new(ExprKind::Variable(name), source))
Ok(Expr::new(
ExprKind::Variable(into_plain_name(name, source)?),
source,
))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse_stmt/parse_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_declaration(&mut self) -> Result<Stmt, ParseError> {
let (name, source) = self.parse_identifier_keep_location(Some("for variable name"))?;

let variable_type = self.parse_type(None::<&str>, Some("for variable type"))?;
let variable_type = self.parse_type(None::<&str>, Some("for variable"))?;

let initial_value = self
.input
Expand Down
Loading

0 comments on commit cc8e1b9

Please sign in to comment.