Skip to content

Commit

Permalink
Refactor some duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Nov 1, 2024
1 parent c2a24f2 commit ffded1d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 187 deletions.
2 changes: 1 addition & 1 deletion kclvm/evaluator/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'ctx> Evaluator<'_> {
/// 1. scan all possible global variables and allocate undefined values to global pointers.
/// 2. build all user-defined schema/rule types.
/// 3. evaluate all codes for the third time.
pub(crate) fn compile_ast_modules(&self, modules: &Vec<Arc<RwLock<ast::Module>>>) -> ValueRef {
pub(crate) fn compile_ast_modules(&self, modules: &[Arc<RwLock<ast::Module>>]) -> ValueRef {
// Scan global variables
for ast_module in modules {
let ast_module = ast_module.read().expect("Failed to acquire module lock");
Expand Down
40 changes: 9 additions & 31 deletions kclvm/evaluator/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::sync::{Arc, RwLock};

use crate::func::ClosureMap;
use crate::lazy::merge_setters;
use crate::{
error as kcl_error, lazy::LazyEvalScope, rule::RuleEvalContextRef, schema::SchemaEvalContextRef,
};
use indexmap::{IndexMap, IndexSet};
use kclvm_ast::ast::{self, Module};
use kclvm_ast::ast;
use kclvm_ast::walker::TypedResultWalker;
use kclvm_runtime::{ValueRef, _kclvm_get_fn_ptr_by_name, MAIN_PKG_PATH};
use kclvm_sema::{builtin, plugin};
Expand Down Expand Up @@ -37,45 +39,20 @@ impl<'ctx> Evaluator<'ctx> {
let scopes = vec![Scope::default()];
pkg_scopes.insert(String::from(pkgpath), scopes);
}
let msg = format!("pkgpath {} is not found", pkgpath);
// Get the AST module list in the package path.
let module_list: Vec<ast::Module> = if self.program.pkgs.contains_key(pkgpath) {
let modules = self.program.pkgs.get(pkgpath).expect(&msg);
let modules: Vec<Module> = modules
.iter()
.map(|m| {
self.program
.get_module(m)
.expect("Failed to acquire module lock")
.expect(&format!("module {:?} not found in program", m))
.clone()
})
.collect();
modules
let module_list: Vec<Arc<RwLock<ast::Module>>> = if self.program.pkgs.contains_key(pkgpath)
{
self.program.get_modules_for_pkg(pkgpath)
} else if pkgpath.starts_with(kclvm_runtime::PKG_PATH_PREFIX)
&& self.program.pkgs.contains_key(&pkgpath[1..])
{
let modules = self
.program
.pkgs
.get(&pkgpath[1..])
.expect(kcl_error::INTERNAL_ERROR_MSG);
let modules: Vec<Module> = modules
.iter()
.map(|m| {
self.program
.get_module(m)
.expect("Failed to acquire module lock")
.expect(&format!("module {:?} not found in program", m))
.clone()
})
.collect();
modules
self.program.get_modules_for_pkg(&pkgpath[1..])
} else {
panic!("pkgpath {} not found", pkgpath);
};
// Init all global types including schema and rule.
for module in &module_list {
let module = module.read().expect("Failed to acquire module lock");
for stmt in &module.body {
let name = match &stmt.node {
ast::Stmt::Schema(schema_stmt) => schema_stmt.name.node.clone(),
Expand All @@ -100,6 +77,7 @@ impl<'ctx> Evaluator<'ctx> {
let mut setters = IndexMap::new();
for (index, module) in module_list.iter().enumerate() {
let index = self.add_global_body(index);
let module = module.read().expect("Failed to acquire module lock");
merge_setters(&mut setters, &self.emit_setters(&module.body, Some(index)))
}
if !lazy_scopes.contains_key(pkgpath) {
Expand Down
12 changes: 4 additions & 8 deletions kclvm/sema/src/advanced_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ impl<'ctx> AdvancedResolver<'ctx> {
pkg_info.kfile_paths.clone(),
);

let modules = advanced_resolver.ctx.program.get_modules_for_pkg(name);
for module in modules.iter() {
let module = program
.get_module(module)
.expect("Failed to acquire module lock")
.expect(&format!("module {:?} not found in program", module));
let module = module.read().expect("Failed to acquire module lock");
advanced_resolver.ctx.current_filename = Some(module.filename.clone());
advanced_resolver.walk_module_schemas(&module)?;
}
Expand Down Expand Up @@ -171,11 +169,9 @@ impl<'ctx> AdvancedResolver<'ctx> {
.unwrap();

advanced_resolver.ctx.scopes.push(scope_ref);
let modules = advanced_resolver.ctx.program.get_modules_for_pkg(name);
for module in modules.iter() {
let module = program
.get_module(module)
.expect("Failed to acquire module lock")
.expect(&format!("module {:?} not found in program", module));
let module = module.read().expect("Failed to acquire module lock");
advanced_resolver.ctx.current_filename = Some(module.filename.clone());
advanced_resolver.walk_module(&module)?;
}
Expand Down
7 changes: 2 additions & 5 deletions kclvm/sema/src/namer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,9 @@ impl<'ctx> Namer<'ctx> {
Some(PackageInfo::new(name.to_string(), real_path, false));
}

let modules = namer.ctx.program.get_modules_for_pkg(name);
for module in modules.iter() {
let module = program
.get_module(module)
.expect("Failed to acquire module lock")
.expect(&format!("module {:?} not found in program", module));

let module = module.read().expect("Failed to acquire module lock");
namer
.ctx
.current_package_info
Expand Down
Loading

0 comments on commit ffded1d

Please sign in to comment.