Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move continuation detection into eval
With this change, eval can return the output of running the code, or
a sentinel value that indicates that more input is needed to run a chunk
of code.  This is so that more advanced forms of detecting when we need
more input, such as if the user is entering input for a multi-line
statement like a block.

This is just a proof-of-concept change; I don't really like how eval
indicates to its caller that it needs more input.  But at least this
will get people to try and see if multi-line input could be feasible.

We also add a new method to HLL::Compiler - needs-more-input, which is
used in conjunction with the sentinel value returned by eval.
  • Loading branch information
hoelzro committed Feb 11, 2016
1 parent afe4963 commit a59d662
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/HLL/Compiler.nqp
@@ -1,3 +1,5 @@
my $more-code-sentinel := nqp::hash();

# Provides base functionality for a compiler object.
class HLL::Compiler does HLL::Backend::Default {
has @!stages;
Expand Down Expand Up @@ -86,16 +88,6 @@ class HLL::Compiler does HLL::Backend::Default {
$code := $code ~ $newcode;
}

if $newcode && nqp::substr($newcode, nqp::chars($newcode) - 1) eq "\\" {
# Need to get more code before we execute
# Strip the trailing \, but reinstate the newline
$code := nqp::substr($code, 0, nqp::chars($code) - 1) ~ "\n";
if $code {
$prompt := '* ';
}
next;
}

# Set the current position of stdout for autoprinting control
my $*AUTOPRINTPOS := nqp::tellfh(nqp::getstdout());
my $*CTXSAVE := self;
Expand All @@ -110,6 +102,15 @@ class HLL::Compiler does HLL::Backend::Default {
self.interactive_exception($!);
}
};
if self.input-incomplete($output) {
# Need to get more code before we execute
# Strip the trailing \, but reinstate the newline
$code := nqp::substr($code, 0, nqp::chars($code) - 1) ~ "\n";
if $code {
$prompt := '* ';
}
next;
}
if nqp::defined($*MAIN_CTX) {
$!save_ctx := $*MAIN_CTX;
}
Expand Down Expand Up @@ -139,9 +140,21 @@ class HLL::Compiler does HLL::Backend::Default {
nqp::print(~$ex ~ "\n")
}

method input-incomplete($value) {
nqp::where($value) == nqp::where($more-code-sentinel);
}

method needs-more-input() {
$more-code-sentinel
}

method eval($code, *@args, *%adverbs) {
my $output;

if $code && nqp::substr($code, nqp::chars($code) - 2) eq "\\\n" {
return self.needs-more-input();
}

if (%adverbs<profile-compile>) {
$output := $!backend.run_profiled({
self.compile($code, :compunit_ok(1), |%adverbs);
Expand Down

0 comments on commit a59d662

Please sign in to comment.