Skip to content

Array and list element access #12

Description

@jbunke

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

Metadata

Metadata

Assignees

Labels

lang-designLanguage design and grammar

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions