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

Implement standard library glob function #511

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hdwalters
Copy link
Contributor

Fixes #463, by implementing glob as a standard library function. I also fixed a bug where failing functions would return the previous array value with the first item blanked out, because the default return value was Bash string '' not Bash array ().

@@ -80,23 +80,27 @@ impl TranslateModule for Failed {
if self.is_question_mark {
// if the failed expression is in the main block we need to clear the return value
let clear_return = if !self.is_main {
let (name, id, variant) = meta.fun_name.clone().expect("Function name not set");
format!("__AF_{name}{id}_v{variant}=''")
let fun_name = meta.fun_name.as_ref().expect("Function name not set");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bundled (name, id, variant) into a new struct FunctionName, because we were duplicating mangled name generation in three places.

let (name, id, variant) = meta.fun_name.clone().expect("Function name not set");
format!("__AF_{name}{id}_v{variant}=''")
let fun_name = meta.fun_name.as_ref().expect("Function name not set");
format!("{}={}", fun_name.mangled_name(), fun_name.default_value())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes a bug where failing functions would return the previous array value with the first item blanked out, because the default return value was Bash string '' not Bash array (). Try running the following Amber script with commits up to "5212ef5 Add glob functions to standard library":

hwalters@Ghostwheel ~/git/amber (HEAD) 
$ cat glob.ab
#!/usr/bin/env amber
import * from "std/fs"

echo "Calling glob(\"*.md\")"
loop file in trust glob("*.md") {
    echo "Found \"{file}\""
}

echo "Calling glob(\"*.xyz\")"
loop file in trust glob("*.xyz") {
    echo "Found \"{file}\""
}
hwalters@Ghostwheel ~/git/amber (HEAD) 
$ ./glob.ab
Calling glob("*.md")
Found "CONTRIBUTING.md"
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"
Calling glob("*.xyz")
Found ""
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"

This is what it does with the latest commit:

hwalters@Ghostwheel ~/git/amber (implement-stdlib-glob-function) 
$ ./glob.ab
Calling glob("*.md")
Found "CONTRIBUTING.md"
Found "LICENSE.md"
Found "NIX.md"
Found "README.md"
Calling glob("*.xyz")

@@ -30,6 +30,14 @@ impl Type {
pub fn is_allowed_in(&self, other: &Type) -> bool {
self == other || self.is_subset_of(other)
}

pub fn is_array(&self) -> bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may cause problems if the union types change supports union types in return values. If a function could return Text | [Text] (which seems like something we would want to disallow, tbh) what would the default return type be?

Copy link
Member

Choose a reason for hiding this comment

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

Then the default can be the first type of the union, Text in your case. No need to disallow this, it's one of union's use cases.

src/std/fs.ab Outdated
/// }
/// }
/// ```
pub fun glob_multiple(path: [Text]): [Text]? {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like a good idea to eventually support either glob("*.txt") or glob(["*.txt", "*.xyz"]). This function is here as a placeholder for that functionality, so we can write unit tests. To be merged into the main function when the union types PR is merged.

@@ -7,5 +7,5 @@ main {
} else {
echo "failed"
}
trust $rm {tmpdir}$
trust $rm -fr {tmpdir}$
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is also part of a different pending PR. Shouldn't cause merge conflicts, but this is implemented in a commit of its own, so I can drop it and rebase if it causes problems.

Copy link
Member

@mks-h mks-h left a comment

Choose a reason for hiding this comment

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

Generally speaking this is a very good looking PR. Just a few nitpicks, and it would be good to safeguard the escaping logic with some tests.

src/std/fs.ab Outdated Show resolved Hide resolved
@@ -30,6 +30,14 @@ impl Type {
pub fn is_allowed_in(&self, other: &Type) -> bool {
self == other || self.is_subset_of(other)
}

pub fn is_array(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Then the default can be the first type of the union, Text in your case. No need to disallow this, it's one of union's use cases.

src/utils/function_name.rs Outdated Show resolved Hide resolved
src/utils/function_name.rs Outdated Show resolved Hide resolved
src/utils/metadata/translate.rs Outdated Show resolved Hide resolved
src/utils/function_name.rs Outdated Show resolved Hide resolved
@hdwalters hdwalters added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Oct 12, 2024
@hdwalters hdwalters self-assigned this Oct 12, 2024
@hdwalters hdwalters requested a review from mks-h October 12, 2024 10:19
src/modules/function/ret.rs Outdated Show resolved Hide resolved
@hdwalters hdwalters requested a review from mks-h October 12, 2024 12:41
src/modules/function/ret.rs Outdated Show resolved Hide resolved
@hdwalters hdwalters requested a review from mks-h October 12, 2024 18:24
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
@hdwalters hdwalters requested a review from mks-h October 13, 2024 09:37
src/std/fs.ab Outdated Show resolved Hide resolved
@hdwalters hdwalters requested a review from mks-h October 14, 2024 17:22
@b1ek
Copy link
Member

b1ek commented Oct 15, 2024

this is very confusing to review.

are you fixing an underlying issue right away which blocks you from implementing the function in stdlib? then it should be in two different PR's, with this one depending on the one that resolves the issue in the compiler

also the default return thing is a new language feature, it should be a feature request issue first, and then a PR

@hdwalters
Copy link
Contributor Author

this is very confusing to review.

are you fixing an underlying issue right away which blocks you from implementing the function in stdlib? then it should be in two different PR's, with this one depending on the one that resolves the issue in the compiler

also the default return thing is a new language feature, it should be a feature request issue first, and then a PR

To summarise our private discussion, I'll create a new issue and PR, to fix the "default return" problem, then rebase this PR once that's been merged. But in return, you have to review both of them! (grin)

@hdwalters
Copy link
Contributor Author

Created issue #515.

@hdwalters
Copy link
Contributor Author

Created issue #515.

And created pull request #516 to fix that; have added @b1ek and @mks-h as reviewers. I'll rebase this PR branch once that's been merged.

@hdwalters hdwalters force-pushed the implement-stdlib-glob-function branch from 5dc0e08 to d9569af Compare October 19, 2024 06:06
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Loop files/directories (glob)
4 participants