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

Array and list element access #12

Open
jbunke opened this issue Jan 7, 2025 · 0 comments
Open

Array and list element access #12

jbunke opened this issue Jan 7, 2025 · 0 comments
Assignees
Labels
lang-design Language design and grammar
Milestone

Comments

@jbunke
Copy link
Owner

jbunke commented Jan 7, 2025

Problem

Based on the current syntax grammar, array and list elements are only accessible via the following rule in expr:

expr
// ...
| assignable                                #AssignableExpression
| literal                                   #LiteralExpression
;

assignable
: ident                                     #SimpleAssignable
| ident LT expr GT                          #ListAssignable
| ident LBRACKET expr RBRACKET              #ArrayAssignable
;

This means that array and list elements cannot be accessed unless they are in the first dimension and their collection is a variable.

Tests

Test to see whether this problem actually exists. Do these scripts (1) compile? and (2) behave correctly?

Accessing an element of an explicit collection

() {
  int arr_elem = [ 6, 5, 3 ][1];
  print(arr_elem); // expected: "5"

  char list_elem = < 'H', 'a', 't', 't', 'e', 'r' ><3>;
  print(list_elem); // expected: "t"
}

Accessing a second-dimension collection element

() {
  int[][] matrix = [
    [ 1, 2, 3, 4 ], 
    [ 5, 6, 7, 8 ], 
    [ 9, 10, 11, 12 ], 
    [ 13, 14, 15, 16 ]
  ];

  print(matrix[2][2]); // expected: "11"
}

Potential grammar fix

expr
// ...
| expr LBRACKET expr RBRACKET               #ArrayIndexExpression  // new production
| expr LT expr RT                           #ListIndexExpression   // new production
// ...
| ident                                     #VariableExpression    // replaces #AssignableExpression
| literal                                   #LiteralExpression
;
  • The rule assignable should remain unchanged, but only be used for assignments
  • Determine where in the precedence order of expr that #ArrayIndexExpression and #ListIndexExpression should go
@jbunke jbunke added the lang-design Language design and grammar label Jan 7, 2025
@jbunke jbunke added this to the 0.2.0 milestone Jan 7, 2025
@jbunke jbunke self-assigned this Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang-design Language design and grammar
Projects
None yet
Development

No branches or pull requests

1 participant