Author: bernhard
Date: Fri Jan 2 08:13:42 2009
New Revision: 34816
Modified:
trunk/languages/pipp/src/pct/actions.pm
trunk/languages/pipp/src/pct/grammar.pg
trunk/languages/pipp/t/php/oo.t
Log:
[Pipp] Pass arguments to the constructor, in a convoluted way.
Modified: trunk/languages/pipp/src/pct/actions.pm
==============================================================================
--- trunk/languages/pipp/src/pct/actions.pm (original)
+++ trunk/languages/pipp/src/pct/actions.pm Fri Jan 2 08:13:42 2009
@@ -209,17 +209,51 @@
make $past;
}
+# TODO: simplify
method constructor_call($/) {
my $class_name := ~$<CLASS_NAME>;
-
- make
+ # The constructor needs a list of it's arguments, or an empty list
+ my $cons_call := +$<argument_list> ??
+ $( $<argument_list>[0] )
+ !!
+ PAST::Op.new( :pasttype('call') );
+ # The object is the first argument
+ $cons_call.unshift(
PAST::Op.new(
- :inline(' %r = new %0',
- " \$P0 = get_global ['" ~ $class_name ~ "'],
'__construct'",
- ' if null $P0 goto no_constructor',
- ' $P0(%r)',
- ' no_constructor:'),
+ :inline('.local pmc obj', '%r = new %0', 'obj = %r'),
$class_name
+ )
+ );
+ # The function comes before the first argument
+ $cons_call.unshift(
+ PAST::Var.new(
+ :name('cons'),
+ :scope('register')
+ )
+ );
+
+ make
+ PAST::Stmts.new(
+ PAST::Op.new( # check whether there is a
constructor
+ :pasttype('if'),
+ PAST::Op.new(
+ :pirop('isnull'),
+ PAST::Op.new(
+ :inline("%r = get_global ['" ~ $class_name ~ "'],
'__construct'", '.local pmc cons', 'cons = %r # condition')
+ )
+ ),
+ PAST::Op.new( # no constructor
call needed
+ :inline('.local pmc obj',
+ '%r = new %0',
+ 'obj = %r'),
+ $class_name
+ ),
+ $cons_call # call the
constructor
+ ),
+ PAST::Var.new( # return the
created object
+ :name('obj'),
+ :scope('register')
+ )
);
}
Modified: trunk/languages/pipp/src/pct/grammar.pg
==============================================================================
--- trunk/languages/pipp/src/pct/grammar.pg (original)
+++ trunk/languages/pipp/src/pct/grammar.pg Fri Jan 2 08:13:42 2009
@@ -1,4 +1,4 @@
-# Copyright (C) 2006-2008, The Perl Foundation.
+# Copyright (C) 2006-2009, The Perl Foundation.
# $Id$
# PHP grammar for Pipp PCT
@@ -21,14 +21,14 @@
token multilinecomment { '/*' .*? '*/' } # C-like multiline comment
token ws_all {
- <.ws_char>
+ | <.ws_char>
| <singlelinecomment>
| <multilinecomment>
}
# whitespace rule used implicity by rules
token ws {
- <!ww> <ws_all>+
+ | <!ww> <ws_all>+
| <ws_all>*
}
@@ -295,7 +295,7 @@
}
rule constructor_call {
- 'new' <CLASS_NAME>
+ 'new' <CLASS_NAME> [ '(' <argument_list> ')' ]?
{*}
}
Modified: trunk/languages/pipp/t/php/oo.t
==============================================================================
--- trunk/languages/pipp/t/php/oo.t (original)
+++ trunk/languages/pipp/t/php/oo.t Fri Jan 2 08:13:42 2009
@@ -20,7 +20,7 @@
use FindBin;
use lib "$FindBin::Bin/../../../../lib", "$FindBin::Bin/../../lib";
-use Parrot::Test tests => 10;
+use Parrot::Test tests => 11;
language_output_is( 'Pipp', <<'CODE', <<'OUT', 'definition of a class' );
<?php
@@ -254,3 +254,19 @@
CODE
method __construct() of class Foo was called.
OUT
+
+language_output_is( 'Pipp', <<'CODE', <<'OUT', 'constructor with one arg' );
+<?php
+
+class Foo {
+ function __construct($msg) {
+ echo "The message is $msg.\n";
+ }
+}
+
+$foo = new Foo('what the message is');
+
+?>
+CODE
+The message is what the message is.
+OUT