From 2be0aec866983e7bf2cd23ffa3f684c29624663b Mon Sep 17 00:00:00 2001 From: mauro-balades Date: Tue, 2 May 2023 20:04:21 +0200 Subject: [PATCH] Fixed strings, message command and exec() --- Cargo.toml | 6 ++++++ src/functions/mod.rs | 12 ++++++++++-- src/parser.lalrpop | 2 +- src/runner.rs | 34 +++++++++++++++++++++------------- test/project.breeze | 3 ++- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b872dad..d698e29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,9 @@ lalrpop-util = {version = "0.19.10", features = ["lexer"]} lazy_static = "1.4.0" regex = "1" subprocess = "0.2.9" + +[build] +rustflags = ["-C", "prefer-dynamic", "-C", "rpath"] + +[lib] +crate-type = ["dylib", "rlib"] \ No newline at end of file diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 55aec08..af5e185 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -16,15 +16,19 @@ pub fn exec(argv: HashMap, args: Args, runner: &mut Runner) -> ( warn!(label: "[Debug]:", "Executing '{}' as a command from shell", cmd); } - let out = Exec::cmd(cmd) + let out = Exec::shell(cmd) .stdout(Redirection::Pipe) .capture(); + if out.is_err() { if argv.contains_key("stderr") { runner.generate_variable(argv.get("stderr").unwrap().to_owned(), &out.unwrap_err().to_string()); } - + + if argv.contains_key("success") { + runner.generate_variable(argv.get("success").unwrap().to_owned(), &"0".to_string()); + } } else { let o = out.unwrap(); if argv.contains_key("stdout") { @@ -34,6 +38,10 @@ pub fn exec(argv: HashMap, args: Args, runner: &mut Runner) -> ( if argv.contains_key("stderr") { runner.generate_variable(argv.get("stderr").unwrap().to_owned(), &o.stderr_str()); } + + if argv.contains_key("success") { + runner.generate_variable(argv.get("success").unwrap().to_owned(), &o.success().to_string()); + } } } diff --git a/src/parser.lalrpop b/src/parser.lalrpop index 510d89e..107ea55 100644 --- a/src/parser.lalrpop +++ b/src/parser.lalrpop @@ -70,7 +70,7 @@ pub Breeze: (ProjectInfo, Vec) = { } Num: i64 = r"[0-9]+" => i64::from_str(<>).unwrap(); -string: String = r#"".*""# => { +string: String = r#""(\\\\|\\"|[^"\\])*""# => { let mut chars = <>.chars(); chars.next(); chars.next_back(); diff --git a/src/runner.rs b/src/runner.rs index 842decc..b305cf8 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -1,17 +1,16 @@ -use std::{collections::{HashMap, HashSet}, process::exit, hash::Hash, fmt}; +use std::{collections::{HashMap}, process::exit}; use crate::{nodes::{{ AST, Node }, Expr}, Args}; use crate::functions::get_std_functions; use lazy_static; -use label_logger::{{ error }, log}; +use label_logger::{{ error }, info}; use regex::Regex; lazy_static! { - static ref STRING_IDENTIFIER_REGEX: Regex = Regex::new(r"[^$]\$\{[a-zA-Z][a-zA-Z0-9_-]*\}").unwrap(); + static ref STRING_IDENTIFIER_REGEX: Regex = Regex::new(r#"(^$|^)\$\{[a-zA-Z][a-zA-Z0-9_-]*\}"#).unwrap(); } - #[derive(Clone)] pub struct Runner { ast: AST, @@ -26,7 +25,7 @@ pub struct Runner { impl Runner { pub fn throw_error(error: String /* TODO: line, col, etc */) { - error!(label: "Execution error", "{}", error); + error!(label: "Exec error", "{}", error); exit(0); } @@ -95,7 +94,10 @@ impl Runner { pub fn generate_variable(&mut self, n: String, e: &String) { let scope = &mut self.scopes[0]; let mut name = n.clone().to_string(); - name.remove(0); + + if name.starts_with("$") { + name.remove(0); + } if scope.contains_key(&n) { Self::throw_error(format!("Variable with name '{}' has already been defined on the same scope!", n)); @@ -113,7 +115,6 @@ impl Runner { } if value.is_none() { - println!("{:?}", self.scopes); Self::throw_error(format!("No variable with name '{}' has been found!", var)); } @@ -124,19 +125,18 @@ impl Runner { match expr { Expr::String(s) => { // THIS IS NOT BEING DETECTED! - let output = STRING_IDENTIFIER_REGEX.replace_all(s, |captures: ®ex::Captures| { + let output = STRING_IDENTIFIER_REGEX.replace_all(&s, |captures: ®ex::Captures| { let matched_word = &mut captures[0].to_string(); // get the matched word - matched_word.remove(0); matched_word.remove(0); matched_word.remove(0); matched_word.pop(); self.get_variable(matched_word.to_string()) - }); + }).to_string(); - Ok(output.to_string()) + Ok(output) }, _ => panic!("Invalid expression given!") @@ -169,7 +169,15 @@ impl Runner { Node::Message(ref e) => { let expr = self.execute_expr(e).unwrap(); - log!("{}", expr); + let lines = expr.lines(); + + if expr.contains("\n") { + for l in lines { + info!(label: "==>", "{}", l); + } + } else { + info!(label: "==>", "{}", expr); + } }, Node::VariableDecl(ref n, ref e) => { @@ -188,7 +196,7 @@ impl Runner { fn execute_task(mut self, executed_task: &Node) -> Result<(), &'static str> { match executed_task { // It should only be this task - Node::Task(ref name, ref nodes) => { + Node::Task(ref _name, ref nodes) => { self.execute_block(nodes).unwrap() }, diff --git a/test/project.breeze b/test/project.breeze index b4b3e5e..b7d960d 100644 --- a/test/project.breeze +++ b/test/project.breeze @@ -1,7 +1,8 @@ project test version "0.0.1"; task build { - message "${myStdout}"; + exec(cmd="git --help", stdout="identifier"); + message "${identifier}"; } default_task build; \ No newline at end of file