core: add module type

This commit is contained in:
abdul124 2024-12-24 12:45:25 +08:00
parent 1531b6cc98
commit 763ab87b32
6 changed files with 42 additions and 3 deletions

View File

@ -636,7 +636,8 @@ impl Nac3 {
TopLevelDef::Function { codegen_callback, .. } => { TopLevelDef::Function { codegen_callback, .. } => {
*codegen_callback = Some(rpc_codegen_callback(*is_async)); *codegen_callback = Some(rpc_codegen_callback(*is_async));
} }
TopLevelDef::Class { methods, .. } => { TopLevelDef::Class { methods, .. } | TopLevelDef::Module { methods, .. } => {
// TODO: Update this to handle functions as well
let (class_def, method_name) = class_data.as_ref().unwrap(); let (class_def, method_name) = class_data.as_ref().unwrap();
for (name, _, id) in &*methods { for (name, _, id) in &*methods {
if name != method_name { if name != method_name {

View File

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

View File

@ -101,7 +101,9 @@ impl TopLevelComposer {
let builtin_name_list = definition_ast_list let builtin_name_list = definition_ast_list
.iter() .iter()
.map(|def_ast| match *def_ast.0.read() { .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::Function { simple_name, .. }
| TopLevelDef::Variable { simple_name, .. } => simple_name.to_string(), | TopLevelDef::Variable { simple_name, .. } => simple_name.to_string(),
}) })

View File

@ -359,6 +359,23 @@ pub fn make_exception_fields(int32: Type, int64: Type, str: Type) -> Vec<(StrRef
impl TopLevelDef { impl TopLevelDef {
pub fn to_string(&self, unifier: &mut Unifier) -> String { pub fn to_string(&self, unifier: &mut Unifier) -> String {
match self { match self {
TopLevelDef::Module { name, fields, methods, .. } => {
let fields_str = fields
.iter()
.map(|(n, ty, _)| (n.to_string(), unifier.stringify(*ty)))
.collect_vec();
let methods_str = methods
.iter()
.map(|(n, ty, id)| (n.to_string(), unifier.stringify(*ty), *id))
.collect_vec();
format!(
"Module {{\nname: {:?},\nfields: {:?},\nmethods: {:?}\n}}",
name,
fields_str.iter().map(|(a, _)| a).collect_vec(),
methods_str.iter().map(|(a, b, _)| (a, b)).collect_vec(),
)
}
TopLevelDef::Class { name, ancestors, fields, methods, type_vars, .. } => { TopLevelDef::Class { name, ancestors, fields, methods, type_vars, .. } => {
let fields_str = fields let fields_str = fields
.iter() .iter()

View File

@ -92,6 +92,24 @@ pub struct FunInstance {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TopLevelDef { pub enum TopLevelDef {
Module {
/// Module name
name: StrRef,
/// Module fields.
///
/// Name and type is mutable.
fields: Vec<(StrRef, Type, bool)>,
/// Module Attributes.
///
/// Name, type, value.
attributes: Vec<(StrRef, Type, ast::Constant)>,
/// Class methods, pointing to the corresponding function definition.
methods: Vec<(StrRef, Type, DefinitionId)>,
/// Symbol resolver of the module defined the class; [None] if it is built-in type.
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
/// Definition location.
loc: Option<Location>,
},
Class { Class {
/// Name for error messages and symbols. /// Name for error messages and symbols.
name: StrRef, name: StrRef,

View File

@ -2695,7 +2695,7 @@ impl<'a> Inferencer<'a> {
.read() .read()
.iter() .iter()
.map(|def| match *def.read() { .map(|def| match *def.read() {
TopLevelDef::Class { name, .. } => (name, false), TopLevelDef::Class { name, .. } | TopLevelDef::Module { name, .. } => (name, false), // TODO: Check if should be global
TopLevelDef::Function { simple_name, .. } => (simple_name, false), TopLevelDef::Function { simple_name, .. } => (simple_name, false),
TopLevelDef::Variable { simple_name, .. } => (simple_name, true), TopLevelDef::Variable { simple_name, .. } => (simple_name, true),
}) })