Skip to content

Add a repl sub to enter the repl from within a program#4325

Merged
lizmat merged 7 commits intomasterfrom
REPL-now
May 25, 2021
Merged

Add a repl sub to enter the repl from within a program#4325
lizmat merged 7 commits intomasterfrom
REPL-now

Conversation

@lizmat
Copy link
Copy Markdown
Contributor

@lizmat lizmat commented Apr 13, 2021

This would allow one to enter the REPL from a non-interactive program.
Ideally, this should be aware of its context, but apparently setting
$!save_ctx is not enough. Maybe @vrurg has some ideas about that.

Sorta inspired by some comments on the #raku channel today.

@Altai-man
Copy link
Copy Markdown
Member

No merge before release.

@ugexe
Copy link
Copy Markdown
Member

ugexe commented Apr 13, 2021

We should avoid methods named identically to those of other core methods that do something completely different (I still 🤦 over creating CUR.candidates when Routine.candidates already existed)

@lizmat
Copy link
Copy Markdown
Contributor Author

lizmat commented Apr 13, 2021

@ugexe Good point. I chose "now" because, well because I want a REPL now. Maybe here would be a better name.

Any suggestions on making it see its context?

@lizmat lizmat marked this pull request as draft April 13, 2021 20:55
@lizmat
Copy link
Copy Markdown
Contributor Author

lizmat commented Apr 13, 2021

@Altai-man Wasn't planning on merging this before release. I changed it to "Draft" to make that clearer!

@vrurg
Copy link
Copy Markdown
Member

vrurg commented Apr 13, 2021

@lizmat Unfortunately, I can't be really helpful with the context. It looks like it should work. Except that, perhaps, passing it as :outerctx($caller-ctx) argument to repl-loop method would do the same but cleaner. I then traced it down to HLL::Compier eval method where it is used exactly for that – to set the outer context of the compiled code. Hence can't tell what's going on without debugging. Will see if I can spare some time for this, but can't make any promise now.

@ugexe
Copy link
Copy Markdown
Member

ugexe commented Apr 13, 2021

here seems too generic for how often I expect this specific functionality to be used... after all I literally want any code I write to be here (and usually for it to run now).

I'd probably go more towards something like REPL.enter-foo-mode or REPL.initiate-loop

@vrurg
Copy link
Copy Markdown
Member

vrurg commented Apr 13, 2021

I'd probably go more towards something like REPL.enter-foo-mode or REPL.initiate-loop

enter-loop?

@lizmat
Copy link
Copy Markdown
Contributor Author

lizmat commented Apr 13, 2021

FWIW, I see this as a debugging tool, so I would like to have it as short as possible.

Making it a sub called repl might also be an option?

Comment thread src/core.c/REPL.pm6 Outdated

method here() {
my $repl := self.new(nqp::getcomp("Raku"),%_);
nqp::bindattr($repl,REPL,'$!save_ctx',nqp::ctxcaller(nqp::ctx));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't going to go well. The compiler is free to optimize out lexicals as it pleases. We do have a list of magical sub names, such as EVAL, which inhibit compiler optimization in all outer scopes. (Others include eval-lives-ok for Test.) I'm not thrilled about this, but at least hope that list can go away in the future by making all such things instead be macros that do...something...to inhibit the optimizations (the point being that the compiler doesn't need to keep a magical list of names).

But that means the things in question should be sub calls today, not method calls. We do, I grant, have a .EVAL method, which I consider an unfortunate mistake, although we could make that a macro postfix:<.EVAL> or something (and hope it wins on the LTM longest literal prefix rule). (I'd personally lean towards deprecation, though.)

Anyway, if we really should have this, please make it a sub. It'll have to go in the same magical list of optimization inhibitors that EVAL is also in.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, I will make it a sub called repl.

@jnthn where does the magical sub names list live?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, I will make it a sub called repl.

That's succinct.

where does the magical sub names list live?

src/Perl6/Optimizer.nqp I believe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Aah, that's why I couldn't find it: eval-lives-ok is not in that list, but throws-like is.

@lizmat lizmat changed the title Add a REPL.now method Add a repl sub to enter the repl from within a program Apr 19, 2021
@lizmat lizmat changed the title Add a repl sub to enter the repl from within a program Add a repl sub to enter the repl from within a program Apr 19, 2021
lizmat added 5 commits May 18, 2021 12:22
This would allow one to enter the REPL from a non-interactive program.
Ideally, this should be aware of its context, but apparently setting
$!save_ctx is not enough.  Maybe @vrurg has some ideas about that.

Sorta inspired by some comments on the #raku channel today.
As indirectly suggested by @ugexe
As suggested by jnthn.  Since this is now a globally visible sub,
make sure the local tests are adapted for it.
This makes the REPL in:

    my $a = 42; repl; say $a

actually know about $a, and allow it to be changed, and see the result
of the change afterwards.  jnthn++ for the hint.
But only if called from the repl() sub
@lizmat lizmat marked this pull request as ready for review May 18, 2021 11:23
@lizmat
Copy link
Copy Markdown
Contributor Author

lizmat commented May 18, 2021

Trying to catch a user doing Ctrl-d, or resetting the eof / error flag on STDIN, is still out of reach. But at least typing "exit" now in repl will not actually exit the program, but just exit the repl-loop, making it re-entrant and thus a simple debugging tool as well.

Therefore I marked this PR now ready for review. As this is new functionality not touching on current functionality, I'd say it's pretty safe to merge before 2021.05 release.

To make clear that typing "exit" is currently the only sane way
to exit the repl (without exiting the program).

Also prevent warning when CTRL-d ing when called as "repl".
@lizmat lizmat merged commit 4183cda into master May 25, 2021
@Altai-man Altai-man deleted the REPL-now branch June 19, 2021 15:14
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.

5 participants