From def10fd60c43741c13d6de45b7386568922c412f Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Wed, 12 May 2010 22:08:08 -0700 Subject: [PATCH 1/5] Write NQP replacements for eval and interactive --- src/HLL/Compiler.pm | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/HLL/Compiler.pm b/src/HLL/Compiler.pm index 3ebe8b9..4619b32 100644 --- a/src/HLL/Compiler.pm +++ b/src/HLL/Compiler.pm @@ -82,4 +82,65 @@ class HLL::Compiler is PCT::HLLCompiler { } } + method interactive(*%adverbs) { + my $target := pir::downcase(%adverbs); + + pir::printerr__vS(self.commandline_banner); + + my $stdin := pir::getstdin__P(); + my $encoding := ~%adverbs; + if $encoding && $encoding ne 'fixed_8' { + $stdin.encoding($encoding); + } + + while 1 { + last unless $stdin; + + my $prompt := self.commandline_prompt // '> '; + my $code := $stdin.readline_interactive(~$prompt); + + last if pir::isnull($code); + + if $code { + $code := $code ~ "\n"; + my $output; + { + $output := self.eval($code, |%adverbs); + CATCH { + pir::print($! ~ "\n"); + next; + } + }; + next if pir::isnull($output); + + if $target { + if $target eq 'pir' { + say($output); + } else { + self.dumper($output, $target, |%adverbs); + } + } + } + } + } + + method eval($code, *@args, *%adverbs) { + my $output; my $outer; + $output := self.compile($code, |%adverbs); + + if !pir::isa($output, 'String') + && %adverbs eq '' { + $outer := %adverbs; + + unless pir::isnull($outer) { + $output[0].set_outer($outer); + } + + pir::trace(%adverbs); + $output := $output[0](); + pir::trace(0); + } + + $output; + } } From 528fb676886eaa380469cc11ae87ac3f9e0f7ddd Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Fri, 14 May 2010 12:58:52 -0700 Subject: [PATCH 2/5] Add the autoprint hook --- src/HLL/Compiler.pm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/HLL/Compiler.pm b/src/HLL/Compiler.pm index 4619b32..c54ca62 100644 --- a/src/HLL/Compiler.pm +++ b/src/HLL/Compiler.pm @@ -82,6 +82,10 @@ class HLL::Compiler is PCT::HLLCompiler { } } + method autoprint($value) { + pir::say(~$value); + } + method interactive(*%adverbs) { my $target := pir::downcase(%adverbs); @@ -113,12 +117,12 @@ class HLL::Compiler is PCT::HLLCompiler { }; next if pir::isnull($output); - if $target { - if $target eq 'pir' { - say($output); - } else { - self.dumper($output, $target, |%adverbs); - } + if !$target { + self.autoprint($output); + } elsif $target eq 'pir' { + pir::say($output); + } else { + self.dumper($output, $target, |%adverbs); } } } From c4649e11887874182d9bd2a56be6a02c00499834 Mon Sep 17 00:00:00 2001 From: pmichaud Date: Fri, 21 May 2010 13:33:59 -0500 Subject: [PATCH 3/5] Refactor outer_ctx handling a bit in HLL::Compiler. --- src/HLL/Compiler.pm | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/HLL/Compiler.pm b/src/HLL/Compiler.pm index c54ca62..09e4135 100644 --- a/src/HLL/Compiler.pm +++ b/src/HLL/Compiler.pm @@ -129,19 +129,18 @@ class HLL::Compiler is PCT::HLLCompiler { } method eval($code, *@args, *%adverbs) { - my $output; my $outer; + my $output; $output := self.compile($code, |%adverbs); if !pir::isa($output, 'String') && %adverbs eq '' { - $outer := %adverbs; - - unless pir::isnull($outer) { - $output[0].set_outer($outer); + my $outer_ctx := %adverbs; + if pir::defined($outer_ctx) { + $output[0].set_outer($outer_ctx); } pir::trace(%adverbs); - $output := $output[0](); + $output := $output(); pir::trace(0); } From fca4ee177643413f80ac426e9b565f0a62c578c1 Mon Sep 17 00:00:00 2001 From: pmichaud Date: Fri, 21 May 2010 13:36:10 -0500 Subject: [PATCH 4/5] HLL::Compiler.eval() needs to pass its slurpy args to the code it evals. --- src/HLL/Compiler.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HLL/Compiler.pm b/src/HLL/Compiler.pm index 09e4135..8dec588 100644 --- a/src/HLL/Compiler.pm +++ b/src/HLL/Compiler.pm @@ -140,7 +140,7 @@ class HLL::Compiler is PCT::HLLCompiler { } pir::trace(%adverbs); - $output := $output(); + $output := $output(|@args); pir::trace(0); } From 3ad311437df65191e7a63f4434409d47b2f3a8dd Mon Sep 17 00:00:00 2001 From: pmichaud Date: Fri, 21 May 2010 14:10:33 -0500 Subject: [PATCH 5/5] Improve autoprint so that it only outputs the result value if the executed code didn't itself send anything to stdout. Coke++ --- src/HLL/Compiler.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/HLL/Compiler.pm b/src/HLL/Compiler.pm index 8dec588..50d7522 100644 --- a/src/HLL/Compiler.pm +++ b/src/HLL/Compiler.pm @@ -83,7 +83,8 @@ class HLL::Compiler is PCT::HLLCompiler { } method autoprint($value) { - pir::say(~$value); + pir::say(~$value) + unless pir::tell__IP(pir::getstdout__P()) > $*AUTOPRINTPOS; } method interactive(*%adverbs) { @@ -91,7 +92,7 @@ class HLL::Compiler is PCT::HLLCompiler { pir::printerr__vS(self.commandline_banner); - my $stdin := pir::getstdin__P(); + my $stdin := pir::getstdin__P(); my $encoding := ~%adverbs; if $encoding && $encoding ne 'fixed_8' { $stdin.encoding($encoding); @@ -105,6 +106,9 @@ class HLL::Compiler is PCT::HLLCompiler { last if pir::isnull($code); + # Set the current position of stdout for autoprinting control + my $*AUTOPRINTPOS := pir::tell__IP(pir::getstdout__P()); + if $code { $code := $code ~ "\n"; my $output;