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): len, #441 #504

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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 {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
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
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", "len"
]
}

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!"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still in alpha; surely it would be better to remove the standard library function completely, rather than deprecating it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have anything to deprecate stuff right now in Amber so I thought that an alert it was the only option

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this, there is no need to deprecate. we are way too early for that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be deleted out btw, since len is a reserved keyword.

or renamed. but there is no point in renaming imo because its a breaking change the same way as deleting is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I agree with @hdwalters and @b1ek

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about split those in 2 lines but when the issue is gone I will check that part

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"
b1ek marked this conversation as resolved.
Show resolved Hide resolved
}
Loading