Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: builtin funtion rm. #401

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod cd;
pub mod echo;
pub mod mv;
pub mod nameof;
pub mod rm;
66 changes: 66 additions & 0 deletions src/modules/builtin/rm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::mem::swap;

use heraclitus_compiler::prelude::*;
use crate::modules::command::modifier::CommandModifier;
use crate::modules::condition::failed::Failed;
use crate::modules::expression::expr::Expr;
use crate::docs::module::DocumentationModule;
use crate::modules::types::{Type, Typed};
use crate::translate::module::TranslateModule;
use crate::utils::{ParserMetadata, TranslateMetadata};

#[derive(Debug, Clone)]
pub struct Rm {
path: Expr,
modifier: CommandModifier,
failed: Failed,
}

impl SyntaxModule<ParserMetadata> for Rm {
syntax_name!("Remove");

fn new() -> Self {
Rm {
path: Expr::new(),
failed: Failed::new(),
modifier: CommandModifier::new().parse_expr(),
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
syntax(meta, &mut self.modifier)?;
self.modifier.use_modifiers(meta, |_this, meta| {
token(meta, "rm")?;
syntax(meta, &mut self.path)?;
let path_type = self.path.get_type();
if path_type != Type::Text {
let position = self.path.get_position(meta);
return error_pos!(meta, position => {
message: "Builtin function `rm` can only be used with values of type Text",
comment: format!("Given type: {}, expected type: {}", path_type, Type::Text)
});
}
syntax(meta, &mut self.failed)?;
Ok(())
})
}
}

impl TranslateModule for Rm {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let path = self.path.translate(meta);
let failed = self.failed.translate(meta);
let mut is_silent = self.modifier.is_silent || meta.silenced;
swap(&mut is_silent, &mut meta.silenced);
let silent = meta.gen_silent();
swap(&mut is_silent, &mut meta.silenced);
format!("rm -rf {path}{silent}\n{failed}").trim_end().to_string()
}
}

impl DocumentationModule for Rm {
fn document(&self, _meta: &ParserMetadata) -> String {
"".to_string()
}
}

6 changes: 4 additions & 2 deletions src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use crate::modules::main::Main;
use crate::modules::builtin::{
echo::Echo,
mv::Mv,
cd::Cd
cd::Cd,
rm:: Rm,
};
use super::comment_doc::CommentDoc;

Expand Down Expand Up @@ -65,6 +66,7 @@ pub enum StatementType {
Cd(Cd),
Echo(Echo),
Mv(Mv),
Rm(Rm),
CommandModifier(CommandModifier),
CommentDoc(CommentDoc)
}
Expand All @@ -91,7 +93,7 @@ impl Statement {
ShorthandMul, ShorthandDiv,
ShorthandModulo,
// Command
CommandModifier, Echo, Mv, Cd,
CommandModifier, Echo, Mv, Cd, Rm,
// Comment doc
CommentDoc,
// Expression
Expand Down
2 changes: 1 addition & 1 deletion src/modules/variable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn variable_name_keywords() -> Vec<&'static str> {
// Command Modifiers
"silent", "unsafe",
// Misc
"echo", "status", "nameof", "mv", "cd"
"echo", "status", "nameof", "mv", "cd", "rm"
]
}

Expand Down
15 changes: 15 additions & 0 deletions src/tests/validity/rm.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * from "std/fs"
main {
let tmpdir = unsafe $mktemp -d /tmp/amber-XXXX$
unsafe $touch {tmpdir}/a$
if not file_exist("{tmpdir}/a") {
echo "Not Found"
}
unsafe rm "{tmpdir}/a"
if not file_exist("{tmpdir}/a") {
echo "Succeded"
} else {
echo "Found"
}
unsafe $rm -fr {tmpdir}$
}