From 5bbb1ba948e210f9c85cf0f03f61221b1733f35c Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Mon, 18 Apr 2022 18:05:34 +0800 Subject: [PATCH] only compile once --- port/linux/package/pikascript/ctypes.py | 2 +- tools/pikaCompiler/src/compiler.rs | 38 +++++++++++++++---------- tools/pikaCompiler/src/main.rs | 11 +++---- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/port/linux/package/pikascript/ctypes.py b/port/linux/package/pikascript/ctypes.py index 2a9ba54ce..4d25e7e3d 100644 --- a/port/linux/package/pikascript/ctypes.py +++ b/port/linux/package/pikascript/ctypes.py @@ -32,4 +32,4 @@ class c_longdouble(c_float):... class Test(TinyObj): def add(self, c_uint1:c_uint, c_uint2:c_uint)->int:... - def dc_cpuapdu_hex(self, slen:int, sendbuf:str, rlen:c_uint, rcvbuf:c_wchar_p) -> int:... + def dc_cpuapdu_hex(self, slen:int, sendbuf:str, rlen:c_uint, rcvbuf:c_char_p) -> int:... diff --git a/tools/pikaCompiler/src/compiler.rs b/tools/pikaCompiler/src/compiler.rs index d249105b6..6f00d11a5 100644 --- a/tools/pikaCompiler/src/compiler.rs +++ b/tools/pikaCompiler/src/compiler.rs @@ -2,6 +2,7 @@ use crate::class_info::ClassInfo; use crate::my_string; // use crate::script::Script; use std::collections::BTreeMap; +use std::collections::LinkedList; use std::fs; use std::fs::File; use std::io; @@ -13,6 +14,7 @@ pub struct Compiler { pub class_list: BTreeMap, pub class_now_name: Option, pub package_now_name: Option, + pub compiled_list: LinkedList, } impl Compiler { @@ -23,10 +25,11 @@ impl Compiler { class_now_name: None, class_list: BTreeMap::new(), package_now_name: None, + compiled_list: LinkedList::new(), }; return compiler; } - pub fn analyze_main_line(mut compiler: Compiler, line: &String) -> Compiler { + pub fn analize_main_line(mut compiler: Compiler, line: &String) -> Compiler { let file_name = "main".to_string(); let class_name = "PikaMain".to_string(); /* get class now or create one */ @@ -50,7 +53,7 @@ impl Compiler { let package_name = my_string::get_last_token(&line, ' ').unwrap(); let package_obj_define = format!("{} = {}()", package_name, package_name); class_now.push_object(package_obj_define, &file_name); - return Compiler::analyze_file(compiler, package_name.to_string(), true); + return Compiler::analize_file(compiler, package_name.to_string(), true); } class_now.script_list.add(&line); return compiler; @@ -69,17 +72,23 @@ impl Compiler { return Err(std::io::Error::from(std::io::ErrorKind::NotFound)); } - pub fn analyze_file(mut compiler: Compiler, file_name: String, is_top_pkg: bool) -> Compiler { - println!(" compiling {}{}.py...", compiler.source_path, file_name); - let file = Compiler::open_file(format!("{}{}.py", compiler.source_path, file_name)); + pub fn analize_file(mut self: Compiler, file_name: String, is_top_pkg: bool) -> Compiler { + /* check if compiled */ + if self.compiled_list.contains(&file_name) { + return self; + } + self.compiled_list.push_back(String::clone(&file_name)); + /* print info */ + println!(" compiling {}{}.py...", self.source_path, file_name); + let file = Compiler::open_file(format!("{}{}.py", self.source_path, file_name)); let mut file = match file { Ok(file) => file, Err(_) => { println!( " [warning]: file: '{}{}.py' no found", - compiler.source_path, file_name + self.source_path, file_name ); - return compiler; + return self; } }; /* solve top package. @@ -96,14 +105,13 @@ impl Compiler { 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, + None => return self, }; let package_name = pacakge_now.this_class_name.clone(); - compiler - .class_list + self.class_list .entry(package_name.clone()) .or_insert(pacakge_now); - compiler.package_now_name = Some(package_name.clone()); + self.package_now_name = Some(package_name.clone()); } /* solve lines in file */ let mut file_str = String::new(); @@ -111,9 +119,9 @@ impl Compiler { let lines: Vec<&str> = file_str.split('\n').collect(); /* analyze each line of pikascript-api.py */ for line in lines.iter() { - compiler = Compiler::analyze_line(compiler, line.to_string(), &file_name, is_top_pkg); + self = Compiler::analyze_line(self, line.to_string(), &file_name, is_top_pkg); } - return compiler; + return self; } pub fn analyze_line( @@ -124,13 +132,13 @@ impl Compiler { ) -> Compiler { let line = line.replace("\r", ""); if file_name == "main" { - return Compiler::analyze_main_line(compiler, &line); + return Compiler::analize_main_line(compiler, &line); } if line.starts_with("import ") { let tokens: Vec<&str> = line.split(" ").collect(); let file = tokens[1]; - return Compiler::analyze_file(compiler, file.to_string(), false); + return Compiler::analize_file(compiler, file.to_string(), false); } if line.starts_with("#") { diff --git a/tools/pikaCompiler/src/main.rs b/tools/pikaCompiler/src/main.rs index b30568c8c..cc0203200 100644 --- a/tools/pikaCompiler/src/main.rs +++ b/tools/pikaCompiler/src/main.rs @@ -25,22 +25,23 @@ fn main() { /* new a compiler, sellect to path */ let mut compiler = Compiler::new(String::from(""), String::from("pikascript-api/")); /* analyze file begin with main.py */ - compiler = Compiler::analyze_file(compiler, String::from("main"), false); + compiler = Compiler::analize_file(compiler, String::from("main"), false); /* Compile packages in requestment.txt, solve the packages as the top packages. */ for package in &version_info.package_list { + /* skip pikascript-core */ if package.0 == "pikascript-core" { continue; } - compiler = Compiler::analyze_file(compiler, String::from(package.0), true); + compiler = Compiler::analize_file(compiler, String::from(package.0), true); } /* Compile packages in PikaStdLib */ - compiler = Compiler::analyze_file(compiler, String::from("PikaStdTask"), true); - compiler = Compiler::analyze_file(compiler, String::from("PikaStdData"), true); - compiler = Compiler::analyze_file(compiler, String::from("PikaDebug"), true); + compiler = Compiler::analize_file(compiler, String::from("PikaStdTask"), true); + compiler = Compiler::analize_file(compiler, String::from("PikaStdData"), true); + compiler = Compiler::analize_file(compiler, String::from("PikaDebug"), true); println!();