From 3ffb28afd6574f249c1e3ac2a784990e0e4cb7dc Mon Sep 17 00:00:00 2001 From: lyon Date: Mon, 10 Jan 2022 21:14:29 +0800 Subject: [PATCH] support " and void line in main.py --- port/linux/package/pikascript/main.py | 2 ++ src/PikaPlatform.h | 2 +- tools/pikaCompiler/main.py | 5 +++-- tools/pikaCompiler/src/class_info.rs | 19 +++++++++++++++++-- tools/pikaCompiler/src/main.rs | 11 +++++++++-- tools/pikaCompiler/src/script.rs | 2 +- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/port/linux/package/pikascript/main.py b/port/linux/package/pikascript/main.py index 20e176cf9..ed4276361 100644 --- a/port/linux/package/pikascript/main.py +++ b/port/linux/package/pikascript/main.py @@ -4,5 +4,7 @@ import PikaStdData import PikaMath from PikaObj import * +print("hello pikascript!\n") for i in range(0, 100): + print(i) diff --git a/src/PikaPlatform.h b/src/PikaPlatform.h index 170902630..7f059ccbc 100644 --- a/src/PikaPlatform.h +++ b/src/PikaPlatform.h @@ -45,7 +45,7 @@ #endif /* default PIKA_WEAK */ #ifndef PIKA_WEAK -#define PIKA_WEAK +#define PIKA_WEAK __attribute__((weak)) #endif /* OS */ diff --git a/tools/pikaCompiler/main.py b/tools/pikaCompiler/main.py index 138b88dc0..e63299d73 100644 --- a/tools/pikaCompiler/main.py +++ b/tools/pikaCompiler/main.py @@ -9,9 +9,10 @@ line.moveTo(20) while True: if right.read() == 1: print('right') + if left.read() == 0: - print('left') + print("left") if up.read() == 0: - print('up') + print('up\n') if down.read() == 0: print('down') diff --git a/tools/pikaCompiler/src/class_info.rs b/tools/pikaCompiler/src/class_info.rs index 2b0d2718b..c6c322a64 100644 --- a/tools/pikaCompiler/src/class_info.rs +++ b/tools/pikaCompiler/src/class_info.rs @@ -113,10 +113,25 @@ impl ClassInfo { pub fn script_fn(&self) -> String { let mut script_fn = String::new(); - script_fn.push_str("PikaObj * pikaScriptInit(){\r\n"); + /* add pikaScriptInit function define */ + script_fn.push_str("PikaObj * pikaScriptInit(void){\r\n"); + /* create the root object */ script_fn.push_str(" PikaObj * pikaMain = newRootObj(\"pikaMain\", New_PikaMain);\r\n"); + /* use obj_run to run the script in main.py */ script_fn.push_str(" obj_run(pikaMain,\n"); - script_fn.push_str(&self.script_list.content); + /* get the origin script content */ + let script_content_origin = String::from(&self.script_list.content); + /* filters for the script content */ + /* remove void line */ + let script_content = String::from(&script_content_origin).replace("@BEGIN@@END@", ""); + /* use \" instead of " */ + let script_content = script_content.replace("\"", "\\\""); + /* add begin and end */ + let script_content = script_content.replace("@BEGIN@", " \""); + let script_content = script_content.replace("@END@", "\\n\"\n"); + /* add the script content */ + script_fn.push_str(&script_content); + /* add the END of script string */ script_fn.push_str(" \"\\n\");\n"); script_fn.push_str(" return pikaMain;\r\n"); script_fn.push_str("}\r\n\r\n"); diff --git a/tools/pikaCompiler/src/main.rs b/tools/pikaCompiler/src/main.rs index d341def7a..5df2d763a 100644 --- a/tools/pikaCompiler/src/main.rs +++ b/tools/pikaCompiler/src/main.rs @@ -13,9 +13,11 @@ use std::fs::File; use std::io::prelude::*; 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); - /* write to compiler-info about the info */ + /* write the infomatrion to compiler-info */ let mut compiler_info_file = File::create(format!("{}compiler-info.txt", compiler.dist_path)).unwrap(); let compiler_info = format!("{:?}", compiler); @@ -82,21 +84,26 @@ fn main() { /* make the pikascript.c */ let api_file_path = format!("{}pikaScript.c", compiler.dist_path); let mut f = File::create(api_file_path).unwrap(); + /* add head */ f.write("/* ******************************** */\n".as_bytes()) .unwrap(); f.write("/* Warning! Don't modify this file! */\n".as_bytes()) .unwrap(); f.write("/* ******************************** */\n".as_bytes()) .unwrap(); + /* add include */ f.write("#include \"PikaMain.h\"\n".as_bytes()).unwrap(); f.write("#include \n".as_bytes()).unwrap(); f.write("#include \n".as_bytes()).unwrap(); f.write("\n".as_bytes()).unwrap(); + /* get script from main.py */ let pika_main = compiler .class_list .get_mut(&"PikaMain".to_string()) .unwrap(); + /* add script function */ f.write(pika_main.script_fn().as_bytes()).unwrap(); + /* make the pikascript.h */ let api_file_path = format!("{}pikaScript.h", compiler.dist_path); let mut f = File::create(api_file_path).unwrap(); @@ -115,7 +122,7 @@ fn main() { f.write("#include \"PikaObj.h\"\n".as_bytes()).unwrap(); f.write("#include \"PikaMain.h\"\n".as_bytes()).unwrap(); f.write("\n".as_bytes()).unwrap(); - f.write("PikaObj * pikaScriptInit();\n".as_bytes()).unwrap(); + f.write("PikaObj * pikaScriptInit(void);\n".as_bytes()).unwrap(); f.write("\n".as_bytes()).unwrap(); f.write("#endif\n".as_bytes()).unwrap(); } diff --git a/tools/pikaCompiler/src/script.rs b/tools/pikaCompiler/src/script.rs index f7956ff29..168f8b564 100644 --- a/tools/pikaCompiler/src/script.rs +++ b/tools/pikaCompiler/src/script.rs @@ -30,6 +30,6 @@ impl Script { return false; } pub fn obj_run(cmd: &String) -> String { - return format!(" \"{}\\n\"\n", cmd); + return format!("@BEGIN@{}@END@", cmd); } }