Skip to content
Permalink
Browse files

Introduce :compile-only flag to EVAL

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 d41f162da02916eebc27018d918a556e9c9e71b3
Showing with 19 additions and 5 deletions.
  1. +19 −5 src/core/ForeignCode.pm6
@@ -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.
@@ -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) {

4 comments on commit d41f162

@lizmat

This comment has been minimized.

Copy link
Contributor Author

replied May 20, 2019

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

@jnthn

This comment has been minimized.

Copy link
Member

replied May 20, 2019

@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

This comment has been minimized.

Copy link
Contributor

replied May 20, 2019

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

@lizmat

This comment has been minimized.

Copy link
Contributor Author

replied May 20, 2019

Changes made as suggested in 5b3a8ce .

Please sign in to comment.
You can’t perform that action at this time.