From 71c3923f43f473f777e74dbc6bd086a87a092682 Mon Sep 17 00:00:00 2001 From: pikastech Date: Wed, 13 Jul 2022 10:26:02 +0800 Subject: [PATCH] support class XXX: and class XXX(): --- tools/pikaCompiler/PyInfo.pyi | 4 ++-- tools/pikaCompiler/main.py | 1 + tools/pikaCompiler/src/class_info.rs | 14 +++++++++++--- tools/pikaCompiler/src/compiler.rs | 14 +++++++------- tools/pikaCompiler/src/entry.rs | 7 ++++--- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/tools/pikaCompiler/PyInfo.pyi b/tools/pikaCompiler/PyInfo.pyi index 38aff5bf0..c946177f3 100644 --- a/tools/pikaCompiler/PyInfo.pyi +++ b/tools/pikaCompiler/PyInfo.pyi @@ -1,5 +1,5 @@ #api -class PyObj(BaseObj): +class PyObj: def importClass(self, className: str, fp: str): pass @@ -17,7 +17,7 @@ class PyObj(BaseObj): pass -class PyMethod (SysObj): +class PyMethod ( ): def makeMethodFun(fp: str): pass diff --git a/tools/pikaCompiler/main.py b/tools/pikaCompiler/main.py index 7ae1972ce..0be21ccb6 100644 --- a/tools/pikaCompiler/main.py +++ b/tools/pikaCompiler/main.py @@ -1,4 +1,5 @@ import PikaStdLib +import PyInfo from pika_cjson import cJSON print('hello pikascript!') mem = PikaStdLib.MemChecker() diff --git a/tools/pikaCompiler/src/class_info.rs b/tools/pikaCompiler/src/class_info.rs index f7f83785f..db2d21784 100644 --- a/tools/pikaCompiler/src/class_info.rs +++ b/tools/pikaCompiler/src/class_info.rs @@ -34,17 +34,25 @@ impl ClassInfo { pub fn new(file_name: &String, define: &String, is_package: bool) -> Option { let define = define.strip_prefix("class ").unwrap().to_string(); let define = define.replace(" ", ""); - let super_class_name = match my_string::cut(&define, '(', ')') { + let mut super_class_name = match my_string::cut(&define, '(', ')') { Some(s) => s, - None => return None, + None => "TinyObj".to_string(), }; + if super_class_name == "" { + super_class_name = "TinyObj".to_string(); + } let super_class_name = match super_class_name.find(".") { 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, + None => match my_string::get_first_token(&define, ':') { + Some(s) => s, + None => { + return None; + } + }, }; let this_class_name_without_file = this_calss_name.clone(); this_calss_name = ClassInfo::add_file_profix(&file_name, &this_calss_name, is_package); diff --git a/tools/pikaCompiler/src/compiler.rs b/tools/pikaCompiler/src/compiler.rs index 492792d5c..47cb6e24c 100644 --- a/tools/pikaCompiler/src/compiler.rs +++ b/tools/pikaCompiler/src/compiler.rs @@ -84,7 +84,7 @@ impl Compiler { let package_obj_define = format!("{} = {}()", package_name, package_name); class_now.push_object(package_obj_define, &file_name); - return Compiler::__do_analyse_file(compiler, package_name.to_string(), true); + return Compiler::analyse_top_package(compiler, package_name.to_string()); } class_now.script_list.add(&line); return compiler; @@ -197,13 +197,13 @@ impl Compiler { if file_name == "main" { self = Compiler::analyse_main_line(self, &line); } else { - self = Compiler::analyse_line(self, line.to_string(), &file_name, is_top_pkg); + self = Compiler::analyse_pyi_line(self, line.to_string(), &file_name, is_top_pkg); } } return self; } - pub fn analyse_line( + pub fn analyse_pyi_line( mut compiler: Compiler, line: String, file_name: &String, @@ -212,7 +212,7 @@ impl Compiler { if line.starts_with("import ") { let tokens: Vec<&str> = line.split(" ").collect(); let file = tokens[1]; - return Compiler::__do_analyse_file(compiler, file.to_string(), false); + return Compiler::analyse_inner_package(compiler, file.to_string()); } if line.starts_with("#") { @@ -299,19 +299,19 @@ mod tests { #[test] fn test_analyse() { let compiler = Compiler::new(String::from(""), String::from("")); - let compiler = Compiler::analyse_line( + let compiler = Compiler::analyse_pyi_line( compiler, String::from("class Test(SuperTest):"), &"Pkg".to_string(), false, ); - let compiler = Compiler::analyse_line( + let compiler = Compiler::analyse_pyi_line( compiler, String::from(" def test()"), &"Pkg".to_string(), false, ); - let compiler = Compiler::analyse_line( + let compiler = Compiler::analyse_pyi_line( compiler, String::from(" testObj = TestObj()"), &"Pkg".to_string(), diff --git a/tools/pikaCompiler/src/entry.rs b/tools/pikaCompiler/src/entry.rs index fd11d2b85..527e72bf5 100644 --- a/tools/pikaCompiler/src/entry.rs +++ b/tools/pikaCompiler/src/entry.rs @@ -15,17 +15,18 @@ pub fn pika_compiler_entry() { let mut compiler = Compiler::new(String::from(""), String::from("pikascript-api/")); /* analyse file begin with main.py */ - compiler = Compiler::analyse_inner_package(compiler, String::from("main")); + compiler = Compiler::analyse_top_package(compiler, String::from("main")); /* Compile packages in requestment.txt, solve the packages as the top packages. */ for package in &version_info.package_list { + let package_name = package.0; /* skip pikascript-core */ - if package.0 == "pikascript-core" { + if package_name == "pikascript-core" { continue; } - compiler = Compiler::analyse_top_package(compiler, String::from(package.0)); + compiler = Compiler::analyse_top_package(compiler, String::from(package_name)); } /* Compile packages in PikaStdLib */