From 1300b5ebdd7330205fc3df28abb0ef9a574f4bbe Mon Sep 17 00:00:00 2001 From: ychenfo Date: Wed, 8 Sep 2021 19:45:36 +0800 Subject: [PATCH] nac3core: clean up and format --- nac3core/src/codegen/expr.rs | 3 +-- nac3core/src/codegen/mod.rs | 5 ++--- nac3core/src/codegen/test.rs | 2 +- nac3core/src/toplevel/mod.rs | 19 +++++++++++++------ nac3core/src/toplevel/test.rs | 15 +++++++++------ nac3core/src/toplevel/type_annotation.rs | 19 ++++++------------- nac3core/src/typecheck/type_inferencer/mod.rs | 18 ++++++++++-------- 7 files changed, 42 insertions(+), 39 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 91656ec5c..013785e5f 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -609,8 +609,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { ExprKind::Call { func, args, keywords } => { if let ExprKind::Name { id, .. } = &func.as_ref().node { // TODO: handle primitive casts and function pointers - let fun = - self.resolver.get_identifier_def(&id).expect("Unknown identifier"); + let fun = self.resolver.get_identifier_def(&id).expect("Unknown identifier"); let mut params = args.iter().map(|arg| (None, self.gen_expr(arg).unwrap())).collect_vec(); let kw_iter = keywords.iter().map(|kw| { diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index ccedbe5de..f690c6bf0 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -305,9 +305,8 @@ pub fn gen_func<'ctx>( }; let symbol = &task.symbol_name; - let fn_val = module - .get_function(symbol) - .unwrap_or_else(|| module.add_function(symbol, fn_type, None)); + let fn_val = + module.get_function(symbol).unwrap_or_else(|| module.add_function(symbol, fn_type, None)); let init_bb = context.append_basic_block(fn_val, "init"); builder.position_at_end(init_bb); diff --git a/nac3core/src/codegen/test.rs b/nac3core/src/codegen/test.rs index 4d6d99eb1..b6452b959 100644 --- a/nac3core/src/codegen/test.rs +++ b/nac3core/src/codegen/test.rs @@ -9,7 +9,7 @@ use crate::{ }, }; use indoc::indoc; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use rustpython_parser::{ast::fold::Fold, parser::parse_program}; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index 2cae75605..9b26f2ed5 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -1,4 +1,11 @@ -use std::{borrow::BorrowMut, collections::{HashMap, HashSet}, fmt::Debug, iter::FromIterator, ops::{Deref, DerefMut}, sync::Arc}; +use std::{ + borrow::BorrowMut, + collections::{HashMap, HashSet}, + fmt::Debug, + iter::FromIterator, + ops::{Deref, DerefMut}, + sync::Arc, +}; use super::typecheck::type_inferencer::PrimitiveStore; use super::typecheck::typedef::{FunSignature, FuncArg, SharedUnifier, Type, TypeEnum, Unifier}; @@ -7,7 +14,7 @@ use crate::{ typecheck::{type_inferencer::CodeLocation, typedef::CallId}, }; use itertools::{izip, Itertools}; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use rustpython_parser::ast::{self, Stmt}; #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)] @@ -588,7 +595,7 @@ impl TopLevelComposer { println!( "{:?}_{} -> {:?}\n", ty, - unifier.stringify(ty, + unifier.stringify(ty, &mut |id| format!("class{}", id), &mut |id| format!("tvar{}", id) ), @@ -599,17 +606,17 @@ impl TopLevelComposer { unifier.unify(ty, target_ty)?; ddddd.push((ty, target_ty)); } - + for (ty, tar_ty) in ddddd { println!( "{:?}_{} -> {:?}_{}", ty, - unifier.stringify(ty, + unifier.stringify(ty, &mut |id| format!("class{}", id), &mut |id| format!("tvar{}", id) ), tar_ty, - unifier.stringify(tar_ty, + unifier.stringify(tar_ty, &mut |id| format!("class{}", id), &mut |id| format!("tvar{}", id) ), diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index 4337ef9a2..e4196bbe9 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -45,7 +45,6 @@ impl SymbolResolver for Resolver { fn get_identifier_def(&self, id: &str) -> Option { self.0.id_to_def.lock().get(id).cloned() } - } #[test_case( @@ -126,7 +125,9 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s id_to_type: Default::default(), class_names: Default::default(), }); - let resolver = Arc::new(Box::new(Resolver(internal_resolver.clone())) as Box); + let resolver = Arc::new( + Box::new(Resolver(internal_resolver.clone())) as Box + ); for s in source { let ast = parse_program(s).unwrap(); @@ -279,12 +280,14 @@ fn test_simple_class_analyze(source: Vec<&str>, res: Vec<&str>) { let internal_resolver = Arc::new(ResolverInternal { id_to_def: Default::default(), - id_to_type: Mutex::new(vec![("T".to_string(), tvar_t.0), ("V".to_string(), tvar_v.0)] - .into_iter() - .collect()), + id_to_type: Mutex::new( + vec![("T".to_string(), tvar_t.0), ("V".to_string(), tvar_v.0)].into_iter().collect(), + ), class_names: Default::default(), }); - let resolver = Arc::new(Box::new(Resolver(internal_resolver.clone())) as Box); + let resolver = Arc::new( + Box::new(Resolver(internal_resolver.clone())) as Box + ); for s in source { let ast = parse_program(s).unwrap(); diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index 291937fc8..7bb02f0b7 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -35,12 +35,7 @@ pub fn parse_ast_to_type_annotation_kinds( "bool" => Ok(TypeAnnotation::PrimitiveKind(primitives.bool)), "None" => Ok(TypeAnnotation::PrimitiveKind(primitives.none)), x => { - if let Some(obj_id) = { - // write this way because the lock in the if/let construct lives - // for the whole if let construct - let id = resolver.get_identifier_def(x); - id - } { + if let Some(obj_id) = resolver.get_identifier_def(x) { let def = top_level_defs[obj_id.0].read(); if let TopLevelDef::Class { type_vars, .. } = &*def { // also check param number here @@ -54,10 +49,7 @@ pub fn parse_ast_to_type_annotation_kinds( } else { Err("function cannot be used as a type".into()) } - } else if let Some(ty) = { - let ty = resolver.get_symbol_type(unifier, primitives, id); - ty - } { + } else if let Some(ty) = resolver.get_symbol_type(unifier, primitives, id) { if let TypeEnum::TVar { .. } = unifier.get_ty(ty).as_ref() { Ok(TypeAnnotation::TypeVarKind(ty)) } else { @@ -240,15 +232,16 @@ pub fn get_type_from_type_annotation_kinds( })); println!("tobj_fields: {:?}", tobj_fields); - println!("{:?}: {}\n", + println!( + "{:?}: {}\n", tobj_fields.get("__init__").unwrap(), unifier.stringify( - *tobj_fields.get("__init__").unwrap(), + *tobj_fields.get("__init__").unwrap(), &mut |id| format!("class{}", id), &mut |id| format!("tvar{}", id) ) ); - + Ok(unifier.add_ty(TypeEnum::TObj { obj_id: *id, //fields: RefCell::new(tobj_fields), diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index 57e19b009..6959b700a 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -7,7 +7,6 @@ use super::typedef::{Call, FunSignature, FuncArg, Type, TypeEnum, Unifier}; use super::{magic_methods::*, typedef::CallId}; use crate::{symbol_resolver::SymbolResolver, toplevel::TopLevelContext}; use itertools::izip; -use parking_lot::Mutex; use rustpython_parser::ast::{ self, fold::{self, Fold}, @@ -164,8 +163,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { ast::ExprKind::Constant { value, .. } => Some(self.infer_constant(value)?), ast::ExprKind::Name { id, .. } => { if !self.defined_identifiers.contains(id) { - if self.function_data.resolver.get_identifier_def(id.as_str()).is_some() - { + if self.function_data.resolver.get_identifier_def(id.as_str()).is_some() { self.defined_identifiers.insert(id.clone()); } else { return Err(format!( @@ -485,11 +483,15 @@ impl<'a> Inferencer<'a> { } else { let variable_mapping = &mut self.variable_mapping; let unifier = &mut self.unifier; - Ok(self.function_data.resolver.get_symbol_type(unifier, self.primitives, id).unwrap_or_else(|| { - let ty = unifier.get_fresh_var().0; - variable_mapping.insert(id.to_string(), ty); - ty - })) + Ok(self + .function_data + .resolver + .get_symbol_type(unifier, self.primitives, id) + .unwrap_or_else(|| { + let ty = unifier.get_fresh_var().0; + variable_mapping.insert(id.to_string(), ty); + ty + })) } }