tcsh-org / tcsh

This is a read-only mirror of the tcsh code repository.
https://www.tcsh.org/
Other
229 stars 42 forks source link

Introduce 'function' built-in. #77

Open Krush206 opened 1 year ago

Krush206 commented 1 year ago

As requested in https://github.com/tcsh-org/tcsh/issues/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 commented 8 months ago

Perhaps some documentation and tests?

Krush206 commented 1 month ago

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.