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

Introduce 'function' built-in. #77

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

Conversation

Krush206
Copy link

@Krush206 Krush206 commented Jul 7, 2023

As requested in #4, here's my proposal. This is a wrapper around goto and source. The script recurses itself and searches for a goto label. It's an error for labels not contain an exit to their end. Function calls outside labels are, by default, labeled main.

This was tested sparsely, and may contain bugs I haven't faced, but is working as expected. One bug to be noted is that pipes don't give up on errors. This is possibly due to forking.

I noticed Tcsh has a built-in function command, but I can't trace the code. Said built-in function command is evaluated before mine, thus those who attempt to execute it won't get the correct error message.

@zoulasc
Copy link
Member

zoulasc commented Nov 18, 2023

Perhaps some documentation and tests?

@Krush206 Krush206 force-pushed the function branch 2 times, most recently from 836268e to 65a6a30 Compare November 22, 2023 12:49
Copy link
Author

@Krush206 Krush206 left a comment

Choose a reason for hiding this comment

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

Nevermind the last paragraph. I had an alias for function. Sorry for the mistake.

As requested in tcsh-org#4, here's my proposal. This is a wrapper around goto and source. The script recurses itself and searches for a goto label. It's an error for labels not contain an exit to their end. Function calls outside labels are, by default, labeled main.

This was tested sparsely, and may contain bugs I haven't faced, but is working as expected. One bug to be noted is that pipes don't give up on errors. This is possibly due to forking.

I noticed Tcsh has a built-in function command, but I can't trace the code. Said built-in function command is evaluated before mine, thus those who attempt to execute it won't get the correct error message.
Next, make a table of functions so they can be called globally. Currently, it isn't possible to call functions from sourced files other than the current.
@Krush206
Copy link
Author

Krush206 commented Jun 1, 2024

I think functions based on pipes makes a simpler feature, as well as allows for use in interactive sessions, resembling Bourne-compatible Shells better. Unlike the goto-based version, this new version only allows functions be called if they were previously declared (i.e: no forward jumps), making a similar behavior to Bourne-compatible Shells.

This version relies on a tree derived from variables and aliases. Unlike to aliases and variables, the tree is restrictive. Once a function is declared, may not be redeclared or undeclared.

I think functions based on pipes makes a simpler feature, as well as allows for use in interactive sessions, resembling Bourne-compatible Shells better. Unlike the goto-based version, functions may only be called if they were previously declared (i.e: no forward jumps), making a similar behavior to Bourne-compatible Shells.

This version relies on a tree derived from variables and aliases. Unlike to aliases and variables, the tree is restrictive. Once a function is declared, may not be redeclared or undeclared.

I was afraid this wouldn't work out for some operations, such as loops and gotos, because pipes cannot rewind. Fortunately, I was wrong, and the fact these operations are possible from interactive sessions, from a terminal, makes the assumption just as wrong, though I'm clueless as to how rewinding is possible on unsupported sources.
Though POSIX defines a minimum for pipe buffer, I decided is safer writing one byte a time. This may render slow reads, though writing in larger quantities rendered broken reads.

For an unknown reason, some operations, after SIGINT, rendered exiting the Shell. This happens on the ERR_RECURSION error as well. longjmp remedies the issue, though I'm afraid isn't the best solution.
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.

None yet

2 participants