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

Conversation

Mte90
Copy link
Member

@Mte90 Mte90 commented Oct 2, 2024

Fix #441

I am not sure why it is reporting "Variable 'len' does not exist", someone can help me?

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

@Mte90 Mte90 changed the title feat(builtin): len, ##441 feat(builtin): len, #441 Oct 2, 2024
@Mte90
Copy link
Member Author

Mte90 commented Oct 2, 2024

Now says "Identifier 'len' is a reserved keyword".

len [1, 2, 3, 4]
echo len [1, 2, 3, 4]

The first line of code works but the second one no, so @Ph0enixKM maybe there is something to improve in the compiler on parsing a builtin that include another one?

src/modules/builtin/len.rs Outdated Show resolved Hide resolved
@@ -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

src/tests/validity/len_string.ab Show resolved Hide resolved
@Ph0enixKM
Copy link
Member

This happens because you have added it to statements modules which means that it works only as a statement like function declaration or loops. What you probably wanted is to add it to an expression modules. Expressions are syntax elements that represent certain value and type. For instance loop is not an expression because it does not represent any type or value. On the other hand a len is an expression because it's a value of length of the value that we put in and it's of type Num. Each expression must implement Typed trait. You can see it being used in modules under modules/expression

Copy link
Member

@b1ek b1ek left a comment

Choose a reason for hiding this comment

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

it doesnt work

code:

let a = "1234";
echo len a;

stdout:

bash: line 8: ${#"${__0_a}"}: bad substitution

also it triggers an shfmt error, which was very confusing, i thought its something printed by amber:

<standard input>:2:15: parameter expansion requires a literal

@@ -162,6 +166,8 @@ impl SyntaxModule<ParserMetadata> for Expr {
impl TranslateModule for Expr {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
translate_expression!(meta, self.value.as_ref().unwrap(), [
// Builtin
Len,
Copy link
Member

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

Copy link
Member

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 giant match:

match (module) {
    ...
    Len(item) => item.translate()
    ...
}

It matters only in the parse function above.

@Mte90
Copy link
Member Author

Mte90 commented Oct 16, 2024

So we discussed and seems that the issue is the fact that Amber load anyway the stdlib so a len already exists also if it is not loaded in the Amber script.
To fix the issue we should remove it from the stdlib and alert in the next release about the breaking change.

@Ph0enixKM says that needs some rewrite Amber in the expression part instead.

Also this is the first builtin that return a value compared to the others we already have.

@b1ek
Copy link
Member

b1ek commented Oct 21, 2024

To fix the issue we should remove it from the stdlib and alert in the next release about the breaking change.

so push a commit

@Ph0enixKM says that needs some rewrite Amber in the expression part instead.

he should say that himself, or even better push a commit resolving the issue

@Mte90
Copy link
Member Author

Mte90 commented Oct 21, 2024

he should say that himself, or even better push a commit resolving the issue

Yeah, I am waiting him to know what to do

@Ph0enixKM
Copy link
Member

Sorry for long overdue. I'll add this issue to my priority to-do list

Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

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

lgtm

@@ -162,6 +166,8 @@ impl SyntaxModule<ParserMetadata> for Expr {
impl TranslateModule for Expr {
fn translate(&self, meta: &mut TranslateMetadata) -> String {
translate_expression!(meta, self.value.as_ref().unwrap(), [
// Builtin
Len,
Copy link
Member

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 giant match:

match (module) {
    ...
    Len(item) => item.translate()
    ...
}

It matters only in the parse function above.

@Mte90
Copy link
Member Author

Mte90 commented Oct 28, 2024

Closed for #545

@Mte90 Mte90 closed this Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Create len builtin
4 participants