-
Notifications
You must be signed in to change notification settings - Fork 94
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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: Box<Expr>, | ||
} | ||
|
||
impl SyntaxModule<ParserMetadata> for Len { | ||
syntax_name!("Length"); | ||
|
||
fn new() -> Self { | ||
Len { | ||
value: Box::new(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() | ||
} | ||
} | ||
|
||
impl Typed for Len { | ||
fn get_type(&self) -> Type { | ||
Type::Num | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod echo; | |
pub mod mv; | ||
pub mod nameof; | ||
pub mod exit; | ||
pub mod len; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has to be deleted out btw, since or renamed. but there is no point in renaming imo because its a breaking change the same way as deleting is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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}}"$ | ||
} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Output | ||
// 4 | ||
|
||
main { | ||
echo len [1, 2, 3, 4] | ||
} |
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
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi the order matters in this thing. if its on the bottom it tries to parse that as a VariableGet and throws as error. probably an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
translate
this does not matter. This macro expands to a giantmatch
:It matters only in the
parse
function above.