Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Add a more lexically-aware interactive REPL. Still some tweaks wanted.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed May 26, 2010
1 parent c5659bb commit eaa4deb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/PARROT_REVISION
@@ -1 +1 @@
46979
47014
22 changes: 20 additions & 2 deletions src/HLL/Compiler.pm
Expand Up @@ -108,17 +108,26 @@ class HLL::Compiler is PCT::HLLCompiler {

# Set the current position of stdout for autoprinting control
my $*AUTOPRINTPOS := pir::tell__IP(pir::getstdout__P());
our $interactive_ctx;
our %interactive_pad;
my $*CTXSAVE := self;
my $*MAIN_CTX;

if $code {
$code := $code ~ "\n";
my $output;
{
$output := self.eval($code, |%adverbs);
$output := self.eval($code, :outer_ctx($interactive_ctx), |%adverbs);
CATCH {
pir::print(~$! ~ "\n");
next;
}
};
if pir::defined($*MAIN_CTX) {
for $*MAIN_CTX.lexpad_full() {
%interactive_pad{$_.key} := $_.value;
}
}
next if pir::isnull($output);

if !$target {
Expand All @@ -140,7 +149,7 @@ class HLL::Compiler is PCT::HLLCompiler {
&& %adverbs<target> eq '' {
my $outer_ctx := %adverbs<outer_ctx>;
if pir::defined($outer_ctx) {
$output[0].set_outer(pir::getattribute__PPs($outer_ctx, 'current_sub'));
$output[0].set_outer_ctx($outer_ctx);
}

pir::trace(%adverbs<trace>);
Expand All @@ -150,4 +159,13 @@ class HLL::Compiler is PCT::HLLCompiler {

$output;
}

method ctxsave() {
$*MAIN_CTX :=
Q:PIR {
$P0 = getinterp
%r = $P0['context';1]
};
$*CTXSAVE := 0;
}
}
4 changes: 4 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -50,6 +50,10 @@ method comp_unit($/) {
my $mainline := $<statementlist>.ast;
my $unit := @BLOCK.shift;

# If our caller wants to know the mainline ctx, provide it here.
# (CTXSAVE is inherited from HLL::Actions.)
$unit.push( self.CTXSAVE() );

# We force a return here, because we have other
# :load/:init blocks to execute that we don't want
# to include as part of the mainline.
Expand Down
21 changes: 21 additions & 0 deletions src/cheats/hll-compiler.pir
Expand Up @@ -103,3 +103,24 @@
# return the result
.return (ns)
.end


# This sub serves as a cumulative "outer context" for code
# executed in HLL::Compiler's interactive REPL. It's invoked
# exactly once upon load/init to obtain a context, and its
# default LexPad is replaced with a Hash that we can use to
# cumulatively store outer context information. Both the
# context and hash are then made available via package
# variables.
.namespace []
.sub '&interactive_outer' :lex :init :load
.local pmc ctx, pad
$P0 = getinterp
ctx = $P0['context']
set_global ['HLL';'Compiler'], '$interactive_ctx', ctx
pad = getattribute ctx, 'lex_pad'
$P1 = new ['Hash']
copy pad, $P1
set_global ['HLL';'Compiler'], '%interactive_pad', pad
.end

0 comments on commit eaa4deb

Please sign in to comment.