Skip to content

Commit

Permalink
t/pie/b0 now completes parse
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@7444 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
rubys committed Jan 10, 2005
1 parent 18f013c commit b6eaa11
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 127 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -363,6 +363,7 @@ dynclasses/main.pasm [devel]
dynclasses/match.pmc [devel]
dynclasses/matchrange.pmc [devel]
dynclasses/pyboolean.pmc [devel]
dynclasses/pyboundcall.pmc [devel]
dynclasses/pyboundmeth.pmc [devel]
dynclasses/pybuiltin.pmc [devel]
dynclasses/pycomplex.pmc [devel]
Expand Down
1 change: 1 addition & 0 deletions config/gen/makefiles/dynclasses.in
Expand Up @@ -19,6 +19,7 @@ pyobject \
pyexception \
pyfloat \
pyfunc \
pyboundcall \
pyboundmeth \
pynci \
pystaticmeth \
Expand Down
108 changes: 108 additions & 0 deletions dynclasses/pyboundcall.pmc
@@ -0,0 +1,108 @@
/*
Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
$Id$

=head1 NAME

classes/nci.pmc - Python Native Call Interface Functions

=head1 DESCRIPTION

Extends Parrot's NCI to include attributes, implemented as properties.

=head2 Methods

=over 4

=cut

*/

#include "parrot/parrot.h"
#include "pyconsts.h"

pmclass PyBoundCall extends PyBoundMeth dynpmc group python_group {

/*

=item C<void* invoke(void* next)>

Invoke a method call on the indicated object.

=cut

*/

void* invoke(void* next) {
PMC *self = REG_PMC(5);
PMC *args = REG_PMC(6);
PMC *keywords = REG_PMC(7);
PMC *func_args = VTABLE_getprop(INTERP, self, PyFunc_args);
PMC *func_defaults = VTABLE_getprop(INTERP, self, PyFunc_defaults);
INTVAL j,n;

if (func_args && VTABLE_defined(INTERP, func_args))
n = VTABLE_elements(INTERP, func_args);
else
n = VTABLE_elements(INTERP, args);

REG_INT(3) = n;

/* fill in defaults */
if (func_defaults && VTABLE_defined(INTERP, func_defaults)) {
INTVAL nd = VTABLE_elements(INTERP, func_defaults);
for (j=n-nd; j<n && j<11; j++) {
REG_PMC(5+j) = VTABLE_get_pmc_keyed_int(INTERP,
func_defaults, j+nd-n);
}
}

/* fill in positional arguments */
if (VTABLE_get_bool(INTERP, args)) {
INTVAL np = VTABLE_elements(INTERP, args);
for (j=0; j<np && j<11; j++) {
REG_PMC(5+j) = VTABLE_get_pmc_keyed_int(INTERP, args, j);
}
}

/* fill in keyword arguments */
if (VTABLE_get_bool(INTERP, keywords)) {
for (j=0; j<n && j<11; j++) {
PMC *name = VTABLE_get_pmc_keyed_int(INTERP, func_args, j);
if (VTABLE_exists_keyed(INTERP, keywords, name)) {
REG_PMC(5+j) =
VTABLE_get_pmc_keyed(INTERP, keywords, name);
}
}
}

return VTABLE_invoke(INTERP, PMC_pmc_val(SELF), next);
}

}

/*

=back

=head1 SEE ALSO

F<docs/pdds/pdd03_calling_conventions.pod>.

=head1 HISTORY

Initial revision by sean 2002/08/04.

=cut

*/

/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
37 changes: 2 additions & 35 deletions dynclasses/pyboundmeth.pmc
Expand Up @@ -4,11 +4,11 @@ $Id$

=head1 NAME

classes/nci.pmc - Python Native Call Interface Functions
classes/pyboundmeth.pmc - Python Bound Method

=head1 DESCRIPTION

Extends Parrot's NCI to include attributes, implemented as properties.
A Python Method, bound to an object, and callable as a function

=head2 Methods

Expand Down Expand Up @@ -58,39 +58,6 @@ Marks the hash as live.

/*

=item C<PMC *find_method(STRING *method_name)>

Find a method call on the indicated object.

=cut

*/

PMC* find_method(STRING* method_name) {
if (0 == string_compare(INTERP, method_name, PyString_call)) {
int i = REG_INT(3)++;
while (i--)
REG_PMC(6+i)=REG_PMC(5+i);
REG_PMC(5) = PMC_struct_val(SELF);
}
return VTABLE_find_method(INTERP, PMC_pmc_val(SELF), method_name);
}
/*

=item C<PMC* get_attr_str(STRING *name)>

Return attribute named C<name>.

=cut

*/

PMC* get_attr_str(STRING* idx) {
return VTABLE_get_attr_str(INTERP, PMC_pmc_val(SELF), idx);
}

/*

=item C<STRING *get_string()>

Return the representation of this object.
Expand Down
21 changes: 21 additions & 0 deletions dynclasses/pybuiltin.pmc
Expand Up @@ -84,6 +84,7 @@ they will be used frequently here.
if (pass) {
PyBuiltin_PyBoolean = Parrot_PMC_typenum(INTERP, "PyBoolean");
PyBuiltin_PyBoundMeth = Parrot_PMC_typenum(INTERP, "PyBoundMeth");
PyBuiltin_PyBoundCall = Parrot_PMC_typenum(INTERP, "PyBoundCall");
PyBuiltin_PyClass = Parrot_PMC_typenum(INTERP, "PyClass");
PyBuiltin_PyComplex = Parrot_PMC_typenum(INTERP, "PyComplex");
PyBuiltin_PyDict = Parrot_PMC_typenum(INTERP, "PyDict");
Expand Down Expand Up @@ -125,6 +126,10 @@ they will be used frequently here.
PyString_str = const_string(INTERP, "__str__");

PyString_next = const_string(INTERP, "next");

PyFunc_args = const_string(INTERP, "func_args");
PyFunc_varargs = const_string(INTERP, "func_varargs");
PyFunc_defaults = const_string(INTERP, "func_defaults");
}
}

Expand Down Expand Up @@ -962,6 +967,22 @@ calculation, and serves as a default whe n the sequence is empty.

/*

=item C<PMC* "repr"(PMC *value)>

Returns the representation of C<value>.

=cut

*/

METHOD PMC* repr(PMC *value) {
PMC * ret = pmc_new(INTERP, PyBuiltin_PyString);
VTABLE_set_string_native(INTERP, ret, VTABLE_get_repr(INTERP, value));
return ret;
}

/*

=back

=cut
Expand Down
2 changes: 2 additions & 0 deletions dynclasses/pyclass.pmc
Expand Up @@ -373,6 +373,8 @@ Call the object.

void* invoke(void* next) {
PMC *call = VTABLE_find_method(INTERP, SELF, PyString_call);
if (!REG_INT(3)) REG_INT(3)=1;
REG_PMC(5) = SELF;
return VTABLE_invoke(INTERP, call, next);
}

Expand Down
5 changes: 5 additions & 0 deletions dynclasses/pyconsts.h
Expand Up @@ -8,6 +8,7 @@

PYVAR_SCOPE INTVAL PyBuiltin_PyBoolean;
PYVAR_SCOPE INTVAL PyBuiltin_PyBoundMeth;
PYVAR_SCOPE INTVAL PyBuiltin_PyBoundCall;
PYVAR_SCOPE INTVAL PyBuiltin_PyClass;
PYVAR_SCOPE INTVAL PyBuiltin_PyComplex;
PYVAR_SCOPE INTVAL PyBuiltin_PyDict;
Expand Down Expand Up @@ -69,6 +70,10 @@ PYVAR_SCOPE STRING *PyString_proxy;
PYVAR_SCOPE STRING *PyString_repr;
PYVAR_SCOPE STRING *PyString_str;

PYVAR_SCOPE STRING *PyFunc_args;
PYVAR_SCOPE STRING *PyFunc_varargs;
PYVAR_SCOPE STRING *PyFunc_defaults;

/* utility functions */

struct parrot_regs_t *
Expand Down
96 changes: 16 additions & 80 deletions dynclasses/pyfunc.pmc
Expand Up @@ -21,32 +21,8 @@ This class implements a Python Function, i.e. a Parrot Closure.
#include "parrot/parrot.h"
#include "pyconsts.h"

static STRING *FUNC_ARGS;
static STRING *FUNC_DEFAULTS;
static STRING *FUNC_VARARGS;

pmclass PyFunc extends Closure dynpmc group python_group {

/*

=item C<void class_init()>

Class initialization. Caches the type id of various PMCs because
they will be used frequently here.

=cut

*/

void class_init() {
if (pass) {
FUNC_ARGS = const_string(INTERP, "func_args");
FUNC_VARARGS = const_string(INTERP, "func_varargs");
FUNC_DEFAULTS = const_string(INTERP, "func_defaults");
}
}


/*

=item C<void init()>
Expand All @@ -61,7 +37,7 @@ Create an (empty) list of function arguments
PMC *func_args;
SUPER();
func_args = pmc_new(INTERP, PyBuiltin_PyList);
VTABLE_setprop(INTERP, SELF, FUNC_ARGS, func_args);
VTABLE_setprop(INTERP, SELF, PyFunc_args, func_args);
}

/*
Expand All @@ -76,58 +52,14 @@ at this time, functions with a maximum of 11 arguments are supported.

TODO: error checking (e.g., missing, duplicate or extra arguments)

XXX: the __call__ support is bad, bad, bad. Consider the following
conversation: Do you have a __call__ method? Sure! Meanwhile I hope
you don't mind me rearranging your registers. I hope you are intending
to call the method real soon... Have a nice day!

The right way to accomplish this would be to create a method-wrapper
object and return it. And this overhead could be avoided in the normal
case if there were a VTABLE_call_method entry which unambiguously
involved a find_method in anticipation of an invoke.

*/

PMC* find_method(STRING* method_name) {
if (0 == string_compare(INTERP, method_name, PyString_call)) {
PMC *args = REG_PMC(5);
PMC *keywords = REG_PMC(6);
PMC *func_args = VTABLE_getprop(INTERP, SELF, FUNC_ARGS);
PMC *func_defaults = VTABLE_getprop(INTERP, SELF, FUNC_DEFAULTS);
INTVAL j;

INTVAL n = VTABLE_elements(INTERP, func_args);
REG_INT(3) = n;

/* fill in defaults */
if (func_defaults && VTABLE_defined(INTERP, func_defaults)) {
INTVAL nd = VTABLE_elements(INTERP, func_defaults);
for (j=n-nd; j<n && j<11; j++) {
REG_PMC(5+j) = VTABLE_get_pmc_keyed_int(INTERP,
func_defaults, j+nd-n);
}
}

/* fill in positional arguments */
if (VTABLE_get_bool(INTERP, args)) {
INTVAL np = VTABLE_elements(INTERP, args);
for (j=0; j<np && j<11; j++) {
REG_PMC(5+j) = VTABLE_get_pmc_keyed_int(INTERP, args, j);
}
}

/* fill in keyword arguments */
if (VTABLE_get_bool(INTERP, keywords)) {
for (j=0; j<n && j<11; j++) {
PMC *name = VTABLE_get_pmc_keyed_int(INTERP, func_args, j);
if (VTABLE_exists_keyed(INTERP, keywords, name)) {
REG_PMC(5+j) =
VTABLE_get_pmc_keyed(INTERP, keywords, name);
}
}
}

return SELF;
PMC *ret = pmc_new(INTERP, PyBuiltin_PyBoundCall);
VTABLE_set_pointer(INTERP, ret, SELF);
VTABLE_set_pmc(INTERP, ret, SELF);
return ret;
}

return SUPER(method_name);
Expand Down Expand Up @@ -202,13 +134,17 @@ Invoke a function. Defaults are filled in first.
*/

void* invoke(void* next) {
PMC *func_args = VTABLE_getprop(INTERP, SELF, FUNC_ARGS);
PMC *func_varargs = VTABLE_getprop(INTERP, SELF, FUNC_VARARGS);
INTVAL n = VTABLE_elements(INTERP, func_args);
INTVAL j;
PMC *func_args = VTABLE_getprop(INTERP, SELF, PyFunc_args);
PMC *func_varargs = VTABLE_getprop(INTERP, SELF, PyFunc_varargs);
INTVAL n,j;

if (func_args && VTABLE_defined(INTERP, func_args))
n = VTABLE_elements(INTERP, func_args);
else
n = REG_INT(3);

if (n > REG_INT(3)) {
PMC *func_defaults = VTABLE_getprop(INTERP,SELF,FUNC_DEFAULTS);
PMC *func_defaults = VTABLE_getprop(INTERP, SELF, PyFunc_defaults);

if (func_defaults && VTABLE_defined(INTERP, func_defaults)) {
INTVAL nd = VTABLE_elements(INTERP, func_defaults);
Expand Down Expand Up @@ -250,7 +186,7 @@ Add an argument to the func_args list
*/

void set_integer_keyed_int (INTVAL key, INTVAL value) {
PMC *func_args = VTABLE_getprop(INTERP, SELF, FUNC_ARGS);
PMC *func_args = VTABLE_getprop(INTERP, SELF, PyFunc_args);
VTABLE_set_integer_keyed_int(INTERP, func_args, key, value);
}

Expand All @@ -265,7 +201,7 @@ Add an argument to the func_args list
*/

void set_string_keyed_int (INTVAL key, STRING* value) {
PMC *func_args = VTABLE_getprop(INTERP, SELF, FUNC_ARGS);
PMC *func_args = VTABLE_getprop(INTERP, SELF, PyFunc_args);
VTABLE_set_string_keyed_int(INTERP, func_args, key, value);
}

Expand Down

0 comments on commit b6eaa11

Please sign in to comment.