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

Proposal for a new 'leval' command #304

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

Proposal for a new 'leval' command #304

wants to merge 8 commits into from

Conversation

msteveb
Copy link
Owner

@msteveb msteveb commented Jul 24, 2024

This is a proposal. Feedback is welcome, including alternative names (such as lsubst).
From the docs:

This command is similar to list in that it creates a list, but uses
the same rules as scripts when constructing the elements of the list.
It is somewhat similar to subst except it produces a list instead of a string.

This means that variables are substituted, commands are evaluated, backslashes are
interpreted, the expansion operator is applied and comments are skipped.

Consider the following example.

    set x 1
    set y {2 3}
    set z 3
    leval {
        # This is a list with interpolation
        $x; # The x variable
        {*}$y; # The y variable expanded
        [string cat a b c]; # A command
        {*}[list 4 5]; # A list expanded into multiple elements
        "$z$z"; # A string with interpolation
    }

The result of leval is the following list with 7 elements.

1 2 3 abc 4 5 33

This is particularly useful when constructing a list (or dict)
as a data structure as it easily allows for comments and variable and command
substitution.

msteveb added 7 commits July 24, 2024 13:49
Halfway between list and subst, makes it easy to construct
lists with substitutions (as opposed to strings with substitutions)

Could also be named lsubst

Signed-off-by: Steve Bennett <steveb@workware.net.au>
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Since we want the same parsing as eval, use JimGetScript()
and extract the list from the resulting script tokens.

This provides better line numbers and reuses more code.

Signed-off-by: Steve Bennett <steveb@workware.net.au>
Including error conditions.
Catch break and continue invoked within leval.

Signed-off-by: Steve Bennett <steveb@workware.net.au>
Useful if treating a script as data

Signed-off-by: Steve Bennett <steveb@workware.net.au>
@gromgit
Copy link
Contributor

gromgit commented Jul 25, 2024

Nice, though I think lsubst is a far more appropriate name than level, since I can't see how the base operation (substitutions) is related to eval.

@msteveb
Copy link
Owner Author

msteveb commented Jul 25, 2024

I agree that lsubst is a better name

Signed-off-by: Steve Bennett <steveb@workware.net.au>
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.

2 participants