Author: bernhard
Date: Sat Nov 29 05:21:00 2008
New Revision: 33339
Modified:
trunk/languages/pipp/CREDITS
trunk/languages/pipp/src/common/guts.pir
trunk/languages/pipp/src/pct/actions.pm
trunk/languages/pipp/src/pct/grammar.pg
trunk/languages/pipp/t/php/functions.t
Log:
[Pipp] Add support for a return value from a user defined function.
Returning a variable doesn't work yet.
Modified: trunk/languages/pipp/CREDITS
==============================================================================
--- trunk/languages/pipp/CREDITS (original)
+++ trunk/languages/pipp/CREDITS Sat Nov 29 05:21:00 2008
@@ -22,4 +22,5 @@
N: Jimmy Zhuo
E: [EMAIL PROTECTED]
D: Bugfixes
+D: Implementation of functions
D: Code beautification
Modified: trunk/languages/pipp/src/common/guts.pir
==============================================================================
--- trunk/languages/pipp/src/common/guts.pir (original)
+++ trunk/languages/pipp/src/common/guts.pir Sat Nov 29 05:21:00 2008
@@ -102,6 +102,25 @@
die $S0
.end
+
+.include 'except_types.pasm'
+.include 'except_severity.pasm'
+
+.sub 'return'
+ .param pmc value :optional
+ .param int has_value :opt_flag
+
+ if has_value goto have_value
+ value = 'list'()
+ have_value:
+ $P0 = new 'Exception'
+ $P0['type'] = .CONTROL_RETURN
+ setattribute $P0, 'payload', value
+ throw $P0
+ .return (value)
+.end
+
+
=back
=cut
Modified: trunk/languages/pipp/src/pct/actions.pm
==============================================================================
--- trunk/languages/pipp/src/pct/actions.pm (original)
+++ trunk/languages/pipp/src/pct/actions.pm Sat Nov 29 05:21:00 2008
@@ -126,6 +126,17 @@
make $past;
}
+method return_statement($/) {
+ my $past := PAST::Op.new(
+ :name('return'),
+ :pasttype('call'),
+ :node( $/ ),
+ $( $/<expression> )
+ );
+
+ make $past;
+}
+
method require_once_statement($/) {
my $past := PAST::Op.new(
:name('require'),
Modified: trunk/languages/pipp/src/pct/grammar.pg
==============================================================================
--- trunk/languages/pipp/src/pct/grammar.pg (original)
+++ trunk/languages/pipp/src/pct/grammar.pg Sat Nov 29 05:21:00 2008
@@ -120,6 +120,7 @@
rule statement {
<namespace_statement> {*} #= namespace_statement
+ | <return_statement> {*} #= return_statement
| <require_once_statement> {*} #= require_once_statement
| <echo_statement> {*} #= echo_statement
| <expression_statement> {*} #= expression_statement
@@ -144,6 +145,12 @@
{*}
}
+# return can appear inside and outside functions
+token return_statement {
+ 'return' <.ws_char> <expression> <.statement_delimiter>
+ {*}
+}
+
token require_once_statement {
'require_once' <.ws_char> <quote> <ws> <.statement_delimiter>
{*}
Modified: trunk/languages/pipp/t/php/functions.t
==============================================================================
--- trunk/languages/pipp/t/php/functions.t (original)
+++ trunk/languages/pipp/t/php/functions.t Sat Nov 29 05:21:00 2008
@@ -20,7 +20,7 @@
use FindBin;
use lib "$FindBin::Bin/../../../../lib", "$FindBin::Bin/../../lib";
-use Parrot::Test tests => 8;
+use Parrot::Test tests => 15;
language_output_is( 'Pipp', <<'CODE', <<'OUT', 'function with no args' );
<?php
@@ -171,26 +171,132 @@
=for perl6
-sub return_100( ) {
+sub func_with_return( ) {
return 100;
}
-my $a = return_100();
+my $a = func_with_return();
print "$a\n";
=cut
-language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return an integer', todo =>
'not implemented yet' );
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return an integer' );
<?php
-function return_100 ( ) {
+function func_with_return ( ) {
return 100;
}
-$a = return_100();
+$a = func_with_return();
echo "$a\n";
?>
CODE
100
OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return a string' );
+<?php
+
+function func_with_return ( ) {
+ return 'I am a string.';
+}
+
+$a = func_with_return();
+echo "$a\n";
+
+?>
+CODE
+I am a string.
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return a number' );
+<?php
+
+function func_with_return ( ) {
+ return 3.14;
+}
+
+$a = func_with_return();
+echo "$a\n";
+
+?>
+CODE
+3.14
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return TRUE' );
+<?php
+
+function func_with_return ( ) {
+ return TRUE;
+}
+
+$a = func_with_return();
+echo "returned: $a\n";
+
+?>
+CODE
+returned: 1
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return FALSE' );
+<?php
+
+function func_with_return ( ) {
+ return FALSE;
+}
+
+$a = func_with_return();
+echo "returned:'$a'\n";
+
+?>
+CODE
+returned:''
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return 0' );
+<?php
+
+function func_with_return ( ) {
+ return 0;
+}
+
+$a = func_with_return();
+echo "$a\n";
+
+?>
+CODE
+0
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return NULL' );
+<?php
+
+function func_with_return ( ) {
+ return NULL;
+}
+
+$a = func_with_return();
+echo "returned:'$a'\n";
+
+?>
+CODE
+returned:''
+OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'return a variable', todo =>
'not working yet' );
+<?php
+
+function func_with_return ( ) {
+ $local_var = 'I was a variable';
+ return $local_var;
+}
+
+$a = func_with_return();
+echo "$a\n";
+
+?>
+CODE
+I was a variable.
+OUT