Skip to content
New issue

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

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

Already on GitHub? # to your account

Miscellaneous refactoring #495

Merged
merged 4 commits into from
Oct 1, 2024
Merged
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
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
6 changes: 3 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ impl SyntaxModule<ParserMetadata> 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(())
Expand All @@ -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())
Expand Down
96 changes: 0 additions & 96 deletions src/modules/command/expr.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/modules/expression/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::literal::{
text::Text,
array::Array,
null::Null,
status::Status
status::Status,
};
use super::binop::{
add::Add,
Expand All @@ -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;
Expand Down Expand Up @@ -79,7 +79,7 @@ pub enum ExprType {
Cast(Cast),
Status(Status),
Nameof(Nameof),
Is(Is)
Is(Is),
}

#[derive(Debug, Clone, Default)]
Expand Down
4 changes: 2 additions & 2 deletions src/modules/expression/literal/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{parse_interpolated_region, translate_interpolated_region};
#[derive(Debug, Clone)]
pub struct Text {
strings: Vec<String>,
interps: Vec<Expr>
interps: Vec<Expr>,
}

impl Typed for Text {
Expand All @@ -23,7 +23,7 @@ impl SyntaxModule<ParserMetadata> for Text {
fn new() -> Self {
Text {
strings: vec![],
interps: vec![]
interps: vec![],
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/modules/function/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ impl SyntaxModule<ParserMetadata> 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
Expand All @@ -69,12 +68,12 @@ impl SyntaxModule<ParserMetadata> 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();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub mod declaration_utils;
pub mod invocation;
pub mod invocation_utils;
pub mod ret;
pub mod fail;
pub mod fail;
32 changes: 18 additions & 14 deletions src/modules/loops/iter_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
iter_name: String,
iter_type: Type
Expand All @@ -24,7 +24,7 @@ impl SyntaxModule<ParserMetadata> 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
Expand All @@ -42,11 +42,11 @@ impl SyntaxModule<ParserMetadata> 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();
Expand All @@ -73,26 +73,30 @@ impl SyntaxModule<ParserMetadata> 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")
},
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ 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;
use crate::modules::builtin::{
echo::Echo,
mv::Mv,
cd::Cd,
exit::Exit
exit::Exit,
};
use super::comment_doc::CommentDoc;
use super::comment::Comment;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub enum StatementType {
Exit(Exit),
CommandModifier(CommandModifier),
Comment(Comment),
CommentDoc(CommentDoc)
CommentDoc(CommentDoc),
}

#[derive(Debug, Clone)]
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", "exit"
"echo", "status", "nameof", "mv", "cd", "exit",
]
}

Expand Down
3 changes: 1 addition & 2 deletions src/tests/stdlib/change_owner.ab
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -13,5 +12,5 @@ main {
} else {
echo "File {tmpdir}/amber-symbolic not exists"
}
unsafe $rm {tmpdir}/amber-symbolic$
unsafe $rm -fr {tmpdir}$
}
2 changes: 1 addition & 1 deletion src/tests/stdlib/create_dir.ab
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Loading