One in golscript can view the stack easily by pressing enter in the shell. But there is a default variable that changing it to a value that does not contain "\n" removes this behavior. Why is this happening?
Example of the behavior:
> 1
[1]
> 2 3 4
[1 2 3 4]
>
[1 2 3 4]
> "As we noted the stack can be displayed by each enter button";
[1 2 3 4]
> :n
> 1
>
> 918882 182391 13829213
> "Now the stack can no longer be displayed.";
>
The stack is worth "\n" as we see with this:
> n print
"\n"
[]
> "\nDaniel \n":n;
[]
Daniel
> "Daniel":n;
>
Looking at the source code, specifically at lines 736 to 740, reveals that in the REPL, after each line of code entered, first that code is compiled and executed, then the stack is updated, and finally the command p
is also compiled and executed.
while (code=Readline.readline("> ", true))
begin
code.compile(true).go
gpush Garray.new(Stack)
'p'.compile.go
Looking then at lines 725 to 731, we can see the default bindings that are also compiled and executed before anything else. In particular, p
is bound to puts
, which in turn is bound to print n print
, while n
is bound to the newline character "\n"
.
'"\n":n;
{print n print}:puts;
{`puts}:p;
{1$if}:and;
{1$\if}:or;
{\!!{!}*}:xor;
'.compile.go
Thus, if you enter a line of code, it is first processed, then the stack is being printed, followed by a newline character. An empty line of code (by just pressing enter in the REPL) is no special case here, the stack and the newline character are still printed. However, if you redefine n
, it is that what will be printed instead of the newline character. To avoid this behavior, redefine puts
to use a literal newline character instead of n
:
> {print "\n" print}:puts;
[]
> 1
[1]
> 2 3 4
[1 2 3 4]
> :n
[1 2 3 4]
> n
[1 2 3 4 4]
> "\nDaniel \n":n;
[1 2 3 4 4]
> n
[1 2 3 4 4 "\nDaniel \n"]
>
[1 2 3 4 4 "\nDaniel \n"]
>
Tested with Golfscript 1.0 Beta, Ruby 3.0.6