Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.7
    • Component/s: Type System
    • Labels:
      None
    • Number of attachments :
      5

      Description

      boo needs support for multidimension arrays of type Object[,], Object[,,], etc.

      For creation, it was suggested to expand the array() macro to take a variable number of args, e.g.:

      multi = array(Person,2, 3, 4)

      would yeild a Person[,,] with lengths of 2, 3, and 4 for each dimension, respectively.

      Access to the items can be done through an extension of the slicing syntax. Taking the above "multi" object, we can access a single entry by referencing mutli[0,2,3] or we can slice a range in each dimension by referencing multi[0:1,1:2,0-2] to get a new multi[,,].

      I started work on this, and I think i've got the antlr part of things done, but the rest I'm not even close on.

      1. boo-array-builtin-extension.diff
        0.6 kB
        Peter Johanson
      2. boo-current-md-support.diff
        30 kB
        Peter Johanson
      3. boo-md-arrays-v2.diff
        36 kB
        Peter Johanson
      4. boo-md-arrays-vFINAL.diff
        29 kB
        Peter Johanson
      5. boo-multi-dimensional-antlr.diff
        2 kB
        Peter Johanson

        Activity

        Hide
        Peter Johanson added a comment -

        Ok, this patch supercedes the previous two patches that were just fragments. Here's what does work (but of course needs testing):

        1) Creation of rectangular arrays of arbitrary rank and type.
        2) Assign and retrieve from arrays
        3) Assign and retrieve from a DefaultMember accessed by multiple indices. e.g:
        [DefaultMember("Items")]
        class Col:
        _objects = array(object, (2,2))

        Items (x as int, y as int) as object:
        get:
        return _objects[x,y]
        set:
        _objects[x,y]=value

        col = Col()
        col[0,0] = 1
        print(col[0,0])

        Working, but needs more testing/errorchecking/love:

        1) retrieval of complex slices of arrays in N dimensions (e.g.: c = a[0:3,2:4,5:6])

        Things that don't work yet:

        1) array indices of negative numbers (e.g a[1,3,-1])
        2) Assignment to members with several indices. See arrays-29.boo for the test case, and what fails. Need to figure out why this is happening.
        3) Assignment to complex rectangular array slices (e.g a[0:1,2:3] = b[2:3,5:6])

        Questions I have for further work:

        1) The following code prints "tr":
        a = "strings"
        print (a[1:3])

        Why does this print items 1-2, not 1-3? (maybe this is some convention from python i don't know (i'm not really a python coder)). My current rectangular array slicing considers "0:1" to mean "elements 0 through 1 inclusive" for each dimension. Should this change?

        2) The problem with array-29.boo test seems to be a result of the lines in ProcessMethodBodies.cs:
        if (AstUtil.IsLhsOfAssignment(node))

        { // leave it to LeaveBinaryExpression to resolve Bind(node, member); return; }

        Removing the call to Bind() makes things just bomb though. See the booc -vvv output of compiling arrays-29.boo for where this Bind() seems to screw things up or something. Pretty lost on this problem though.

        Ok, there it is so far. Comments? Bugs? Flames? Recipes for apple pie?

        Show
        Peter Johanson added a comment - Ok, this patch supercedes the previous two patches that were just fragments. Here's what does work (but of course needs testing): 1) Creation of rectangular arrays of arbitrary rank and type. 2) Assign and retrieve from arrays 3) Assign and retrieve from a DefaultMember accessed by multiple indices. e.g: [DefaultMember("Items")] class Col: _objects = array(object, (2,2)) Items (x as int, y as int) as object: get: return _objects [x,y] set: _objects [x,y] =value col = Col() col [0,0] = 1 print(col [0,0] ) Working, but needs more testing/errorchecking/love: 1) retrieval of complex slices of arrays in N dimensions (e.g.: c = a [0:3,2:4,5:6] ) Things that don't work yet: 1) array indices of negative numbers (e.g a [1,3,-1] ) 2) Assignment to members with several indices. See arrays-29.boo for the test case, and what fails. Need to figure out why this is happening. 3) Assignment to complex rectangular array slices (e.g a [0:1,2:3] = b [2:3,5:6] ) Questions I have for further work: 1) The following code prints "tr": a = "strings" print (a [1:3] ) Why does this print items 1-2, not 1-3? (maybe this is some convention from python i don't know (i'm not really a python coder)). My current rectangular array slicing considers "0:1" to mean "elements 0 through 1 inclusive" for each dimension. Should this change? 2) The problem with array-29.boo test seems to be a result of the lines in ProcessMethodBodies.cs: if (AstUtil.IsLhsOfAssignment(node)) { // leave it to LeaveBinaryExpression to resolve Bind(node, member); return; } Removing the call to Bind() makes things just bomb though. See the booc -vvv output of compiling arrays-29.boo for where this Bind() seems to screw things up or something. Pretty lost on this problem though. Ok, there it is so far. Comments? Bugs? Flames? Recipes for apple pie?
        Hide
        Peter Johanson added a comment -

        Forgot to mention one thing:

        Currently this patch causes arrays-23.boo to fail, as it creates an array() override with the signature array(Type type, params int[]). Unfortunately, i've yet to actually figure out how to make boo code then understand "array(int, 1,2,5)" and it still requires "array(int, (1,2,5))" to work.

        I believe there's already a different issue open regarding solving this var args issue.

        Show
        Peter Johanson added a comment - Forgot to mention one thing: Currently this patch causes arrays-23.boo to fail, as it creates an array() override with the signature array(Type type, params int[]). Unfortunately, i've yet to actually figure out how to make boo code then understand "array(int, 1,2,5)" and it still requires "array(int, (1,2,5))" to work. I believe there's already a different issue open regarding solving this var args issue.
        Hide
        Peter Johanson added a comment -

        here's a new version of the patch. What's fixed/implemented with this version:

        1) Settings MD slices, e.g:

        a = array(int, (3,3,3))
        a[0:2,0:2,0:2] = array(int, (2,2,2))

        2) Retrieval of MD arrays is now non-recursive (my head hurts)
        3) Fixed a problem that was present in the current code where trying to do "m.Item[x]=1" was broken but "m[x]=1" worked (if you had the right setup with DefaultMember, etc).
        4) Add an ArrayTypeReference now with rank. This currently has a few problems i'm not sure how to address.
        a) You can't put "_foo as (int^2)" before a constructor or method declaration, as _il won't yet be defined. Code needs to be refactored somehow to avoid this
        b) If you add a decleration as above, an exception is generated about 1 member being left on the stack. I think it's the "2" for the array rank being left around, as the type left on the stack is an Int32.

        Generally, I am very happy with this patch except for the above ArrayTypeReference issue, and the failure of arrays-23.boo due to the "array()" builtin not working with a variable number of arguments (requiring array(int, (2,3,4)) to create an array of rank 3)

        Thoughts?

        Show
        Peter Johanson added a comment - here's a new version of the patch. What's fixed/implemented with this version: 1) Settings MD slices, e.g: a = array(int, (3,3,3)) a [0:2,0:2,0:2] = array(int, (2,2,2)) 2) Retrieval of MD arrays is now non-recursive (my head hurts) 3) Fixed a problem that was present in the current code where trying to do "m.Item [x] =1" was broken but "m [x] =1" worked (if you had the right setup with DefaultMember, etc). 4) Add an ArrayTypeReference now with rank. This currently has a few problems i'm not sure how to address. a) You can't put "_foo as (int^2)" before a constructor or method declaration, as _il won't yet be defined. Code needs to be refactored somehow to avoid this b) If you add a decleration as above, an exception is generated about 1 member being left on the stack. I think it's the "2" for the array rank being left around, as the type left on the stack is an Int32. Generally, I am very happy with this patch except for the above ArrayTypeReference issue, and the failure of arrays-23.boo due to the "array()" builtin not working with a variable number of arguments (requiring array(int, (2,3,4)) to create an array of rank 3) Thoughts?
        Hide
        Bill Wood added a comment -

        I like (int,3) better than (int^3) - why introduce a new operator just for this?

        a as (int,3)

        works for me.

        If comma is suboptimal (not sure why?) then how about

        a as (int*3)

        which gives the flavor of an int multiplied or extended in 3 directions

        Show
        Bill Wood added a comment - I like (int,3) better than (int^3) - why introduce a new operator just for this? a as (int,3) works for me. If comma is suboptimal (not sure why?) then how about a as (int*3) which gives the flavor of an int multiplied or extended in 3 directions
        Hide
        Peter Johanson added a comment -

        Ok, I would consider this the final version of this patch.

        Both setting and getting works for arrays, including rank collapse (i.e a = b[0:1,0:3] vs a = b[0,0:3] (one returns an array of rank 2, the other of rank 1)). I've abandoned using Array.Copy altogether, because testing showed it to be considerably slower than an item by item GetValue/SetValue. Multi indexed accessors works, etc. The type references is implemented as "as (int,3)" meaning "an int array of rank 3" for now; I still don't know what the final verdict on this one is, but it's an easy change.

        The only problem i currently have is this breaks arrays-23.boo, as using the "array()" typed constructor doesnt's work in the "params" style yet. Not sure if this patch should wait for that to be working until support for that is working in boo or not. Opinions?

        Show
        Peter Johanson added a comment - Ok, I would consider this the final version of this patch. Both setting and getting works for arrays, including rank collapse (i.e a = b [0:1,0:3] vs a = b [0,0:3] (one returns an array of rank 2, the other of rank 1)). I've abandoned using Array.Copy altogether, because testing showed it to be considerably slower than an item by item GetValue/SetValue. Multi indexed accessors works, etc. The type references is implemented as "as (int,3)" meaning "an int array of rank 3" for now; I still don't know what the final verdict on this one is, but it's an easy change. The only problem i currently have is this breaks arrays-23.boo, as using the "array()" typed constructor doesnt's work in the "params" style yet. Not sure if this patch should wait for that to be working until support for that is working in boo or not. Opinions?

          People

          • Assignee:
            Peter Johanson
            Reporter:
            Peter Johanson
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: