diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 623b122..de34180 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -22,7 +22,7 @@ use parking_lot::{Mutex, RwLock}; use nac3core::{ codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry}, symbol_resolver::SymbolResolver, - toplevel::{composer::{TopLevelComposer, CoreMode}, DefinitionId, GenCall, TopLevelDef}, + toplevel::{composer::{TopLevelComposer, ComposerConfig}, DefinitionId, GenCall, TopLevelDef}, typecheck::typedef::{FunSignature, FuncArg}, typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, }; @@ -239,7 +239,10 @@ impl Nac3 { }))), ), ]; - let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone(), CoreMode::Artiq); + let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone(), ComposerConfig { + kernel_ann: Some("Kernel"), + kernel_invariant_ann: "KernelInvariant" + }); let builtins_mod = PyModule::import(py, "builtins").unwrap(); let id_fn = builtins_mod.getattr("id").unwrap(); @@ -375,7 +378,10 @@ impl Nac3 { filename: &str, py: Python, ) -> PyResult<()> { - let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone(), CoreMode::Artiq); + let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone(), ComposerConfig { + kernel_ann: Some("Kernel"), + kernel_invariant_ann: "KernelInvariant" + }); let mut id_to_def = HashMap::new(); let mut id_to_type = HashMap::new(); diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 3398adf..6fe1dac 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -9,9 +9,9 @@ use crate::{ use super::*; -pub enum CoreMode { - Artiq, - Standalone +pub struct ComposerConfig { + pub kernel_ann: Option<&'static str>, + pub kernel_invariant_ann: &'static str, } type DefAst = (Arc>, Option>); @@ -31,12 +31,15 @@ pub struct TopLevelComposer { // number of built-in function and classes in the definition list, later skip pub builtin_num: usize, // indicate the mode that we are using the core - pub mode: CoreMode, + pub core_config: ComposerConfig, } impl Default for TopLevelComposer { fn default() -> Self { - Self::new(vec![], CoreMode::Standalone).0 + Self::new(vec![], ComposerConfig { + kernel_ann: None, + kernel_invariant_ann: "Invariant" + }).0 } } @@ -45,7 +48,7 @@ impl TopLevelComposer { /// resolver can later figure out primitive type definitions when passed a primitive type name pub fn new( builtins: Vec<(StrRef, FunSignature, Arc)>, - mode: CoreMode + core_config: ComposerConfig ) -> (Self, HashMap, HashMap) { let mut primitives = Self::make_primitives(); let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives); @@ -116,7 +119,7 @@ impl TopLevelComposer { keyword_list, defined_names, method_class, - mode + core_config, }, builtin_id, builtin_ty, @@ -563,7 +566,7 @@ impl TopLevelComposer { unifier, primitives, &mut type_var_to_concrete_def, - (&self.keyword_list, &self.mode) + (&self.keyword_list, &self.core_config) )? } } @@ -836,9 +839,9 @@ impl TopLevelComposer { unifier: &mut Unifier, primitives: &PrimitiveStore, type_var_to_concrete_def: &mut HashMap, - core_info: (&HashSet, &CoreMode), + core_info: (&HashSet, &ComposerConfig), ) -> Result<(), String> { - let (keyword_list, core_mode) = core_info; + let (keyword_list, core_config) = core_info; let mut class_def = class_def.write(); let ( class_id, @@ -1068,27 +1071,17 @@ impl TopLevelComposer { let dummy_field_type = unifier.get_fresh_var().0; // handle Kernel[T], KernelInvariant[T] - let (annotation, mutable) = { - match core_mode { - CoreMode::Artiq => match &annotation.as_ref().node { - ast::ExprKind::Subscript { value, slice, .. } if matches!( - &value.node, - ast::ExprKind::Name { id, .. } if id == &"Kernel".into() - ) => (slice, true), - ast::ExprKind::Subscript { value, slice, .. } if matches!( - &value.node, - ast::ExprKind::Name { id, .. } if id == &"KernelInvariant".into() - ) => (slice, false), - _ => continue // ignore fields annotated otherwise - }, - CoreMode::Standalone => match &annotation.as_ref().node { - ast::ExprKind::Subscript { value, slice, .. } if matches!( - &value.node, - ast::ExprKind::Name { id, .. } if id == &"Invariant".into() - ) => (slice, false), - _ => (annotation, true) - } - } + let (annotation, mutable) = match &annotation.node { + ast::ExprKind::Subscript { value, slice, .. } if matches!( + &value.node, + ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into() + ) => (slice, false), + ast::ExprKind::Subscript { value, slice, .. } if matches!( + &value.node, + ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into()) + ) => (slice, true), + _ if core_config.kernel_ann.is_none() => (annotation, true), + _ => continue // ignore fields annotated otherwise }; class_fields_def.push((*attr, dummy_field_type, mutable)); diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index abc8e82..accc8fe 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -14,7 +14,7 @@ use nac3core::{ }, symbol_resolver::SymbolResolver, toplevel::{ - composer::{TopLevelComposer, CoreMode}, + composer::{TopLevelComposer, ComposerConfig}, TopLevelDef, helper::parse_parameter_default_value, type_annotation::*, }, @@ -46,7 +46,7 @@ fn main() { let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new( vec![], - CoreMode::Standalone + ComposerConfig { kernel_ann: None, kernel_invariant_ann: "Invariant" } ); let internal_resolver: Arc = ResolverInternal {