cvsuser 04/07/22 09:19:50
Modified: languages/python pie-thon.pl
languages/python/t/pie b3.t
src objects.c py_func.c
Log:
Pie-thon 90 - more hacks for b3; __new__ creates objects
Revision Changes Path
1.62 +10 -8 parrot/languages/python/pie-thon.pl
Index: pie-thon.pl
===================================================================
RCS file: /cvs/public/parrot/languages/python/pie-thon.pl,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -w -r1.61 -r1.62
--- pie-thon.pl 22 Jul 2004 12:48:28 -0000 1.61
+++ pie-thon.pl 22 Jul 2004 16:19:40 -0000 1.62
@@ -258,20 +258,22 @@
$k =~ s/[\*"]//g;
$params[$v] = $k;
}
+ my $self;
if ($meth ne '') {
- my $self = shift @params;
- if ($self ne 'self') {
- print <<EOC;
- .local pmc $self
- $self = self
-EOC
- }
+ $self = shift @params;
+ shift @{$def_args{$arg}};
}
$params = join("\n\t", map {".param pmc $_"} @params);
print <<EOC;
$params
EOC
+ if ($self && $self ne 'self') {
+ print <<EOC;
+ .local pmc $self
+ $self = self
+EOC
+ }
if ($func_info{$arg}{flags} & 0x20) { # GENERATOR flag
for (my $i = 0; $i < @params; ++$i) {
my $p = $params[$i];
@@ -741,7 +743,7 @@
unshift @{$def_args{$f}}, $gn;
}
}
- if ($cur_func =~ /Build::(\w+)/) {
+ if ($cur_func =~ /Build::(\w+)/) { ## XXX && $f ne '__new__'
$namespace{$f} = $classes{$1};
if ($vtables{$f}) {
print <<EOC;
1.2 +17 -2 parrot/languages/python/t/pie/b3.t
Index: b3.t
===================================================================
RCS file: /cvs/public/parrot/languages/python/t/pie/b3.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -w -r1.1 -r1.2
--- b3.t 22 Jul 2004 12:48:15 -0000 1.1
+++ b3.t 22 Jul 2004 16:19:46 -0000 1.2
@@ -1,9 +1,9 @@
-# $Id: b3.t,v 1.1 2004/07/22 12:48:15 leo Exp $
+# $Id: b3.t,v 1.2 2004/07/22 16:19:46 leo Exp $
use strict;
use lib '../../lib';
-use Parrot::Test tests => 3;
+use Parrot::Test tests => 4;
sub test {
language_output_is('python', $_[0], '', $_[1]);
@@ -86,3 +86,18 @@
main()
CODE
+
+test(<<'CODE', 'TT class');
+T = int
+
+class TT(T):
+ def __repr__(self):
+ return "T(%d)" % self
+
+def main():
+ i = TT(5)
+ print i, `i`
+
+main()
+
+CODE
1.110 +60 -12 parrot/src/objects.c
Index: objects.c
===================================================================
RCS file: /cvs/public/parrot/src/objects.c,v
retrieving revision 1.109
retrieving revision 1.110
diff -u -w -r1.109 -r1.110
--- objects.c 22 Jul 2004 12:48:35 -0000 1.109
+++ objects.c 22 Jul 2004 16:19:49 -0000 1.110
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-$Id: objects.c,v 1.109 2004/07/22 12:48:35 leo Exp $
+$Id: objects.c,v 1.110 2004/07/22 16:19:49 leo Exp $
=head1 NAME
@@ -25,6 +25,8 @@
#include "objects.str"
static void* instantiate_py_object(Interp*, PMC*, void*);
+extern void
+parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class);
static PMC *
clone_array(Parrot_Interp interpreter, PMC *source_array)
@@ -255,15 +257,18 @@
PMC *classname_pmc;
PMC *parents, *temp_pmc;
int parent_is_class;
+ int is_python = 0;
if (base_class->vtable->base_type == enum_class_FixedPMCArray) {
PMC *tuple = base_class;
/* got a tuple holding parents - Python!
*/
INTVAL n = VTABLE_elements(interpreter, tuple);
+ is_python = 1;
if (!n) {
PMC* class = pmc_new(interpreter, enum_class_ParrotClass);
Parrot_new_class(interpreter, class, child_class_name);
+ parrot_py_set_vtable(interpreter, class);
return class;
}
if (n > 1)
@@ -276,6 +281,8 @@
if (base_class->vtable->base_type == enum_class_ParrotClass) {
PMC* class = pmc_new(interpreter, enum_class_ParrotClass);
Parrot_new_class(interpreter, class, child_class_name);
+ if (is_python)
+ parrot_py_set_vtable(interpreter, class);
return class;
}
parent_is_class = PObj_is_class_TEST(base_class);
@@ -313,9 +320,20 @@
/* Our penultimate parent list is a clone of our parent's parent
list, with our parent unshifted onto the beginning */
if (parent_is_class) {
- temp_pmc = clone_array(interpreter,
- get_attrib_num((SLOTTYPE *)PMC_data(base_class),
- PCD_ALL_PARENTS));
+ PMC *all_parents, *last;
+ all_parents = get_attrib_num((SLOTTYPE *)PMC_data(base_class),
+ PCD_ALL_PARENTS);
+ temp_pmc = clone_array(interpreter, all_parents);
+ /*
+ * if any of the parent is a PMC, we need a deleg_pmc vtable
+ * XXX - we always use the parents vtable XXX
+ */
+ if (0 && VTABLE_elements(interpreter, all_parents)) {
+ last = VTABLE_get_pmc_keyed_int(interpreter, all_parents, -1);
+ if (!PObj_is_class_TEST(last))
+ parent_is_class = 0;
+ }
+
}
else {
/*
@@ -347,6 +365,8 @@
*/
create_deleg_pmc_vtable(interpreter, child_class, child_class_name);
}
+ if (is_python)
+ parrot_py_set_vtable(interpreter, child_class);
return child_class;
}
@@ -532,8 +552,6 @@
return Parrot_find_method_with_cache(interpreter, class, meth);
}
-extern void
-parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class, PMC *object);
static void
do_py_initcall(Parrot_Interp interpreter, PMC* class, PMC *object)
@@ -704,8 +722,30 @@
instantiate_py_object(Interp* interpreter, PMC* class, void* next)
{
INTVAL type = class->vtable->base_type;
- PMC *object = pmc_new_noinit(interpreter, type);
+ PMC *object = NULL;
+ if (PObj_is_class_TEST(class)) {
+ /* __new__ is a type constructor, it takes a class and
+ * arguments and returns a new object
+ */
+ STRING *meth_str = CONST_STRING(interpreter, "__new__");
+ PMC *meth = Parrot_find_method_with_cache(interpreter, class, meth_str);
+ if (meth) {
+ void *data = Parrot_save_register_frames(interpreter, meth);
+ REG_STR(0) = meth_str;
+ REG_PMC(2) = class;
+ /* args are just passed on */
+ PIO_eprintf(interpreter, "__new__ class %Ss nargs = %d\n",
+ VTABLE_name(interpreter, class),
+ (int)REG_INT(3));
+ Parrot_runops_fromc(interpreter, meth);
+ object = REG_PMC(5);
+ Parrot_restore_register_frames(interpreter, data);
+ }
+ }
+ if (!object) {
+ object = pmc_new_noinit(interpreter, type);
instantiate_object(interpreter, object, (void*)-1);
+ }
REG_PMC(5) = object;
return next;
}
@@ -758,7 +798,6 @@
/*
* we are coming from Python
*/
- parrot_py_set_vtable(interpreter, class, object);
do_py_initcall(interpreter, class, object);
}
@@ -1138,10 +1177,13 @@
* see also enter_nci_method()
*/
if (!PObj_is_class_TEST(class)) {
- STRING *class_name = class->vtable->whoami;
+ STRING *class_name;
STRING *isa;
UINTVAL start;
INTVAL pos;
+find_in_pmc:
+
+ class_name = class->vtable->whoami;
method = Parrot_find_global(interpreter,
class_name,
method_name);
@@ -1151,6 +1193,8 @@
* now look into that PMCs parents
* the parent classes are in vtable->isa_str as blank
* terminated class names - suboptimal but good enough for now
+ *
+ * TODO check vtable standard names
*/
start = class_name->strlen + 1;
for (isa = class->vtable->isa_str; ;) {
@@ -1204,8 +1248,12 @@
for (searchoffset = 0; searchoffset < classcount; searchoffset++) {
curclass = VTABLE_get_pmc_keyed_int(interpreter,
classsearch_array, searchoffset);
- if (!PObj_is_class_TEST(curclass))
+ if (!PObj_is_class_TEST(curclass)) {
+ class = curclass;
+ if (class->vtable->base_type == enum_class_delegate)
break;
+ goto find_in_pmc;
+ }
method = Parrot_find_global(interpreter,
VTABLE_get_string(interpreter,
get_attrib_num((SLOTTYPE *)PMC_data(curclass),
1.39 +62 -18 parrot/src/py_func.c
Index: py_func.c
===================================================================
RCS file: /cvs/public/parrot/src/py_func.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -w -r1.38 -r1.39
--- py_func.c 22 Jul 2004 08:01:36 -0000 1.38
+++ py_func.c 22 Jul 2004 16:19:49 -0000 1.39
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2004 The Perl Foundation. All Rights Reserved.
-$Id: py_func.c,v 1.38 2004/07/22 08:01:36 leo Exp $
+$Id: py_func.c,v 1.39 2004/07/22 16:19:49 leo Exp $
=head1 NAME
@@ -361,6 +361,15 @@
}
static PMC *
+parrot_py_str(Interp *interpreter, PMC *pmc)
+{
+ PMC *res = pmc_new(interpreter, enum_class_PerlString);
+ STRING *repr = VTABLE_get_string(interpreter, pmc);
+ VTABLE_assign_string_native(interpreter, res, repr);
+ return res;
+}
+
+static PMC *
parrot_py_hash(Interp *interpreter, PMC *pmc)
{
PMC *h = pmc_new_noinit(interpreter, enum_class_PerlInt);
@@ -700,16 +709,37 @@
result = PMC_int_val(self) / value;
VTABLE_set_integer_native(interp, destination, result);
}
+
+static PMC*
+parrot_py_instantiate_new(Parrot_Interp interpreter, PMC *class)
+{
+ if (PObj_is_class_TEST(class)) {
+ /* init calls instantiate */
+ return pmc_new_init(interpreter, class->vtable->base_type, (void*) -1);
+ }
+ else {
+ VTABLE_invoke(interpreter, class, NULL);
+ }
+ return REG_PMC(5);
+}
+
static void
parrot_py_create_default_meths(Interp *interpreter)
{
STRING *pio = CONST_STRING(interpreter, "PIO");
STRING *class = CONST_STRING(interpreter, "object");
- STRING *meth = CONST_STRING(interpreter, "__get_repr");
+ STRING *repr = CONST_STRING(interpreter, "__get_repr");
+ STRING *str = CONST_STRING(interpreter, "__get_string");
+ STRING *new__ = CONST_STRING(interpreter, "__new__");
parrot_py_object(interpreter, class,
- F2DPTR(parrot_py_repr), meth, pio);
+ F2DPTR(parrot_py_repr), repr, pio);
+ /* parrot_py_object(interpreter, class,
+ F2DPTR(parrot_py_str), str, pio);
+ */
+ parrot_py_global(interpreter,
+ F2DPTR(parrot_py_instantiate_new), new__, pio);
mmd_register(interpreter, MMD_DIVIDE,
enum_class_PerlInt, enum_class_PerlInt,
@@ -898,7 +928,7 @@
}
void
-parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class, PMC *object);
+parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class);
static PMC*
parrot_py_get_attr_str(Interp* interpreter, PMC *object, STRING *name)
@@ -923,9 +953,10 @@
}
}
/*
- * check attributes
+ * check attributes, if its an object
*/
+ if (PObj_is_object_TEST(object)) {
class = GET_CLASS((SLOTTYPE *)PMC_data(object), object);
class_array = (SLOTTYPE *)PMC_data(class);
p = get_attrib_num(class_array, PCD_ATTRIBUTES);
@@ -941,6 +972,13 @@
p = Parrot_find_method_with_cache(interpreter, class, name);
if (p)
return p;
+ }
+ /*
+ * 4) global - class method
+ */
+ p = Parrot_find_global(interpreter, NULL, name);
+ if (p)
+ return p;
/*
* pitch a fit
*/
@@ -986,11 +1024,17 @@
}
void
-parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class, PMC *object)
+parrot_py_set_vtable(Parrot_Interp interpreter, PMC* class)
{
- object->vtable->get_attr_str = parrot_py_get_attr_str;
- object->vtable->set_attr_str = parrot_py_set_attr_str;
- object->vtable->get_iter = parrot_py_get_iter;
+ PMC * vtable_pmc =
+ get_attrib_num((SLOTTYPE *)PMC_data(class), PCD_OBJECT_VTABLE);
+ VTABLE *vtable = PMC_struct_val(vtable_pmc);
+
+ vtable->get_attr_str = parrot_py_get_attr_str;
+ vtable->set_attr_str = parrot_py_set_attr_str;
+ vtable->get_iter = parrot_py_get_iter;
+
+ class->vtable->get_attr_str = parrot_py_get_attr_str;
}
/*