core: add module type

This commit is contained in:
abdul124 2024-12-31 11:44:38 +08:00
parent 59a5cdb8f9
commit e5b358ae8c
6 changed files with 27 additions and 1 deletions

View File

@ -665,6 +665,9 @@ impl Nac3 {
"Unsupported @rpc annotation on global variable",
)))
}
TopLevelDef::Module { .. } => {
unreachable!("Type module cannot be decorated with @rpc")
}
}
}
}

View File

@ -979,7 +979,7 @@ pub fn gen_call<'ctx, G: CodeGenerator>(
TopLevelDef::Class { .. } => {
return Ok(Some(generator.gen_constructor(ctx, fun.0, &def, params)?))
}
TopLevelDef::Variable { .. } => unreachable!(),
TopLevelDef::Variable { .. } | TopLevelDef::Module { .. } => unreachable!(),
}
}
.or_else(|_: String| {

View File

@ -102,6 +102,7 @@ impl TopLevelComposer {
.iter()
.map(|def_ast| match *def_ast.0.read() {
TopLevelDef::Class { name, .. } => name.to_string(),
TopLevelDef::Module { alias, .. } => alias.to_string(),
TopLevelDef::Function { simple_name, .. }
| TopLevelDef::Variable { simple_name, .. } => simple_name.to_string(),
})

View File

@ -375,6 +375,13 @@ pub fn make_exception_fields(int32: Type, int64: Type, str: Type) -> Vec<(StrRef
impl TopLevelDef {
pub fn to_string(&self, unifier: &mut Unifier) -> String {
match self {
TopLevelDef::Module { name, alias, methods, .. } => {
let method_str = methods.iter().map(|(n, _)| n.to_string()).collect_vec();
format!(
"Module {{\nname: {:?},\nalias{:?},\nattributes{:?}\n}}",
name, alias, method_str
)
}
TopLevelDef::Class {
name, ancestors, fields, methods, attributes, type_vars, ..
} => {

View File

@ -92,6 +92,20 @@ pub struct FunInstance {
#[derive(Debug, Clone)]
pub enum TopLevelDef {
Module {
/// Name of the module
name: StrRef,
/// Alias used by the module
alias: StrRef,
/// Module ID used for [`TypeEnum`]
module_id: DefinitionId,
/// Methods
methods: HashMap<StrRef, DefinitionId>,
/// Symbol resolver of the module defined the class.
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
/// Definition location.
loc: Option<Location>,
},
Class {
/// Name for error messages and symbols.
name: StrRef,

View File

@ -2735,6 +2735,7 @@ impl Inferencer<'_> {
.iter()
.map(|def| match *def.read() {
TopLevelDef::Class { name, .. } => (name, false),
TopLevelDef::Module { alias, .. } => (alias, false),
TopLevelDef::Function { simple_name, .. } => (simple_name, false),
TopLevelDef::Variable { simple_name, .. } => (simple_name, true),
})