Skip to content

ast: Fix IndexExpr#218

Merged
mcfriend99 merged 3 commits intoblade-lang:devfrom
benstigsen:fix/ast-index-expressions
Jun 3, 2025
Merged

ast: Fix IndexExpr#218
mcfriend99 merged 3 commits intoblade-lang:devfrom
benstigsen:fix/ast-index-expressions

Conversation

@benstigsen
Copy link
Contributor

Try running this code:

import ast
import json

var tree = ast.parse('array[1] = v', '')
echo json.encode(tree, false)

What you'll see is the following:

[
  {
    "type": "ExprStmt",
    "expr": {
      "type": "=",
      "expr": {
        "type": "IndexExpr",
        "args": [
          {
            "type": "LiteralExpr",
            "value": "1"
          }
        ]
      },
      "value": {
        "type": "IdentifierExpr",
        "value": "v"
      }
    }
  }
]

As can be seen our identifier array (or whatever might be on the left hand side), is never included in the expression node.
This PR adds an expr parameter to the IndexExpr node. Now our tree looks like this:

[
  {
    "type": "ExprStmt",
    "expr": {
      "type": "=",
      "expr": {
        "type": "IndexExpr",
        "expr": {
          "type": "IdentifierExpr",
          "value": "array"
        },
        "args": [
          {
            "type": "LiteralExpr",
            "value": "1"
          }
        ]
      },
      "value": {
        "type": "IdentifierExpr",
        "value": "v"
      }
    }
  }
]

Oh, and there's also a small documentation addition for clib.named_struct() which I accidentally committed. 😅

@benstigsen benstigsen changed the base branch from main to dev June 3, 2025 18:42
@benstigsen
Copy link
Contributor Author

benstigsen commented Jun 3, 2025

I think there might be a couple of issues. There is no way to tell whether a LiteralExpr is a string or a number. Ideally the LiteralExpr should contain the quotes themselves if it's a string.

I'll go through it this week and try to fix the remaining parts of the ast module. Feel free to merge this now if you think it's okay, or we can wait until the other fixes.

@mcfriend99
Copy link
Member

The climb comments are welcomed since it clarified the functions.

I would have preferred it as a separate PR, but I think I can still merge it.

@mcfriend99 mcfriend99 merged commit a447d6e into blade-lang:dev Jun 3, 2025
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

Successfully merging this pull request may close these issues.

2 participants