Skip to content

Commit

Permalink
Fixed strings, message command and exec()
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed May 2, 2023
1 parent 3374fdb commit 2be0aec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
12 changes: 10 additions & 2 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ pub fn exec(argv: HashMap<String, String>, 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") {
Expand All @@ -34,6 +38,10 @@ pub fn exec(argv: HashMap<String, String>, 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());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub Breeze: (ProjectInfo, Vec<Node>) = {
}

Num: i64 = r"[0-9]+" => i64::from_str(<>).unwrap();
string: String = r#"".*""# => {
string: String = r#""(\\\\|\\"|[^"\\])*""# => {
let mut chars = <>.chars();
chars.next();
chars.next_back();
Expand Down
34 changes: 21 additions & 13 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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));
Expand All @@ -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));
}

Expand All @@ -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: &regex::Captures| {
let output = STRING_IDENTIFIER_REGEX.replace_all(&s, |captures: &regex::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!")
Expand Down Expand Up @@ -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) => {
Expand All @@ -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()
},

Expand Down
3 changes: 2 additions & 1 deletion test/project.breeze
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 2be0aec

Please sign in to comment.