Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fixes for the --target option (take 2) #19

Closed
wants to merge 2 commits into from

3 participants

@felliott

Hello,

(These have been rebased and retested against pmichaud's latest commits)

Here are two patches to improve handling of the --target option. The first simply validates that the given target is a valid member of @!stages and panics if not. The second enables --target={parse,past,post,evalpmc} when run with -e. PAST output is currently broken, but that issue is unrelated to these patches.

Cheers,
Fitz Elliott

@rakudo

Thanks for the patches! However, I don't want to enforce validation on --target -- it's entirely valid for a compiler to intercept --target without having to place it as an explicit stage. The fix for --target=pir and -e is somewhat close to what I want, but I'm really looking for a cleaner refactor that works with both interactive mode and command_line output.

Pm

@pmichaud pmichaud closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 14 deletions.
  1. +33 −14 src/HLL/Compiler.pm
View
47 src/HLL/Compiler.pm
@@ -302,6 +302,7 @@ class HLL::Compiler {
self.version if %adverbs<version>;
self.show-config if %adverbs<show-config>;
self.nqpevent(%adverbs<nqpevent>) if %adverbs<nqpevent>;
+ self.validate_target(%adverbs<target>) if (%adverbs<target>);
my $result;
if %adverbs<e> { $result := self.eval(%adverbs<e>, |@a, |%adverbs) }
@@ -309,14 +310,18 @@ class HLL::Compiler {
elsif %adverbs<combine> { $result := self.evalfiles(@a, |%adverbs) }
else { $result := self.evalfiles(@a[0], |@a, |%adverbs) }
- if !pir::isnull($result) && %adverbs<target> eq 'pir' {
- my $output := %adverbs<output>;
- my $fh := ($output eq '' || $output eq '-')
- ?? pir::getinterp__P().stdout_handle()
- !! pir::new__Ps('FileHandle').open($output, 'w');
- self.panic("Cannot write to $output") unless $fh;
- pir::print($fh, $result);
- $fh.close()
+ if %adverbs<e> || @a {
+ $result := self.target_output($result, |%adverbs);
+
+ if !pir::isnull($result) && %adverbs<target> eq 'pir' {
+ my $output := %adverbs<output>;
+ my $fh := ($output eq '' || $output eq '-')
+ ?? pir::getinterp__P().stdout_handle()
+ !! pir::new__Ps('FileHandle').open($output, 'w');
+ self.panic("Cannot write to $output") unless $fh;
+ pir::print($fh, $result);
+ $fh.close()
+ }
}
}
@@ -362,12 +367,7 @@ class HLL::Compiler {
}
my $code := pir::join('', @codes);
my $?FILES := pir::join(' ', @files);
- my $r := self.eval($code, |@args, |%adverbs);
- if $target eq '' || $target eq 'pir' {
- return $r;
- } else {
- return self.dumper($r, $target, |%adverbs);
- }
+ return self.eval($code, |@args, |%adverbs);
}
method compile($source, *%adverbs) {
@@ -438,6 +438,14 @@ class HLL::Compiler {
$compiler($source)
}
+ method target_output($result, *%adverbs) {
+ if !%adverbs<target> || %adverbs<target> eq 'pir' {
+ return $result;
+ } else {
+ return self.dumper($result, %adverbs<target>, |%adverbs);
+ }
+ }
+
method dumper($obj, $name, *%options) {
if %options<dumper> {
pir::load_bytecode('PCT/Dumper.pbc');
@@ -449,6 +457,17 @@ class HLL::Compiler {
}
}
+ method validate_target($target) {
+ my $seen := 0;
+ for @!stages {
+ if $_ eq $target {
+ $seen := 1;
+ last;
+ }
+ }
+ self.panic("\"$target\" is not a valid target") unless $seen;
+ }
+
method usage($name?) {
if $name {
say($name);
Something went wrong with that request. Please try again.