Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for REPL multiline input
When an input line ends with '\', wait for more code before executing.
  • Loading branch information
retupmoca committed Apr 16, 2014
1 parent 9daec9e commit 74875ba
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/HLL/Compiler.nqp
Expand Up @@ -62,15 +62,28 @@ class HLL::Compiler does HLL::Backend::Default {

my $target := nqp::lc(%adverbs<target>);
my $save_ctx;
my $prompt := self.interactive_prompt // '> ';
my $code;
while 1 {
last if nqp::eoffh($stdin);

my $prompt := self.interactive_prompt // '> ';
my $code := nqp::readlineintfh($stdin, ~$prompt);
if nqp::isnull($code) || !nqp::defined($code) {
my $newcode := nqp::readlineintfh($stdin, ~$prompt);
if nqp::isnull($newcode) || !nqp::defined($newcode) {
nqp::print("\n");
last;
}
if $newcode {
$code := $code ~ $newcode;
}

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

# Set the current position of stdout for autoprinting control
my $*AUTOPRINTPOS := nqp::tellfh(nqp::getstdout());
Expand All @@ -89,6 +102,10 @@ class HLL::Compiler does HLL::Backend::Default {
if nqp::defined($*MAIN_CTX) {
$save_ctx := $*MAIN_CTX;
}

$code := "";
$prompt := self.interactive_prompt // '> ';

next unless nqp::defined($output);
next if nqp::isnull($output);

Expand Down

0 comments on commit 74875ba

Please sign in to comment.