Skip to content

Commit

Permalink
Introduce :compile-only flag to EVAL
Browse files Browse the repository at this point in the history
Does all of the stuff it needs to do to run, but doesn't actually run it.
Returns Nil if compilation was ok, throws otherwise (as usual).  Inspired by:
https://stackoverflow.com/questions/56190676/checking-syntax-of-a-string-or-file
  • Loading branch information
lizmat committed May 20, 2019
1 parent e007642 commit d41f162
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/core/ForeignCode.pm6
Expand Up @@ -24,7 +24,14 @@ my class Rakudo::Internals::EvalIdSource {
$lock.protect: { $count++ }
}
}
proto sub EVAL($code is copy where Blob|Cool|Callable, Str() :$lang = 'perl6', PseudoStash :$context, Str() :$filename = Str, *%n) {
proto sub EVAL(
$code is copy where Blob|Cool|Callable,
Str() :$lang = 'perl6',
PseudoStash :$context,
Str() :$filename = Str,
Bool() :$compile-only = False,
*%_
) {
die "EVAL() in Perl 6 is intended to evaluate strings, did you mean 'try'?"
if nqp::istype($code,Callable);
# First look in compiler registry.
Expand Down Expand Up @@ -58,10 +65,17 @@ proto sub EVAL($code is copy where Blob|Cool|Callable, Str() :$lang = 'perl6', P
|(:optimize($_) with nqp::getcomp('perl6').cli-options<optimize>),
|(%(:grammar($LANG<MAIN>), :actions($LANG<MAIN-actions>)) if $LANG);

$*W.add_additional_frames(mast_frames)
if $*W and $*W.is_precompilation_mode; # we are still compiling
nqp::forceouterctx(nqp::getattr($compiled, ForeignCode, '$!do'), $eval_ctx);
$compiled();
if $compile-only {
Nil
}
else {
$*W.add_additional_frames(mast_frames)
if $*W and $*W.is_precompilation_mode; # we are still compiling
nqp::forceouterctx(
nqp::getattr($compiled,ForeignCode,'$!do'),$eval_ctx
);
$compiled()
}
}

multi sub EVAL($code, Str :$lang where { ($lang // '') eq 'Perl5' }, PseudoStash :$context, Str() :$filename = Str) {
Expand Down

4 comments on commit d41f162

@lizmat
Copy link
Contributor Author

@lizmat lizmat commented on d41f162 May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnthn: hope this is ok, seemed like a nice feature to have. If not, please revert.

@jnthn
Copy link
Member

@jnthn jnthn commented on d41f162 May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lizmat I guess as a feature it's OK, though I wonder if :check would not be a more succinct and appropriate name (meaning both of "run it up to CHECK time" and "just check the syntax"). Also, what happens if we use this with, say, :lang<Perl5>? (I'd not expect it to support the feature without work, but I would expect it to not silently just go ahead and run the code...)

@niner
Copy link
Collaborator

@niner niner commented on d41f162 May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to make Perl6::Compiler available?

@lizmat
Copy link
Contributor Author

@lizmat lizmat commented on d41f162 May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made as suggested in 5b3a8ce0ed .

Please sign in to comment.