[core] add module type

This commit is contained in:
abdul124 2025-01-10 11:06:14 +08:00
parent 4bd5349381
commit febfd1241d
6 changed files with 25 additions and 3 deletions

View File

@ -713,6 +713,7 @@ 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

@ -101,7 +101,8 @@ impl TopLevelComposer {
let builtin_name_list = definition_ast_list
.iter()
.map(|def_ast| match *def_ast.0.read() {
TopLevelDef::Class { name, .. } => name.to_string(),
TopLevelDef::Class { name, .. }
| TopLevelDef::Module { name, .. } => name.to_string(),
TopLevelDef::Function { simple_name, .. }
| TopLevelDef::Variable { simple_name, .. } => simple_name.to_string(),
})

View File

@ -379,6 +379,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, attributes, .. } => {
let method_str = attributes.iter().map(|(n, _)| n.to_string()).collect_vec();
format!(
"Module {{\nname: {:?},\nattributes{:?}\n}}",
name, method_str
)
}
TopLevelDef::Class {
name, ancestors, fields, methods, attributes, type_vars, ..
} => {

View File

@ -92,6 +92,18 @@ pub struct FunInstance {
#[derive(Debug, Clone)]
pub enum TopLevelDef {
Module {
/// Name of the module
name: StrRef,
/// Module ID used for [`TypeEnum`]
module_id: DefinitionId,
/// DefinitionId of `TopLevelDef::{Class, Function, Variable}` within the module
attributes: 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

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