perl.cvs.parrot
[Top] [All Lists]

[svn:parrot] r30681 - in trunk/languages/cardinal: src/parser t

Subject: [svn:parrot] r30681 - in trunk/languages/cardinal: src/parser t
From:
Date: Mon, 1 Sep 2008 09:49:16 -0700 PDT
Newsgroups: perl.cvs.parrot

Author: tene
Date: Mon Sep  1 09:49:15 2008
New Revision: 30681

Modified:
   trunk/languages/cardinal/src/parser/actions.pm
   trunk/languages/cardinal/src/parser/grammar.pg
   trunk/languages/cardinal/t/02-functions.t

Log:
[cardinal]
* Support default arguments to functions
* Add a test and convert another test file to use the test library


Modified: trunk/languages/cardinal/src/parser/actions.pm
==============================================================================
--- trunk/languages/cardinal/src/parser/actions.pm      (original)
+++ trunk/languages/cardinal/src/parser/actions.pm      Mon Sep  1 09:49:15 2008
@@ -519,10 +519,18 @@
     make $past;
 }
 
+method sig_identifier($/) {
+    my $past := $($<identifier>);
+    if +$<default> == 1 {
+        $past.viviself( $( $<default>[0] ) );
+    }
+    make $past;
+}
+
 method block_signature($/) {
     my $params := PAST::Stmts.new( :node($/) );
     my $past := PAST::Block.new($params, :blocktype('declaration'));
-    for $<identifier> {
+    for $<sig_identifier> {
         my $parameter := $( $_ );
         $past.symbol($parameter.name(), :scope('lexical'));
         $parameter.scope('parameter');
@@ -537,7 +545,7 @@
         $past.symbol($block.name(), :scope('lexical'));
         $params.push($block);
     }
-    $params.arity( +$<identifier> + +$<block_param> );
+    $params.arity( +$<sig_identifier> + +$<block_param> );
     our $?BLOCK_SIGNATURED := $past;
     make $past;
 }

Modified: trunk/languages/cardinal/src/parser/grammar.pg
==============================================================================
--- trunk/languages/cardinal/src/parser/grammar.pg      (original)
+++ trunk/languages/cardinal/src/parser/grammar.pg      Mon Sep  1 09:49:15 2008
@@ -150,9 +150,14 @@
     '|' <block_signature> '|'
 }
 
+rule sig_identifier {
+                      #XXX Should this be basic_primary or expr or what?
+    <identifier>[ '=' <default=basic_primary>]? {*}
+}
+
 rule block_signature {
     [
-    | <identifier> [',' <identifier>]* [',' <slurpy_param>]? [',' 
<block_param>]?
+    | <sig_identifier> [',' <sig_identifier>]* [',' <slurpy_param>]? [',' 
<block_param>]?
     | <slurpy_param> [',' <block_param>]?
     | <block_param>?
     ] {*}

Modified: trunk/languages/cardinal/t/02-functions.t
==============================================================================
--- trunk/languages/cardinal/t/02-functions.t   (original)
+++ trunk/languages/cardinal/t/02-functions.t   Mon Sep  1 09:49:15 2008
@@ -1,16 +1,18 @@
-puts "1..6"
+require 'test'
+
+plan 7
 
 def first
-    puts "ok 1"
+    ok 1
 end
 
 def second(n)
-    puts "ok ", n
+    is n%3, 2
 end
 
 def third(a,b)
-    second(a)
-    second(b)
+    ok a == 3
+    is b, 4
 end
 
 def fib(n)
@@ -25,10 +27,15 @@
     f(n)
 end
 
+def defaults(n=7)
+    is n, 7
+end
+
 first
 second(2)
 third(3,4)
 second fib(6) - 3;
-blocks(6) do |i|
-    puts 'ok ', i
+blocks('foo') do |i|
+    is i, 'foo'
 end
+defaults()

<Prev in Thread] Current Thread [Next in Thread>
  • [svn:parrot] r30681 - in trunk/languages/cardinal: src/parser t, tene <=