amritrout / ArcaneScript

ArcaneScript is a custom programming language developed to explore language design and interpretation. This project includes the development of both the ArcaneScript language and its online Runner.
https://arcanescript.netlify.app/
6 stars 3 forks source link

Fix: Support expressions as array indices in ArcaneScript #3

Closed thecloudcode closed 1 month ago

thecloudcode commented 1 month ago

Summary

This PR enhances the array access functionality in ArcaneScript by supporting expressions (including variables) as array indices instead of limiting it to numeric literals.

Fixes #1

Changes Made

  1. Grammar Update: Modified ArcaneGrammar7.grm to replace NumberLiteral with <Expression> in array access rules.

    • Updated rule: <Value> ::= Identifier '[' <Expression> ']'
    • Updated rule: <Statement> ::= Identifier '=' Identifier '[' <Expression> ']' ';'
  2. Code Modifications:

    • ArrayAccess.java: Updated to process and evaluate expressions as indices, including type checking and error handling for invalid index types and out-of-bounds access.
    • ArrayValue.java: Introduced a new class to handle array access when used as a value, ensuring consistency in behavior.

Testing and Validation

Notes

image

Let me know if there are any questions or additional updates required!