Skip to content

Instantly share code, notes, and snippets.

@regnarg
Created December 28, 2016 09:23
Show Gist options
  • Save regnarg/3b98a0b5f3348de8886bacc1bb0da3ff to your computer and use it in GitHub Desktop.
Save regnarg/3b98a0b5f3348de8886bacc1bb0da3ff to your computer and use it in GitHub Desktop.
Run Perl 6 REPL in context of a script
#!/usr/bin/perl6
use nqp;
class MyREPL is REPL {
# Simplified copy-paste of one iteration of repl-loop
method eval-in-ctx($code, *%adverbs) {
my $*CTXSAVE := self;
my $*MAIN_CTX;
my $ctx_attr = self.^attributes.first('$!save_ctx');
my $output = self.repl-eval(
$code,
:outer_ctx($ctx_attr.get_value(self)),
|%adverbs);
if self.input-incomplete($output) {
die "Incomplete input";
}
if self.input-toplevel-control($output) {
die "Control flow commands not allowed in toplevel";
}
if $*MAIN_CTX {
#$!save_ctx := $*MAIN_CTX;
$ctx_attr.set_value(self, $*MAIN_CTX);
}
}
}
my $comp = nqp::getcomp('perl6');
my $repl = MyREPL.new($comp, {});
sub MAIN(Str $init-file) {
# XXX HACK to prevent runnng &MAIN from $init-file
my @*MODULES = ["xxx"];
$repl.eval-in-ctx: $init-file.IO.slurp;
$repl.repl-loop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment