Skip to content

Commit

Permalink
Added initial ability for structs to be namespaced, as well as cleane…
Browse files Browse the repository at this point in the history
…d up some namespacing code
  • Loading branch information
IsaacShelton committed Sep 22, 2024
1 parent f56eba3 commit 2c5a70d
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 63 deletions.
5 changes: 2 additions & 3 deletions src/ast/function/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
mod parameters;

use super::{Stmt, Type};
use crate::{source_files::Source, tag::Tag};
use crate::{name::Name, source_files::Source, tag::Tag};
pub use parameters::{Parameter, Parameters};

#[derive(Clone, Debug)]
pub struct Function {
pub name: String,
pub name: Name,
pub parameters: Parameters,
pub return_type: Type,
pub stmts: Vec<Stmt>,
pub is_foreign: bool,
pub source: Source,
pub abide_abi: bool,
pub tag: Option<Tag>,
pub namespace: Option<String>,
}
4 changes: 2 additions & 2 deletions src/ast/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::Type;
use crate::source_files::Source;
use crate::{name::Name, source_files::Source};
use indexmap::IndexMap;

#[derive(Clone, Debug)]
pub struct Structure {
pub name: String,
pub name: Name,
pub fields: IndexMap<String, Field>,
pub is_packed: bool,
pub source: Source,
Expand Down
4 changes: 2 additions & 2 deletions src/c/translation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
ParameterDeclarationCore, ParameterTypeList, ParseError,
},
diagnostics::Diagnostics,
name::Name,
};
use std::collections::HashMap;

Expand Down Expand Up @@ -72,15 +73,14 @@ pub fn declare_function(
};

ast_file.functions.push(Function {
name,
name: Name::plain(name),
parameters,
return_type,
stmts: vec![],
is_foreign: true,
source,
abide_abi: true,
tag: None,
namespace: None,
});

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/c/translation/types/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn make_composite(
let is_packed = false;

if let Some(name) = &composite.name {
let name = format!("struct<{}>", name);
let name = Name::plain(format!("struct<{}>", name));

ast_file.structures.push(Structure {
name: name.clone(),
Expand All @@ -99,7 +99,7 @@ pub fn make_composite(
source: composite.source,
});

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

Expand Down
29 changes: 11 additions & 18 deletions src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use crate::{
};
use indexmap::IndexMap;

fn thin_void_function(name: impl ToString, syscall_kind: InterpreterSyscallKind) -> Function {
fn thin_void_function(name: impl Into<String>, syscall_kind: InterpreterSyscallKind) -> Function {
let source = Source::internal();
let void = TypeKind::Void.at(Source::internal());

Function {
name: name.to_string(),
name: Name::plain(name),
parameters: Parameters {
required: vec![],
is_cstyle_vararg: false,
Expand All @@ -39,33 +39,29 @@ fn thin_void_function(name: impl ToString, syscall_kind: InterpreterSyscallKind)
is_foreign: false,
source,
tag: None,
namespace: None,
}
}

fn thin_cstring_function(
name: impl ToString,
param_name: impl ToString,
name: impl Into<String>,
param_name: impl Into<String>,
syscall_kind: InterpreterSyscallKind,
) -> Function {
let source = Source::internal();
let void = TypeKind::Void.at(Source::internal());
let ptr_char = TypeKind::Pointer(Box::new(TypeKind::char().at(source))).at(source);

let param_name = param_name.into();
Function {
name: name.to_string(),
name: Name::plain(name.into()),
parameters: Parameters {
required: vec![Parameter::new(param_name.to_string(), ptr_char.clone())],
required: vec![Parameter::new(param_name.clone(), ptr_char.clone())],
is_cstyle_vararg: false,
},
return_type: void.clone(),
stmts: vec![StmtKind::Expr(
ExprKind::InterpreterSyscall(Box::new(InterpreterSyscall {
kind: syscall_kind,
args: vec![(
ptr_char.clone(),
ExprKind::Variable(param_name.to_string()).at(source),
)],
args: vec![(ptr_char.clone(), ExprKind::Variable(param_name).at(source))],
result_type: void.clone(),
}))
.at(source),
Expand All @@ -75,7 +71,6 @@ fn thin_cstring_function(
is_foreign: false,
source,
tag: None,
namespace: None,
}
}

Expand All @@ -94,15 +89,14 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
.at(Source::internal());

file.functions.push(Function {
name: "<interpreter entry point>".into(),
name: Name::plain("<interpreter entry point>"),
parameters: Parameters::default(),
return_type: void.clone(),
stmts: vec![StmtKind::Return(Some(call)).at(Source::internal())],
is_foreign: false,
source,
abide_abi: false,
tag: Some(Tag::InterpreterEntryPoint),
namespace: None,
});

file.enums.insert(
Expand Down Expand Up @@ -130,7 +124,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
);

file.structures.push(Structure {
name: "Project".into(),
name: Name::plain("Project"),
fields: IndexMap::from_iter([(
"kind".into(),
Field {
Expand Down Expand Up @@ -185,7 +179,7 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
));

file.functions.push(Function {
name: "project".into(),
name: Name::plain("project"),
parameters: Parameters {
required: vec![
Parameter::new("name".into(), ptr_char.clone()),
Expand Down Expand Up @@ -223,7 +217,6 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
is_foreign: false,
source,
tag: None,
namespace: None,
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/lexer/identifier_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl IdentifierState {

if let Some(last_slash) = self.last_slash {
let basename = identifier.split_off(last_slash + 1);

// Remove trailing slash
identifier.pop();

let namespace = identifier;

return TokenKind::NamespacedIdentifier(Name {
Expand Down
1 change: 0 additions & 1 deletion src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ fn lower_function(
}
}

eprintln!("warning: name mangling does not take all cases into account yet");
let mangled_name = if function.name.plain() == "main" {
"main".into()
} else if function.is_foreign {
Expand Down
23 changes: 22 additions & 1 deletion src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ pub struct Name {
}

impl Name {
pub fn new(namespace: Option<impl Into<String>>, basename: impl Into<String>) -> Self {
Self {
namespace: namespace
.map(|namespace| namespace.into())
.unwrap_or_default(),
basename: basename.into(),
}
}

pub fn plain(basename: impl Into<String>) -> Self {
Self {
namespace: "".into(),
Expand All @@ -29,11 +38,19 @@ impl Name {
None
}
}

pub fn fullname(&self) -> String {
if self.namespace.is_empty() {
self.basename.clone()
} else {
format!("{}/{}", self.namespace, self.basename)
}
}
}

impl Display for Name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.namespace, self.basename)
write!(f, "{}", self.fullname())
}
}

Expand All @@ -44,6 +61,10 @@ pub enum ResolvedName {
}

impl ResolvedName {
pub fn new(name: &Name) -> Self {
Self::Project(name.fullname().into_boxed_str())
}

pub fn plain(&self) -> &str {
match self {
ResolvedName::Remote(name) => &**name,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{
use crate::{
ast::{Function, Parameters, TypeKind},
inflow::Inflow,
name::Name,
token::{Token, TokenKind},
};

Expand Down Expand Up @@ -57,15 +58,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
.unwrap_or_default();

Ok(Function {
name,
name: Name::new(namespace, name),
parameters,
return_type,
stmts,
is_foreign,
source,
abide_abi,
tag: None,
namespace,
})
}
}
7 changes: 5 additions & 2 deletions src/parser/parse_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{
use crate::{
ast::{Field, Structure},
inflow::Inflow,
name::Name,
token::{Token, TokenKind},
};
use indexmap::IndexMap;
Expand All @@ -22,11 +23,13 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.ignore_newlines();

let mut is_packed = false;
let mut namespace = None;

for annotation in annotations {
match annotation.kind {
AnnotationKind::Packed => is_packed = true,
_ => return Err(self.unexpected_annotation(&annotation, Some("for structure"))),
AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace),
_ => return Err(self.unexpected_annotation(&annotation, Some("for struct"))),
}
}

Expand Down Expand Up @@ -61,7 +64,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
self.parse_token(TokenKind::CloseParen, Some("to end struct fields"))?;

Ok(Structure {
name,
name: Name::new(namespace, name),
fields,
is_packed,
source,
Expand Down
4 changes: 2 additions & 2 deletions src/pragma_section/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
ast::{AstFile, Expr, ExprKind, Function, Parameters, Stmt, StmtKind, TypeKind},
diagnostics::ErrorDiagnostic,
inflow::Inflow,
name::Name,
parser::{self, error::ParseError, Input},
show::{into_show, Show},
source_files::Source,
Expand Down Expand Up @@ -79,7 +80,7 @@ impl PragmaSection {
}

ast_file.functions.push(Function {
name: "main".into(),
name: Name::plain("main"),
parameters: Parameters {
required: vec![],
is_cstyle_vararg: false,
Expand All @@ -90,7 +91,6 @@ impl PragmaSection {
source,
abide_abi: false,
tag: None,
namespace: None,
});
} else {
return Err(Box::new(ParseError::expected(
Expand Down
2 changes: 1 addition & 1 deletion src/pragma_section/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl PragmaSection {
else {
return Err(into_show(
ParseErrorKind::Other {
message: "No Adept version was specifed for module!".into(),
message: "No Adept version was specifed for module! Use `pragma => adept(\"3.0\")` at the top of the module file".into(),
}
.at(self.pragma_source),
));
Expand Down
3 changes: 2 additions & 1 deletion src/resolve/core_structure_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use super::error::{ResolveError, ResolveErrorKind};
use crate::{
name::ResolvedName,
resolved::{self, StructureRef},
source_files::Source,
};

pub fn get_core_structure_info(
resolved_type: &resolved::Type,
source: Source,
) -> Result<(&str, StructureRef), ResolveError> {
) -> Result<(&ResolvedName, StructureRef), ResolveError> {
match &resolved_type.kind {
resolved::TypeKind::Structure(name, structure_ref) => Ok((name, *structure_ref)),
_ => Err(ResolveErrorKind::CannotCreateStructLiteralForNonStructure {
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/expr/struct_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn resolve_struct_literal_expr(
let (struct_name, structure_ref) = get_core_structure_info(&resolved_type, source)?;

let structure_type =
resolved::TypeKind::Structure(struct_name.to_string(), structure_ref).at(source);
resolved::TypeKind::Structure(struct_name.clone(), structure_ref).at(source);

let mut next_index = 0;
let mut resolved_fields = IndexMap::new();
Expand Down
Loading

0 comments on commit 2c5a70d

Please sign in to comment.