nac3core/artiq/standalone: composer only config kernel annotation names

This commit is contained in:
ychenfo 2021-12-17 04:44:16 +08:00
parent 6ce2f1b7ec
commit dba165d95c
3 changed files with 35 additions and 36 deletions

View File

@ -22,7 +22,7 @@ use parking_lot::{Mutex, RwLock};
use nac3core::{ use nac3core::{
codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry}, codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry},
symbol_resolver::SymbolResolver, symbol_resolver::SymbolResolver,
toplevel::{composer::{TopLevelComposer, CoreMode}, DefinitionId, GenCall, TopLevelDef}, toplevel::{composer::{TopLevelComposer, ComposerConfig}, DefinitionId, GenCall, TopLevelDef},
typecheck::typedef::{FunSignature, FuncArg}, typecheck::typedef::{FunSignature, FuncArg},
typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, 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 builtins_mod = PyModule::import(py, "builtins").unwrap();
let id_fn = builtins_mod.getattr("id").unwrap(); let id_fn = builtins_mod.getattr("id").unwrap();
@ -375,7 +378,10 @@ impl Nac3 {
filename: &str, filename: &str,
py: Python, py: Python,
) -> PyResult<()> { ) -> 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_def = HashMap::new();
let mut id_to_type = HashMap::new(); let mut id_to_type = HashMap::new();

View File

@ -9,9 +9,9 @@ use crate::{
use super::*; use super::*;
pub enum CoreMode { pub struct ComposerConfig {
Artiq, pub kernel_ann: Option<&'static str>,
Standalone pub kernel_invariant_ann: &'static str,
} }
type DefAst = (Arc<RwLock<TopLevelDef>>, Option<ast::Stmt<()>>); type DefAst = (Arc<RwLock<TopLevelDef>>, Option<ast::Stmt<()>>);
@ -31,12 +31,15 @@ pub struct TopLevelComposer {
// number of built-in function and classes in the definition list, later skip // number of built-in function and classes in the definition list, later skip
pub builtin_num: usize, pub builtin_num: usize,
// indicate the mode that we are using the core // indicate the mode that we are using the core
pub mode: CoreMode, pub core_config: ComposerConfig,
} }
impl Default for TopLevelComposer { impl Default for TopLevelComposer {
fn default() -> Self { 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 /// resolver can later figure out primitive type definitions when passed a primitive type name
pub fn new( pub fn new(
builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>, builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>,
mode: CoreMode core_config: ComposerConfig
) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) { ) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) {
let mut primitives = Self::make_primitives(); let mut primitives = Self::make_primitives();
let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives); let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives);
@ -116,7 +119,7 @@ impl TopLevelComposer {
keyword_list, keyword_list,
defined_names, defined_names,
method_class, method_class,
mode core_config,
}, },
builtin_id, builtin_id,
builtin_ty, builtin_ty,
@ -563,7 +566,7 @@ impl TopLevelComposer {
unifier, unifier,
primitives, primitives,
&mut type_var_to_concrete_def, &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, unifier: &mut Unifier,
primitives: &PrimitiveStore, primitives: &PrimitiveStore,
type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>, type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>,
core_info: (&HashSet<StrRef>, &CoreMode), core_info: (&HashSet<StrRef>, &ComposerConfig),
) -> Result<(), String> { ) -> Result<(), String> {
let (keyword_list, core_mode) = core_info; let (keyword_list, core_config) = core_info;
let mut class_def = class_def.write(); let mut class_def = class_def.write();
let ( let (
class_id, class_id,
@ -1068,27 +1071,17 @@ impl TopLevelComposer {
let dummy_field_type = unifier.get_fresh_var().0; let dummy_field_type = unifier.get_fresh_var().0;
// handle Kernel[T], KernelInvariant[T] // handle Kernel[T], KernelInvariant[T]
let (annotation, mutable) = { let (annotation, mutable) = match &annotation.node {
match core_mode { ast::ExprKind::Subscript { value, slice, .. } if matches!(
CoreMode::Artiq => match &annotation.as_ref().node { &value.node,
ast::ExprKind::Subscript { value, slice, .. } if matches!( ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into()
&value.node, ) => (slice, false),
ast::ExprKind::Name { id, .. } if id == &"Kernel".into() ast::ExprKind::Subscript { value, slice, .. } if matches!(
) => (slice, true), &value.node,
ast::ExprKind::Subscript { value, slice, .. } if matches!( ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into())
&value.node, ) => (slice, true),
ast::ExprKind::Name { id, .. } if id == &"KernelInvariant".into() _ if core_config.kernel_ann.is_none() => (annotation, true),
) => (slice, false), _ => continue // ignore fields annotated otherwise
_ => 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)
}
}
}; };
class_fields_def.push((*attr, dummy_field_type, mutable)); class_fields_def.push((*attr, dummy_field_type, mutable));

View File

@ -14,7 +14,7 @@ use nac3core::{
}, },
symbol_resolver::SymbolResolver, symbol_resolver::SymbolResolver,
toplevel::{ toplevel::{
composer::{TopLevelComposer, CoreMode}, composer::{TopLevelComposer, ComposerConfig},
TopLevelDef, helper::parse_parameter_default_value, TopLevelDef, helper::parse_parameter_default_value,
type_annotation::*, type_annotation::*,
}, },
@ -46,7 +46,7 @@ fn main() {
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0;
let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new( let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(
vec![], vec![],
CoreMode::Standalone ComposerConfig { kernel_ann: None, kernel_invariant_ann: "Invariant" }
); );
let internal_resolver: Arc<ResolverInternal> = ResolverInternal { let internal_resolver: Arc<ResolverInternal> = ResolverInternal {