From 390005f9a8202891f4607602d438098101f5938e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 27 Aug 2021 23:19:50 +0900 Subject: [PATCH 1/2] clean up Compiler::create_qualified_name --- compiler/src/compile.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index fb09e5304..259d7666b 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -1020,9 +1020,10 @@ impl Compiler { }, }; - let qualified_name = self.create_qualified_name(name, ""); - let old_qualified_path = self.current_qualified_path.replace(qualified_name.clone()); - self.current_qualified_path = Some(self.create_qualified_name("", "")); + let old_qualified_path = self.current_qualified_path.clone(); + self.push_qualified_name(name); + let qualified_name = self.current_qualified_path.clone().unwrap(); + self.push_qualified_name(""); let (body, doc_str) = get_doc(body); @@ -1190,11 +1191,9 @@ impl Compiler { let prev_class_name = std::mem::replace(&mut self.class_name, Some(name.to_owned())); - let qualified_name = self.create_qualified_name(name, ""); - let old_qualified_path = std::mem::replace( - &mut self.current_qualified_path, - Some(qualified_name.clone()), - ); + let old_qualified_path = self.current_qualified_path.clone(); + self.push_qualified_name(name); + let qualified_name = self.current_qualified_path.clone().unwrap(); self.push_output(bytecode::CodeFlags::empty(), 0, 0, 0, name.to_owned()); @@ -2466,11 +2465,12 @@ impl Compiler { self.current_source_location.row() } - fn create_qualified_name(&self, name: &str, suffix: &str) -> String { - if let Some(ref qualified_path) = self.current_qualified_path { - format!("{}.{}{}", qualified_path, name, suffix) + fn push_qualified_name(&mut self, name: &str) { + if let Some(ref mut qualified_path) = self.current_qualified_path { + qualified_path.push('.'); + qualified_path.push_str(name); } else { - format!("{}{}", name, suffix) + self.current_qualified_path = Some(name.to_owned()); } } From 93b91bab2d0855f7ef15b3714956248ea374d8c3 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 27 Aug 2021 23:45:47 +0900 Subject: [PATCH 2/2] Compiler::qualified_name as vec --- compiler/src/compile.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index 259d7666b..beded10cf 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -56,7 +56,7 @@ struct Compiler { symbol_table_stack: Vec, source_path: String, current_source_location: ast::Location, - current_qualified_path: Option, + qualified_path: Vec, done_with_future_stmts: bool, ctx: CompileContext, class_name: Option, @@ -190,7 +190,7 @@ impl Compiler { symbol_table_stack: Vec::new(), source_path, current_source_location: ast::Location::default(), - current_qualified_path: None, + qualified_path: Vec::new(), done_with_future_stmts: false, ctx: CompileContext { loop_data: None, @@ -1020,10 +1020,9 @@ impl Compiler { }, }; - let old_qualified_path = self.current_qualified_path.clone(); - self.push_qualified_name(name); - let qualified_name = self.current_qualified_path.clone().unwrap(); - self.push_qualified_name(""); + self.push_qualified_path(name); + let qualified_name = self.qualified_path.join("."); + self.push_qualified_path(""); let (body, doc_str) = get_doc(body); @@ -1041,7 +1040,8 @@ impl Compiler { } let code = self.pop_code_object(); - self.current_qualified_path = old_qualified_path; + self.qualified_path.pop(); + self.qualified_path.pop(); self.ctx = prev_ctx; // Prepare type annotations: @@ -1191,9 +1191,8 @@ impl Compiler { let prev_class_name = std::mem::replace(&mut self.class_name, Some(name.to_owned())); - let old_qualified_path = self.current_qualified_path.clone(); - self.push_qualified_name(name); - let qualified_name = self.current_qualified_path.clone().unwrap(); + self.push_qualified_path(name); + let qualified_name = self.qualified_path.join("."); self.push_output(bytecode::CodeFlags::empty(), 0, 0, 0, name.to_owned()); @@ -1239,7 +1238,7 @@ impl Compiler { let code = self.pop_code_object(); self.class_name = prev_class_name; - self.current_qualified_path = old_qualified_path; + self.qualified_path.pop(); self.ctx = prev_ctx; let mut funcflags = bytecode::MakeFunctionFlags::empty(); @@ -2465,13 +2464,8 @@ impl Compiler { self.current_source_location.row() } - fn push_qualified_name(&mut self, name: &str) { - if let Some(ref mut qualified_path) = self.current_qualified_path { - qualified_path.push('.'); - qualified_path.push_str(name); - } else { - self.current_qualified_path = Some(name.to_owned()); - } + fn push_qualified_path(&mut self, name: &str) { + self.qualified_path.push(name.to_owned()); } fn mark_generator(&mut self) {