adding package class

This commit is contained in:
lyon1998 2021-10-18 10:52:42 +08:00
parent cf480c2b85
commit d6ebc6b11e
5 changed files with 30 additions and 10 deletions

View File

@ -15,7 +15,10 @@ pub struct ClassInfo {
}
impl ClassInfo {
pub fn add_file_profix(file_name: &String, class_name: &String) -> String {
pub fn add_file_profix(file_name: &String, class_name: &String, isPackage: bool) -> String {
if isPackage {
return class_name.clone();
}
if file_name == "main" {
return class_name.clone();
} else if class_name == "BaseObj" || class_name == "TinyObj" {
@ -25,7 +28,7 @@ impl ClassInfo {
}
}
pub fn new(file_name: &String, define: &String) -> Option<ClassInfo> {
pub fn new(file_name: &String, define: &String, is_package: bool) -> Option<ClassInfo> {
let define = define.strip_prefix("class ").unwrap().to_string();
let define = define.replace(" ", "");
let super_class_name = match my_string::cut(&define, '(', ')') {
@ -33,14 +36,14 @@ impl ClassInfo {
None => return None,
};
let super_class_name = match super_class_name.find(".") {
None => ClassInfo::add_file_profix(&file_name, &super_class_name),
None => ClassInfo::add_file_profix(&file_name, &super_class_name, is_package),
Some(x) => super_class_name.replace(".", "_"),
};
let mut this_calss_name = match my_string::get_first_token(&define, '(') {
Some(s) => s,
None => return None,
};
this_calss_name = ClassInfo::add_file_profix(&file_name, &this_calss_name);
this_calss_name = ClassInfo::add_file_profix(&file_name, &this_calss_name, false);
let new_class_info = ClassInfo {
this_class_name: this_calss_name,
super_class_name: super_class_name,
@ -161,7 +164,8 @@ mod tests {
assert_eq!(
ClassInfo::new(
&String::from("Pkg"),
&String::from("class Test(SuperTest):")
&String::from("class Test(SuperTest):"),
false
)
.unwrap()
.this_class_name,
@ -170,7 +174,8 @@ mod tests {
assert_eq!(
ClassInfo::new(
&String::from("Pkg"),
&String::from("class Test(SuperTest):")
&String::from("class Test(SuperTest):"),
false
)
.unwrap()
.super_class_name,
@ -182,6 +187,7 @@ mod tests {
let mut class_info = ClassInfo::new(
&String::from("Pkg"),
&String::from("class Test(SuperTest):"),
false,
)
.unwrap();
class_info.push_method(String::from("def test(data: str)-> str:"));
@ -220,6 +226,7 @@ mod tests {
let mut class_info = ClassInfo::new(
&String::from("Pkg"),
&String::from("class Test(SuperTest):"),
false,
)
.unwrap();
class_info.push_object(String::from("testObj = TestObj()"), &"Pkg".to_string());
@ -249,6 +256,7 @@ mod tests {
let mut class_info = ClassInfo::new(
&String::from("Pkg"),
&String::from("class Test(SuperTest):"),
false,
)
.unwrap();
class_info.push_import(String::from("TestObj()"), &"Pkg".to_string());

View File

@ -9,6 +9,8 @@ pub struct Compiler {
pub source_path: String,
pub class_list: BTreeMap<String, ClassInfo>,
pub class_now_name: Option<String>,
pub package_list: BTreeMap<String, ClassInfo>,
pub package_now_name: Option<String>,
}
impl Compiler {
@ -18,6 +20,8 @@ impl Compiler {
source_path: source_path.clone(),
class_now_name: None,
class_list: BTreeMap::new(),
package_list: BTreeMap::new(),
package_now_name: None,
};
return compiler;
}
@ -30,6 +34,7 @@ impl Compiler {
ClassInfo::new(
&file_name,
&"class PikaMain(PikaStdLib.SysObj):".to_string(),
false,
)
.unwrap(),
),
@ -49,6 +54,13 @@ impl Compiler {
pub fn analyze_file(mut compiler: Compiler, file_name: String) -> Compiler {
println!("analyzing file: {}{}.py", compiler.source_path, file_name);
let mut file = File::open(format!("{}{}.py", compiler.source_path, file_name)).unwrap();
if file_name != "main" {
let pkg_define = format!("class {}(TinyObj):", &file_name);
let pacakge_now = match ClassInfo::new(&String::from(""), &pkg_define, true) {
Some(s) => s,
None => return compiler,
};
}
let mut file_str = String::new();
file.read_to_string(&mut file_str).unwrap();
let lines: Vec<&str> = file_str.split('\n').collect();
@ -76,7 +88,7 @@ impl Compiler {
}
if line.starts_with("class") {
let class_now = match ClassInfo::new(&file_name, &line) {
let class_now = match ClassInfo::new(&file_name, &line, false) {
Some(s) => s,
None => return compiler,
};

View File

@ -1,5 +1,5 @@
use crate::my_string;
use crate::class_info;
use crate::my_string;
#[derive(Debug)]
pub struct ImportInfo {
pub class_name: String,
@ -18,7 +18,7 @@ impl ImportInfo {
None => return None,
};
import_class_name = match import_class_name.find(".") {
None => class_info::ClassInfo::add_file_profix(&file_name, &import_class_name),
None => class_info::ClassInfo::add_file_profix(&file_name, &import_class_name, false),
Some(x) => import_class_name.replace(".", "_"),
};
import_class_name = import_class_name.replace(".", "_");

View File

@ -23,7 +23,7 @@ impl ObjectInfo {
None => return None,
};
import_class_name = match import_class_name.find(".") {
None => class_info::ClassInfo::add_file_profix(&file_name, &import_class_name),
None => class_info::ClassInfo::add_file_profix(&file_name, &import_class_name, false),
Some(x) => import_class_name.replace(".", "_"),
};
return Some(ObjectInfo {