Skip to content

Commit

Permalink
feat(builtin): len, #amber-lang#441
Browse files Browse the repository at this point in the history
  • Loading branch information
Mte90 committed Oct 2, 2024
1 parent 0e67c89 commit 5e6d9ba
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 21 deletions.
46 changes: 46 additions & 0 deletions src/modules/builtin/len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use heraclitus_compiler::prelude::*;
use crate::modules::expression::expr::Expr;
use crate::translate::module::TranslateModule;
use crate::docs::module::DocumentationModule;
use crate::modules::types::{Type, Typed};
use crate::utils::{ParserMetadata, TranslateMetadata};

#[derive(Debug, Clone)]
pub struct Len {
value: Expr,
}

impl SyntaxModule<ParserMetadata> for Len {
syntax_name!("Length");

fn new() -> Self {
Len {
value: Expr::new(),
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
token(meta, "len")?;
syntax(meta, &mut self.value)?;

Ok(())
}
}

impl TranslateModule for Len {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
let path_type = self.value.get_type();
let value = self.value.translate(meta);
if path_type != Type::Text {
format!("echo \"${{#{}}}\"", value).trim_end().to_string()
} else {
format!("echo \"${{#{}[@]}}\"", value).trim_end().to_string()
}
}
}

impl DocumentationModule for Len {
fn document(&self, _meta: &ParserMetadata) -> String {
"".to_string()
}
}
1 change: 1 addition & 0 deletions src/modules/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod echo;
pub mod mv;
pub mod nameof;
pub mod exit;
pub mod len;
4 changes: 3 additions & 1 deletion src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::modules::builtin::{
mv::Mv,
cd::Cd,
exit::Exit,
len::Len,
};
use super::comment_doc::CommentDoc;
use super::comment::Comment;
Expand Down Expand Up @@ -68,6 +69,7 @@ pub enum StatementType {
Echo(Echo),
Mv(Mv),
Exit(Exit),
Len(Len),
CommandModifier(CommandModifier),
Comment(Comment),
CommentDoc(CommentDoc),
Expand Down Expand Up @@ -95,7 +97,7 @@ impl Statement {
ShorthandMul, ShorthandDiv,
ShorthandModulo,
// Command
CommandModifier, Echo, Mv, Cd, Exit,
CommandModifier, Echo, Mv, Cd, Exit, Len,
// Comment doc
CommentDoc, Comment,
// Expression
Expand Down
11 changes: 7 additions & 4 deletions src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fun chars(text: Text): [Text] {
/// Gets the length of provided text or array.
#[allow_absurd_cast]
pub fun len(value): Num {
echo "The len stdlib is deprecated, use the builtin!"
unsafe {
if value is Text:
return $echo "\$\{#{nameof value}}"$ as Num
Expand Down Expand Up @@ -140,7 +141,7 @@ pub fun ends_with(text: Text, suffix: Text): Bool {
/// If `length` is provided, the substring will include `length` characters; otherwise, it slices to the end of `text`.
/// If `length` is negative, an empty string is returned.
pub fun slice(text: Text, index: Num, length: Num = 0): Text {
if length == 0: length = len(text) - index
if length == 0: length = len text - index
if length <= 0: return ""
return unsafe $printf "%.{length}s" "\$\{text:{index}}"$
}
Expand All @@ -159,16 +160,18 @@ pub fun capitalize(text: Text): Text {

/// Pads `text` with the specified `pad` character on left until it reaches the desired `length`.
pub fun lpad(text: Text, pad: Text, length: Num): Text {
if length <= len(text): return text
length = len(text) - length
if length <= len text: return text
length = len text
length -= length
pad = unsafe $printf "%{length}s" "" | tr " " "{pad}"$
return pad + text
}

/// Pads `text` with the specified `pad` character on the right until it reaches the desired `length`.
pub fun rpad(text: Text, pad: Text, length: Num): Text {
if length <= len(text): return text
length = len(text) - length
length = len text
length -= length
pad = unsafe $printf "%{length}s" "" | tr " " "{pad}"$
return text + pad
}
Expand Down
8 changes: 0 additions & 8 deletions src/tests/stdlib/len_list.ab

This file was deleted.

8 changes: 0 additions & 8 deletions src/tests/stdlib/len_string.ab

This file was deleted.

6 changes: 6 additions & 0 deletions src/tests/validity/len_list.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Output
// 4

main {
echo len [1, 2, 3, 4]
}
6 changes: 6 additions & 0 deletions src/tests/validity/len_string.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Output
// 5

main {
echo len "hello"
}

0 comments on commit 5e6d9ba

Please sign in to comment.