Skip to content

Commit 8bc0dbe

Browse files
committed
[truffle] Implement dumping of the TAST
./nqp-j nqp-truffle.nqp --target=tast -e 'nqp::say("Hello World")'
1 parent 85a2b55 commit 8bc0dbe

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

nqp-truffle.nqp

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ my $T_INT := 1;
33
my $T_NUM := 2;
44
my $T_STR := 3;
55

6+
class TAST {
7+
has $!tree;
8+
method run() {
9+
nqp::runtruffle($!tree);
10+
}
11+
12+
sub sexpr(int $unquoted, $thing) {
13+
if nqp::islist($thing) {
14+
my @ret;
15+
my int $first := 1;
16+
for $thing -> $element {
17+
nqp::push(@ret, sexpr($first, $element));
18+
$first := 0;
19+
}
20+
'(' ~ nqp::join(' ', @ret) ~ ')';
21+
} elsif nqp::isstr($thing) {
22+
$unquoted ?? $thing !! '"' ~ nqp::escape($thing) ~ '"';
23+
}
24+
}
25+
26+
method dump() {
27+
sexpr(0, $!tree) ~ "\n";
28+
}
29+
}
30+
631
class QAST::OperationsTruffle {
732
my %ops;
833
my %hll_ops;
@@ -81,23 +106,8 @@ class QAST::OperationsTruffle {
81106
}
82107

83108
class QAST::TruffleCompiler {
84-
method sexpr($thing) {
85-
if nqp::islist($thing) {
86-
my @ret;
87-
for $thing -> $element {
88-
nqp::push(@ret, self.sexpr($element));
89-
}
90-
'(' ~ nqp::join(' ', @ret) ~ ')';
91-
} elsif nqp::isstr($thing) {
92-
$thing;
93-
}
94-
}
95-
96-
method run(QAST::CompUnit $cu) {
97-
my $compiled := self.as_truffle($cu, :want($T_OBJ));
98-
99-
say(self.sexpr($compiled));
100-
nqp::runtruffle($compiled);
109+
method compile(QAST::CompUnit $cu) {
110+
TAST.new(tree => self.as_truffle($cu, :want($T_OBJ)));
101111
}
102112

103113
proto method as_truffle($node, :$want) {
@@ -144,12 +154,16 @@ class QAST::TruffleCompiler {
144154

145155
class TruffleBackend {
146156
method stages() {
147-
'truffle'
157+
'tast truffle'
158+
}
159+
160+
method tast($qast, *%adverbs) {
161+
QAST::TruffleCompiler.compile($qast);
148162
}
149163

150-
method truffle($qast, *%adverbs) {
164+
method truffle($tast, *%adverbs) {
151165
sub (*@args) {
152-
QAST::TruffleCompiler.run($qast);
166+
$tast.run();
153167
}
154168
}
155169

0 commit comments

Comments
 (0)