From 33e2a02caf45fb78bd3185b20989a483edff6e5a Mon Sep 17 00:00:00 2001 From: pikastech Date: Wed, 24 Aug 2022 13:10:38 +0800 Subject: [PATCH] add decorator.rs --- tools/pikaCompiler/src/decorator.rs | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tools/pikaCompiler/src/decorator.rs diff --git a/tools/pikaCompiler/src/decorator.rs b/tools/pikaCompiler/src/decorator.rs new file mode 100644 index 000000000..37529a6bb --- /dev/null +++ b/tools/pikaCompiler/src/decorator.rs @@ -0,0 +1,48 @@ +use crate::my_string; +#[derive(Debug, Clone)] +pub struct Decorator { + pub name: String, + pub arg: String, +} + +impl Decorator { + pub fn new(stmt: String) -> Option { + // PIKA_C_MACRO_IF("MACRO_STMT") or PIKA_C_MACRO_IF('MACRO_STMT') + let name = match my_string::get_first_token(&stmt, '(') { + Some(name) => name, + None => return None, + }; + let arg_str = match my_string::cut(&stmt, '(', ')') { + Some(arg) => arg, + None => return None, + }; + + let arg = arg_str.replace("\"", "").replace("'", "").replace(" ", ""); + + return Some(Decorator { name, arg }); + } + + pub fn gen_before(&self) -> String { + if self.name == "PIKA_C_MACRO_IF" { + return format!("#if {}\n", self.arg); + } + + if self.name == "PIKA_C_MACRO_IFDEF" { + return format!("#ifdef {}\n", self.arg); + } + + return String::from(""); + } + + pub fn gen_after(&self) -> String { + if self.name == "PIKA_C_MACRO_IF" { + return String::from("#endif\n"); + } + + if self.name == "PIKA_C_MACRO_IFDEF" { + return String::from("#endif\n"); + } + + return String::from(""); + } +}