Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour when accessing 2D arrays #356

Open
mikachelya opened this issue Dec 8, 2024 · 1 comment
Open

Inconsistent behaviour when accessing 2D arrays #356

mikachelya opened this issue Dec 8, 2024 · 1 comment

Comments

@mikachelya
Copy link

array at 0 at 0 correctly parses to array[0][0].
However, array at x at y incorrectly parses to array[x[y]].

Screenshot 2024-12-08 231429
Screenshot 2024-12-08 231547

@dylanbeattie
Copy link
Collaborator

OK, a quick recap of my thinking on this so far: what's the problem, why does it matter, and what constraints apply to any proposed solution:

There are 1D arrays, "true" 2D arrays (matrices), and jagged arrays (arrays of arrays)

In languages that support all three, you're talking about:

array[1]
array[1,2]
array[1][2]

Rockstar doesn't have matrix arrays, but it does have jagged arrays. Now, imagine we've got an array like this:

indexes = [ 5, 2, 3, 1, 0, 6 ]

In languages that use square-bracket arrays, the syntax is unambiguous:

array [ indexes [ 0 ] ] // use the first element of indexes as an index
array [ indexes ] [ 0 ] // jagged array

Rockstar doesn't currently have any way to disambiguate this.

  • array at 1 at 2 is ALWAYS a jagged array, because 1 at 2 isn't a valid expression
  • array at x at y is parsed as array[x[y]] because the parser in this scenario is right-associative, so it consumes x at y, produces an expression, and then uses that expression to produce array at <expr>

This means there is no way in Rockstar right now to write array[x][y], because there is no left-associative syntax for array at x that produces an indexable expression which then results in the <expr> at y production.

Like most things in Rockstar, you can work around the syntactic limitations if you're prepared to be verbose about it. You CAN do this:

let array2 be array at x
let element be array2 at y

or

let index be x at y
let element be array at index

However, there's definitely a need for a better syntax for indexed expressions, so I'm trying to come up with a Rockstar-style equivalent of the various possible permutations:

array [ x ] [ y ] [ z ]
array [ x [ y ] ] [ z ]
array [ x ] [ y [ z ] ]
array [ x [ y [ z ] ] ]

The syntax can't be anything that's valid inside an expression, so we can't reuse keywords like and, with, of because array at x and y of z is equivalent to array[x & y * z]

One idea was to use at and ,, (effectively representing [ and ]) but that leads to expressions like array at x at y,, at z and the double comma isn't valid English.

I'm going to keep tinkering with it but if anybody has any good ideas for an appropriate syntax, I'd love to hear them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants