diff --git a/build.rs b/build.rs index c96fa0df..8e208bc5 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,4 @@ fn main() { - build_helper::rerun_if_changed("src/tests/**"); + // See [https://doc.rust-lang.org/cargo/reference/build-scripts.html]. + build_helper::rerun_if_changed("src/tests"); } diff --git a/src/compiler.rs b/src/compiler.rs index 9520c10b..406faec7 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -162,11 +162,11 @@ impl AmberCompiler { ); } - let mut res = result.join("\n") + "\n"; + let mut result = result.join("\n") + "\n"; if !self.cli_opts.disable_format { if let Some(formatter) = BashFormatter::get_available() { - res = formatter.format(res); + result = formatter.format(result); } } @@ -179,7 +179,7 @@ impl AmberCompiler { .to_string() .as_str()), ].join("\n"); - format!("{}\n{}", header, res) + format!("{}\n{}", header, result) } pub fn document(&self, block: Block, meta: ParserMetadata, output: String) { diff --git a/src/modules/block.rs b/src/modules/block.rs index b414ff35..fb7c42c7 100644 --- a/src/modules/block.rs +++ b/src/modules/block.rs @@ -46,14 +46,14 @@ impl SyntaxModule for Block { else if token.word == "}" { break; } - let mut statemant = Statement::new(); - if let Err(failure) = statemant.parse(meta) { + let mut statement = Statement::new(); + if let Err(failure) = statement.parse(meta) { return match failure { Failure::Quiet(pos) => error_pos!(meta, pos, "Unexpected token"), Failure::Loud(err) => return Err(Failure::Loud(err)) } } - self.statements.push(statemant); + self.statements.push(statement); } meta.pop_scope(); Ok(()) @@ -68,8 +68,7 @@ impl TranslateModule for Block { meta.increase_indent(); let result = if self.is_empty() { ":".to_string() - } - else { + } else { self.statements.iter() .map(|statement| statement.translate(meta)) .filter(|translation| !translation.trim().is_empty()) diff --git a/src/modules/command/expr.rs b/src/modules/command/expr.rs deleted file mode 100644 index 97eee1b2..00000000 --- a/src/modules/command/expr.rs +++ /dev/null @@ -1,96 +0,0 @@ -use std::mem::swap; -use heraclitus_compiler::prelude::*; -use crate::utils::{ParserMetadata, TranslateMetadata}; -use crate::modules::condition::failed::Failed; -use crate::modules::types::{Type, Typed}; -use crate::docs::module::DocumentationModule; -use crate::modules::expression::expr::{Expr, ExprType}; -use crate::translate::module::TranslateModule; -use crate::modules::expression::literal::{parse_interpolated_region, translate_interpolated_region}; - -use super::modifier::CommandModifier; - -#[derive(Debug, Clone)] -pub struct CommandExpr { - strings: Vec, - interps: Vec, - modifier: CommandModifier, - failed: Failed, - is_silent_expr: bool -} - -impl Typed for CommandExpr { - fn get_type(&self) -> Type { - Type::Failable(Box::new()); - } -} - -impl SyntaxModule for CommandExpr { - syntax_name!("CommandExpr"); - - fn new() -> Self { - CommandExpr { - strings: vec![], - interps: vec![], - modifier: CommandModifier::new().parse_expr(), - failed: Failed::new(), - is_silent_expr: false - } - } - - fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult { - match syntax(meta, &mut self.modifier) { - // If the command modifier was parsed successfully, then we swap the result - Ok(_) => { - if let Some(ExprType::CommandExpr(command)) = &mut self.modifier.expr.value.clone() { - swap(command, self); - // Retrieve the silent modifier from the command modifier - self.is_silent_expr = command.modifier.is_silent; - } - Ok(()) - }, - Err(Failure::Loud(err)) => Err(Failure::Loud(err)), - Err(Failure::Quiet(_)) => { - let tok = meta.get_current_token(); - (self.strings, self.interps) = parse_interpolated_region(meta, '$')?; - match syntax(meta, &mut self.failed) { - Ok(_) => Ok(()), - Err(Failure::Quiet(_)) => error!(meta, tok => { - message: "Every command statement must handle failed execution", - comment: "You can use '?' in the end to fail the exit code of the command" - }), - Err(err) => Err(err) - } - } - } - } -} - -impl TranslateModule for CommandExpr { - fn translate(&self, meta: &mut TranslateMetadata) -> String { - // Translate all interpolations - let interps = self.interps.iter() - .map(|item| item.translate(meta)) - .collect::>(); - let failed = self.failed.translate(meta); - let silent = if self.is_silent_expr { " 2>/dev/null" } else { "" }; - if failed.is_empty() { - let translation = translate_interpolated_region(self.strings.clone(), interps, false); - meta.gen_subprocess(&(translation + silent)) - } else { - let id = meta.gen_value_id(); - let quote = meta.gen_quote(); - let dollar = meta.gen_dollar(); - let translation = translate_interpolated_region(self.strings.clone(), interps, false); - meta.stmt_queue.push_back(format!("__AMBER_VAL_{id}=$({translation}{silent})")); - meta.stmt_queue.push_back(failed); - format!("{quote}{dollar}{{__AMBER_VAL_{id}}}{quote}") - } - } -} - -impl DocumentationModule for CommandExpr { - fn document(&self, _meta: &ParserMetadata) -> String { - "".to_string() - } -} diff --git a/src/modules/expression/expr.rs b/src/modules/expression/expr.rs index d522b393..368f70aa 100644 --- a/src/modules/expression/expr.rs +++ b/src/modules/expression/expr.rs @@ -15,7 +15,7 @@ use super::literal::{ text::Text, array::Array, null::Null, - status::Status + status::Status, }; use super::binop::{ add::Add, @@ -35,11 +35,11 @@ use super::binop::{ }; use super::unop::{ not::Not, - neg::Neg + neg::Neg, }; use super::typeop::{ cast::Cast, - is::Is + is::Is, }; use super::parentheses::Parentheses; use crate::modules::variable::get::VariableGet; @@ -79,7 +79,7 @@ pub enum ExprType { Cast(Cast), Status(Status), Nameof(Nameof), - Is(Is) + Is(Is), } #[derive(Debug, Clone, Default)] diff --git a/src/modules/expression/literal/text.rs b/src/modules/expression/literal/text.rs index 6d0de380..54537383 100644 --- a/src/modules/expression/literal/text.rs +++ b/src/modules/expression/literal/text.rs @@ -8,7 +8,7 @@ use super::{parse_interpolated_region, translate_interpolated_region}; #[derive(Debug, Clone)] pub struct Text { strings: Vec, - interps: Vec + interps: Vec, } impl Typed for Text { @@ -23,7 +23,7 @@ impl SyntaxModule for Text { fn new() -> Self { Text { strings: vec![], - interps: vec![] + interps: vec![], } } diff --git a/src/modules/function/invocation.rs b/src/modules/function/invocation.rs index 5aa7198e..a98f81eb 100644 --- a/src/modules/function/invocation.rs +++ b/src/modules/function/invocation.rs @@ -58,8 +58,7 @@ impl SyntaxModule for FunctionInvocation { // Get the function name let tok = meta.get_current_token(); if let Some(ref tok) = tok { - self.line = tok.pos.0; - self.col = tok.pos.1; + (self.line, self.col) = tok.pos; } self.name = variable(meta, variable_name_extensions())?; // Get the arguments @@ -69,12 +68,12 @@ impl SyntaxModule for FunctionInvocation { if token(meta, ")").is_ok() { break } - let mut expr = Expr::new(); - syntax(meta, &mut expr)?; - self.args.push(expr); + let mut arg = Expr::new(); + syntax(meta, &mut arg)?; + self.args.push(arg); match token(meta, ")") { Ok(_) => break, - Err(_) => token(meta, ",")? + Err(_) => token(meta, ",")?, }; } let function_unit = meta.get_fun_declaration(&self.name).unwrap().clone(); diff --git a/src/modules/function/mod.rs b/src/modules/function/mod.rs index 56605837..16dc93c1 100644 --- a/src/modules/function/mod.rs +++ b/src/modules/function/mod.rs @@ -3,4 +3,4 @@ pub mod declaration_utils; pub mod invocation; pub mod invocation_utils; pub mod ret; -pub mod fail; \ No newline at end of file +pub mod fail; diff --git a/src/modules/loops/iter_loop.rs b/src/modules/loops/iter_loop.rs index 1b7006e2..f333d572 100644 --- a/src/modules/loops/iter_loop.rs +++ b/src/modules/loops/iter_loop.rs @@ -12,7 +12,7 @@ use crate::modules::block::Block; #[derive(Debug, Clone)] pub struct IterLoop { block: Block, - iterable: Expr, + iter_expr: Expr, iter_index: Option, iter_name: String, iter_type: Type @@ -24,7 +24,7 @@ impl SyntaxModule for IterLoop { fn new() -> Self { IterLoop { block: Block::new(), - iterable: Expr::new(), + iter_expr: Expr::new(), iter_index: None, iter_name: String::new(), iter_type: Type::Generic @@ -42,11 +42,11 @@ impl SyntaxModule for IterLoop { context!({ // Parse iterable let tok = meta.get_current_token(); - syntax(meta, &mut self.iterable)?; - match self.iterable.get_type() { - Type::Array(kind) => self.iter_type = *kind, - _ => return error!(meta, tok, "Expected iterable") - } + syntax(meta, &mut self.iter_expr)?; + self.iter_type = match self.iter_expr.get_type() { + Type::Array(kind) => *kind, + _ => return error!(meta, tok, "Expected iterable"), + }; token(meta, "{")?; // Create iterator variable meta.push_scope(); @@ -73,26 +73,30 @@ impl SyntaxModule for IterLoop { impl TranslateModule for IterLoop { fn translate(&self, meta: &mut TranslateMetadata) -> String { let name = &self.iter_name; - let iterable = self.iterable.translate(meta); + let expr = self.iter_expr.translate(meta); match self.iter_index.as_ref() { Some(index) => { // Create an indentation for the index increment meta.increase_indent(); let indent = meta.gen_indent(); meta.decrease_indent(); - [format!("{index}=0;"), - format!("for {name} in {iterable}"), + [ + format!("{index}=0;"), + format!("for {name} in {expr}"), "do".to_string(), self.block.translate(meta), format!("{indent}(( {index}++ )) || true"), - "done".to_string()].join("\n") + "done".to_string(), + ].join("\n") }, None => { - [format!("for {name} in {iterable}"), + [ + format!("for {name} in {expr}"), "do".to_string(), self.block.translate(meta), - "done".to_string()].join("\n") - } + "done".to_string(), + ].join("\n") + }, } } } diff --git a/src/modules/statement/stmt.rs b/src/modules/statement/stmt.rs index 4b8c924d..8459561d 100644 --- a/src/modules/statement/stmt.rs +++ b/src/modules/statement/stmt.rs @@ -6,31 +6,31 @@ use crate::modules::expression::expr::{Expr, ExprType}; use crate::translate::module::TranslateModule; use crate::modules::variable::{ init::VariableInit, - set::VariableSet + set::VariableSet, }; use crate::modules::command::modifier::CommandModifier; use crate::handle_types; use crate::modules::condition::{ ifchain::IfChain, - ifcond::IfCondition + ifcond::IfCondition, }; use crate::modules::shorthand::{ add::ShorthandAdd, sub::ShorthandSub, mul::ShorthandMul, div::ShorthandDiv, - modulo::ShorthandModulo + modulo::ShorthandModulo, }; use crate::modules::loops::{ infinite_loop::InfiniteLoop, iter_loop::IterLoop, break_stmt::Break, - continue_stmt::Continue + continue_stmt::Continue, }; use crate::modules::function::{ declaration::FunctionDeclaration, ret::Return, - fail::Fail + fail::Fail, }; use crate::modules::imports::import::Import; use crate::modules::main::Main; @@ -38,7 +38,7 @@ use crate::modules::builtin::{ echo::Echo, mv::Mv, cd::Cd, - exit::Exit + exit::Exit, }; use super::comment_doc::CommentDoc; use super::comment::Comment; @@ -70,7 +70,7 @@ pub enum StatementType { Exit(Exit), CommandModifier(CommandModifier), Comment(Comment), - CommentDoc(CommentDoc) + CommentDoc(CommentDoc), } #[derive(Debug, Clone)] diff --git a/src/modules/variable/mod.rs b/src/modules/variable/mod.rs index 08e5a30d..51bab21a 100644 --- a/src/modules/variable/mod.rs +++ b/src/modules/variable/mod.rs @@ -32,7 +32,7 @@ pub fn variable_name_keywords() -> Vec<&'static str> { // Command Modifiers "silent", "unsafe", // Misc - "echo", "status", "nameof", "mv", "cd", "exit" + "echo", "status", "nameof", "mv", "cd", "exit", ] } diff --git a/src/tests/stdlib/change_owner.ab b/src/tests/stdlib/change_owner.ab index d027d241..5535c30b 100644 --- a/src/tests/stdlib/change_owner.ab +++ b/src/tests/stdlib/change_owner.ab @@ -1,7 +1,6 @@ import { change_owner } from "std/fs" // We use `whoami` to get the running user to assign again the same user as permission main { - let tmpdir = $mktemp -d /tmp/amber-XXXX$ failed { echo "It wasn't possible to create the folder" } @@ -13,5 +12,5 @@ main { } else { echo "File {tmpdir}/amber-symbolic not exists" } - unsafe $rm {tmpdir}/amber-symbolic$ + unsafe $rm -fr {tmpdir}$ } diff --git a/src/tests/stdlib/create_dir.ab b/src/tests/stdlib/create_dir.ab index cc17a73f..e39c7a3a 100644 --- a/src/tests/stdlib/create_dir.ab +++ b/src/tests/stdlib/create_dir.ab @@ -2,7 +2,7 @@ import { create_dir, dir_exist } from "std/fs" main { create_dir("/tmp/amber-test") if dir_exist("/tmp/amber-test") { - unsafe $rm /tmp/amber-test$ + unsafe $rm -fr /tmp/amber-test$ echo "Succeded" } } diff --git a/src/tests/stdlib/create_symbolic_link.ab b/src/tests/stdlib/create_symbolic_link.ab index 31a6948a..de68e945 100644 --- a/src/tests/stdlib/create_symbolic_link.ab +++ b/src/tests/stdlib/create_symbolic_link.ab @@ -7,6 +7,5 @@ main { } else { echo "failed" } - unsafe $rm {tmpdir}/amber-symbolic$ - unsafe $rm {tmpdir}/amber-symbolic-link$ + unsafe $rm -fr {tmpdir}$ } diff --git a/src/tests/stdlib/make_executable.ab b/src/tests/stdlib/make_executable.ab index 83d95620..73fee6ba 100644 --- a/src/tests/stdlib/make_executable.ab +++ b/src/tests/stdlib/make_executable.ab @@ -6,9 +6,8 @@ import { make_executable } from "std/fs" main { let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$ unsafe $touch {tmpdir}/amber-symbolic$ - unsafe $touch /tmp/amber-symbolic$ - if make_executable("/tmp/amber-symbolic") { + if make_executable("{tmpdir}/amber-symbolic") { echo "created" } - unsafe $rm {tmpdir}/amber-symbolic$ + unsafe $rm -fr {tmpdir}$ }