From febfd1241d64486b6496b4a1c92ea18aa743e4d7 Mon Sep 17 00:00:00 2001 From: abdul124 Date: Fri, 10 Jan 2025 11:06:14 +0800 Subject: [PATCH] [core] add module type --- nac3artiq/src/lib.rs | 1 + nac3core/src/codegen/expr.rs | 2 +- nac3core/src/toplevel/composer.rs | 3 ++- nac3core/src/toplevel/helper.rs | 7 +++++++ nac3core/src/toplevel/mod.rs | 12 ++++++++++++ nac3core/src/typecheck/type_inferencer/mod.rs | 3 ++- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index d35e66d1..59d4dbed 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -713,6 +713,7 @@ impl Nac3 { "Unsupported @rpc annotation on global variable", ))) } + TopLevelDef::Module { .. } => unreachable!("Type module cannot be decorated with @rpc"), } } } diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 30b8dcd3..6d2057e1 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -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| { diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index bd9a9214..b293fb4c 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -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(), }) diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index eb72d37a..72502aa4 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -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, .. } => { diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index cba2f5e7..88c007ec 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -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, + /// Symbol resolver of the module defined the class. + resolver: Option>, + /// Definition location. + loc: Option, + }, Class { /// Name for error messages and symbols. name: StrRef, diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index 742fa197..a58045be 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -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), })