Skip to content

Commit

Permalink
ComplexMatrix2D now extracts a complex value from PMCs using role-bas…
Browse files Browse the repository at this point in the history
…ed rules instead of checking equality on vtable->base_type. As of Parrot r48883 this allows using subclasses of Complex for most operations. All tests pass again
  • Loading branch information
Whiteknight committed Sep 9, 2010
1 parent 73ed80c commit 673cca5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/include/pla_matrix_library.h
Expand Up @@ -9,5 +9,6 @@ void intkey_to_coords(PARROT_INTERP, const INTVAL rows, const INTVAL cols,
const INTVAL key, INTVAL * row, INTVAL * col);

PMC * get_external_pmc(PARROT_INTERP, const INTVAL type);
PMC * get_external_pmc_init(PARROT_INTERP, const INTVAL type, PMC * const value);

#endif /* _PLA_MATRIX_LIBRARY_H */
47 changes: 25 additions & 22 deletions src/lib/math_common.c
Expand Up @@ -36,27 +36,30 @@ void
get_complex_value_from_pmc(PARROT_INTERP, PMC * value, FLOATVAL * real,
FLOATVAL * imag)
{
switch (value->vtable->base_type) {
case enum_class_String:
value = Parrot_pmc_new_init(interp, enum_class_Complex, value);
case enum_class_Complex:
*real = VTABLE_get_number_keyed_int(interp, value, 0);
*imag = VTABLE_get_number_keyed_int(interp, value, 1);
break;
case enum_class_Float:
*real = VTABLE_get_number(interp, value);
*imag = 0.0;
break;
case enum_class_Integer:
{
const INTVAL _r = VTABLE_get_integer(interp, value);
*real = (FLOATVAL)_r;
}
*imag = 0.0;
break;
default:
/* TODO: We should support HLL-mapped types here. */
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"PLA: cannot set unknown PMC type");
if (PMC_IS_NULL(value)) {
*real = 0.0;
*imag = 0.0;
}
else if (VTABLE_does(interp, value, Parrot_str_new(interp, "complex", 7))) {
*real = VTABLE_get_number_keyed_int(interp, value, 0);
*imag = VTABLE_get_number_keyed_int(interp, value, 1);
}
else if (VTABLE_does(interp, value, Parrot_str_new(interp, "float", 5))) {
*real = VTABLE_get_number(interp, value);
*imag = 0.0;
}
else if (VTABLE_does(interp, value, Parrot_str_new(interp, "integer", 7))) {
const INTVAL _r = VTABLE_get_integer(interp, value);
*real = (FLOATVAL)_r;
*imag = 0.0;
}
else if (VTABLE_does(interp, value, Parrot_str_new(interp, "string", 6))) {
PMC * const c = get_external_pmc_init(interp, enum_class_Complex, value);
*real = VTABLE_get_number_keyed_int(interp, c, 0);
*imag = VTABLE_get_number_keyed_int(interp, c, 1);
}
else {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_OUT_OF_BOUNDS,
"PLA: cannot set unknown PMC type");
}
}
7 changes: 7 additions & 0 deletions src/lib/matrix_common.c
Expand Up @@ -27,3 +27,10 @@ get_external_pmc(PARROT_INTERP, const INTVAL type)
INTVAL realtype = Parrot_get_ctx_HLL_type(interp, type);
return Parrot_pmc_new(interp, realtype);
}

PMC *
get_external_pmc_init(PARROT_INTERP, const INTVAL type, PMC * const val)
{
INTVAL realtype = Parrot_get_ctx_HLL_type(interp, type);
return Parrot_pmc_new_init(interp, realtype, val);
}
7 changes: 6 additions & 1 deletion src/pmc/complexmatrix2d.pmc
Expand Up @@ -182,11 +182,16 @@ init_from_pmc_array(PARROT_INTERP, PMC * self, INTVAL rows_size,
{
DECLATTRS_ComplexMatrix2D(self, attrs);
INTVAL i, j, num = 0;
const INTVAL elems = VTABLE_elements(interp, values);
resize_matrix(interp, self, rows_size - 1, cols_size - 1);

for (i = 0; i < rows_size; i++) {
for (j = 0; j < cols_size; j++) {
PMC * const value = VTABLE_get_pmc_keyed_int(interp, values, num);
PMC * value;
if (num < elems)
value = VTABLE_get_pmc_keyed_int(interp, values, num);
else
value = PMCNULL;
set_complex_pmc_at_xy(interp, self, value, i, j);
num++;
}
Expand Down

0 comments on commit 673cca5

Please sign in to comment.