Author: allison
Date: Thu Jan 12 13:26:12 2006
New Revision: 11131
Modified:
trunk/ (props changed)
trunk/languages/punie/lib/past2post.g
trunk/languages/punie/lib/pge2past.g
trunk/languages/punie/lib/post2pir.g
trunk/languages/punie/lib/punie.g
trunk/languages/punie/t/io_print.t
Log:
Support comma separated lists in Punie.
Modified: trunk/languages/punie/lib/past2post.g
==============================================================================
--- trunk/languages/punie/lib/past2post.g (original)
+++ trunk/languages/punie/lib/past2post.g Thu Jan 12 13:26:12 2006
@@ -49,6 +49,19 @@ PAST::Op: result(.) = {
push newchildren, $P3
goto iter_loop
iter_end:
+
+ # In the context of an Op node, collapse a child comma op, so the
+ # child's children become the children of the current node.
+ $I0 = elements newchildren
+ if $I0 > 1 goto no_munge
+ $P5 = newchildren[0]
+ $S3 = typeof $P5
+ unless $S3 == 'POST::Op' goto no_munge
+ $S4 = $P5.op()
+ unless $S4 == 'O_COMMA' goto no_munge
+ newchildren = $P5.children()
+
+ no_munge:
# The results for the children become the children of the new node.
$S1 = node.source()
$I1 = node.pos()
Modified: trunk/languages/punie/lib/pge2past.g
==============================================================================
--- trunk/languages/punie/lib/pge2past.g (original)
+++ trunk/languages/punie/lib/pge2past.g Thu Jan 12 13:26:12 2006
@@ -123,6 +123,43 @@ PunieGrammar::gprint: result(.) = {
.return (result)
}
+PunieGrammar::cexpr: result(.) = {
+ .local pmc result
+ $I0 = defined node["PunieGrammar::term"]
+ unless $I0 goto err_no_term
+ $P1 = node["PunieGrammar::term"]
+ .local pmc children
+ children = new PerlArray
+ $P0 = new Iterator, $P1 # setup iterator for node
+ set $P0, 0 # reset iterator, begin at start
+ iter_loop:
+ unless $P0, iter_end # while (entries) ...
+ shift $P2, $P0 # get next entry
+ $P3 = tree.get('result', $P2, 'PunieGrammar::term')
+ push children, $P3
+ goto iter_loop
+ iter_end:
+
+ # If there's only one child node, it's a single element, not a list,
+ # so just return the single element. Otherwise, we have a comma
+ # separated list, so build a comma op node.
+ $I0 = elements children
+ unless $I0 > 1 goto no_comma
+ result = new 'PAST::Op'
+ # get the source string and position offset from start of source
+ # code for this match node
+ $S2 = node
+ $I3 = node.from()
+ result.set_node($S2,$I3,'O_COMMA',children)
+ .return (result)
+ no_comma:
+ result = shift children # there's only one result
+ .return (result)
+ err_no_term:
+ print "The cexpr node doesn't contain a 'term' match.\n"
+ end
+}
+
PunieGrammar::term: result(.) = {
.local pmc result
result = new 'PAST::Val'
Modified: trunk/languages/punie/lib/post2pir.g
==============================================================================
--- trunk/languages/punie/lib/post2pir.g (original)
+++ trunk/languages/punie/lib/post2pir.g Thu Jan 12 13:26:12 2006
@@ -21,19 +21,37 @@ ROOT: result(.) = {
POST::Op: result(.) = {
.local string output
- $S1 = node.op()
- output = " " . $S1
- output .= " "
+ .local int counter
+ counter = 0
+ .local string opname
+ opname = node.op()
+ $I1 = opname == 'print'
+ if $I1 goto no_lead
+ output = " " . opname
+ output .= " "
+ no_lead:
$P1 = node.children()
.local pmc iter
iter = new Iterator, $P1 # setup iterator for node
iter = 0
iter_loop:
- unless iter, iter_end # while (entries) ...
+ unless iter goto iter_end # while (entries) ...
shift $P2, iter
+ inc counter
$S3 = tree.get('result', $P2)
+ if $I1 goto repeat_opname
+ if counter <= 1 goto no_comma_out
+ output .= ", "
+ no_comma_out:
output .= $S3
goto iter_loop
+ repeat_opname:
+ output .= " "
+ output .= opname
+ output .= " "
+ output .= $S3
+ output .= "\n"
+ goto iter_loop
iter_end:
output .= "\n"
.return (output)
Modified: trunk/languages/punie/lib/punie.g
==============================================================================
--- trunk/languages/punie/lib/punie.g (original)
+++ trunk/languages/punie/lib/punie.g Thu Jan 12 13:26:12 2006
@@ -1,7 +1,12 @@
grammar PunieGrammar;
rule term { \d+ | <PGE::Text::bracketed: "> }
rule gprint { (print) \s* <PunieGrammar::expr> }
-rule expr { <PunieGrammar::gprint> | <PunieGrammar::term> }
+rule expr { <PunieGrammar::gprint> | <PunieGrammar::cexpr> }
rule line { \s*<PunieGrammar::expr>;\s* }
rule lineseq { \s*<PunieGrammar::line>*\s* }
rule prog { ^<PunieGrammar::lineseq>$ }
+
+rule cexpr {
+ <PunieGrammar::term> \s* [, \s* <PunieGrammar::term>]*
+}
+
Modified: trunk/languages/punie/t/io_print.t
==============================================================================
--- trunk/languages/punie/t/io_print.t (original)
+++ trunk/languages/punie/t/io_print.t Thu Jan 12 13:26:12 2006
@@ -2,7 +2,7 @@
use strict;
use lib qw(t . lib ../lib ../../lib ../../../lib);
-use Parrot::Test tests => 5;
+use Parrot::Test tests => 6;
language_output_is('punie', <<'EOC', '1', 'printing one');
print 1;
@@ -30,3 +30,11 @@ ok 1
ok 2
OUT
+language_output_is('punie', <<'CODE', <<'OUT', 'printing list of strings');
+print "ok 2\n","ok 3\n","ok 4\n","ok 5\n";
+CODE
+ok 2
+ok 3
+ok 4
+ok 5
+OUT