Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Created November 11, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/ed414755e6b0d1a5752cb93a337eb224 to your computer and use it in GitHub Desktop.
Save MasterDuke17/ed414755e6b0d1a5752cb93a337eb224 to your computer and use it in GitHub Desktop.
nqp level file/line
diff --git a/src/HLL/Compiler.nqp b/src/HLL/Compiler.nqp
index b605706..5781211 100644
--- a/src/HLL/Compiler.nqp
+++ b/src/HLL/Compiler.nqp
@@ -617,7 +617,7 @@ class HLL::Compiler does HLL::Backend::Default {
if $cache {
$*LINEPOSCACHE := $linepos;
}
- my str $s := ~$target;
+ my str $s := $target;
my int $eos := nqp::chars($s);
my int $jpos := 0;
my int $ord;
diff --git a/src/vm/moar/QAST/QASTCompilerMAST.nqp b/src/vm/moar/QAST/QASTCompilerMAST.nqp
index b05ea54..0062e5e 100644
--- a/src/vm/moar/QAST/QASTCompilerMAST.nqp
+++ b/src/vm/moar/QAST/QASTCompilerMAST.nqp
@@ -20,6 +20,9 @@ my class MASTCompilerInstance {
# The most recent op we tried to compile, for error reporting.
has $!last_op;
+ # original source file lines
+ has %!source_lines;
+
# This uses a very simple scheme. Write registers are assumed
# to be write-once, read-once. Therefore, if a QAST control
# structure wants to reuse the intermediate result of an
@@ -1402,9 +1405,28 @@ my class MASTCompilerInstance {
# Annotate with line number if we have one.
my $node := $_.node;
if nqp::isconcrete($node) && nqp::can($node,'orig') {
- my $line := HLL::Compiler.lineof($node.orig(), $node.from(), :cache(1));
+ my str $target := ~$node.orig();
+ my int $pos := $node.from();
+ my str $file := $!file;
+ my str $line_comment = "\n#line 1 ";
+ my int $sf_index_s := nqp::rindex($target, $line_comment, $pos);
+ if $sf_index_s >= 0 {
+ my int $i := $sf_index_s + nqp::chars($line_comment);
+ $file := nqp::substr($target, $i, nqp::index($target, "\n", $i) - $i);
+ }
+ my int $s_line := 0;
+ if $sf_index_s >= 0 {
+ if nqp::existskey(%!source_lines, $sf_index_s) {
+ $s_line := %!source_lines{$sf_index_s};
+ }
+ else {
+ $s_line := HLL::Compiler.lineof($target, $sf_index_s, :cache(1)) - 1;
+ %!source_lines{$sf_index_s} := $s_line;
+ }
+ }
+ my $line := HLL::Compiler.lineof($target, $pos, :cache(1)) - $s_line;
nqp::push(@all_ins, MAST::Annotated.new(
- :$!file, :$line, :instructions($last_stmt.instructions) ));
+ :$file, :$line, :instructions($last_stmt.instructions) ));
}
else {
nqp::splice(@all_ins, $last_stmt.instructions, +@all_ins, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment