Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multidimensional Arrays

0 views
Skip to first unread message

Roger Browne

unread,
Dec 5, 2005, 7:50:17 AM12/5/05
to p6i List
I'm having some trouble accessing elements of multidimensional arrays
from PIR, and I'm not sure if what I want to do is buggy or
unimplemented, or if I'm simply doing it the wrong way.

I have no problem accessing a one-dimensional Array with either an
integer index, or with a PMC index of type Integer:

.sub "main" :main

# Set up an array ["zero", "one"]
.local pmc array
array = new .Array
array = 2
array[0] = "zero"
array[1] = "one"

# print the final element (prints "one" as expected)
$S0 = array[1]
print $S0
print "\n"

# print the final element another way (prints "one" as expected)
$P0 = new .Integer
$P0 = 1
$S0 = array[$P0]
print $S0
print "\n"

.end

But if I try the same thing with an Array of Arrays, I can use literal
integers for the Array offsets, but not Integer PMCs:

.sub "main" :main

# Set up an array of arrays [["zero", "one"], ["two", "three"]]
.local pmc array, subarray1, subarray2
subarray1 = new .Array
subarray1 = 2
subarray1[0] = "zero"
subarray1[1] = "one"

subarray2 = new .Array
subarray2 = 2
subarray2[0] = "two"
subarray2[1] = "three"

array = new .Array
array = 2
array[0] = subarray1
array[1] = subarray2

# print the final element (prints "three" as expected)
$S0 = array[1; 1]
print $S0
print "\n"

# print the final element another way
$P0 = new .Integer
$P0 = 1
$P1 = new .Integer
$P1 = 1
$S0 = array[$P0; $P1] # [1]
print $S0
print "\n"

.end

The above code aborts with "build_key: wrong register set" at [1].

Can I use multi-dimensional PMC keys from PIR? There are opcodes like
"new_key" and "set_key" in pdd06, but they don't appear to be
implemented.

Or should I be building the key myself by using the ".Key" PMC?

Regards,
Roger Browne


Leopold Toetsch

unread,
Dec 5, 2005, 9:29:45 AM12/5/05
to Roger Browne, p6i List

On Dec 5, 2005, at 13:50, Roger Browne wrote:

> I'm having some trouble accessing elements of multidimensional arrays
> from PIR, and I'm not sure if what I want to do is buggy or
> unimplemented, or if I'm simply doing it the wrong way.

> $S0 = array[$P0; $P1] # [1]

Multi-dimensional keys can consist only of int or string parts, or of
constants of these types. If you really need a PMC for the key, you
have to create a Key PMC yourself.

E.g.

k1 = new .Key
k1 = 1
k2 = new .Key
k2 = 2
push k1, k2 # append next key
elem = array[k1] # [1:2]

(untested)

But it's probably simpler to just teach your compiler to use
integers/strings rather than PMCs.

> Regards,
> Roger Browne

leo

Roger Browne

unread,
Dec 5, 2005, 10:00:40 AM12/5/05
to p6i List
Leopold Toetsch wrote:

> ... it's probably simpler to just teach your compiler to use
> integers/strings rather than PMCs.

In general, I don't think a HLL has enough information to do this.

For example, if you have an Array of Hashes, the HLL doesn't easily know
to convert to an integer for the first part of the key, and to convert
to a string for the second.

When compiling the following HLL pseudocode ...

x = [{"1" => "one", "2" => "two"}, {"3" => "three"}]
pmc1 = new Integer(1)
pmc2 = new String("3")
print x[pmc1, pmc2]

... I think the HLL has to build the key structure from the PMCs that it
has available to it, and let the aggregate complain if it gets some kind
of key that it can't cope with.

> If you really need a PMC for the key, you
> have to create a Key PMC yourself.

That's what I'll do.

Thanks for your help!

Regards,
Roger Browne

Roger Browne

unread,
Dec 5, 2005, 12:11:17 PM12/5/05
to p6i List
> > $S0 = array[$P0; $P1]

Leo suggested:
> ... If you really need a PMC for the key, you

> have to create a Key PMC yourself.
>
> E.g.
>
> k1 = new .Key
> k1 = 1

That fragment works OK, but if I try to use a PMC instead of a literal
integer...

k1 = new .Key
$P0 = new .Integer
$P0 = 73
k1 = $P0

...then k1 just ends up as an .Integer with value 73. If, instead of
that last line, I try...

assign k1, $P0

...then I get this message:

this is broken - see slice.pmc

The Slice PMC doesn't include the string "assign", so I don't know what
this message is trying to tell me.

Maybe multidimensional array access using PMC indexes isn't supposed to
be working yet.

Regards,
Roger Browne


Leopold Toetsch

unread,
Dec 5, 2005, 3:11:57 PM12/5/05
to Roger Browne, p6i List

On Dec 5, 2005, at 18:11, Roger Browne wrote:

> Maybe multidimensional array access using PMC indexes isn't supposed to
> be working yet.

I see. Yes you are right. You really can rigth now do the 'easy' cases,
where a compiler knows or infers the involved types, but a general
multi-dim array access with PMC keys is broken currently.

Please create a TODO ticket for it, with a summary. Thanks.

> Regards,
> Roger Browne

leo

0 new messages