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

Support continuous expressions #221

Open
vishwajeet-pandey-exa opened this issue Apr 16, 2022 · 1 comment
Open

Support continuous expressions #221

vishwajeet-pandey-exa opened this issue Apr 16, 2022 · 1 comment

Comments

@vishwajeet-pandey-exa
Copy link

vishwajeet-pandey-exa commented Apr 16, 2022

Right now when an expression like 2 + 3 + 4 + 5 is passed to the parse function, it's broken down into 'Binary expressions' & the tree generated is binary one & looks like what it'd be for (2 + 3) + (4 + 5):

{
  "type": "BinaryExpression",
  "operator": "+",
  "left": {
    "type": "BinaryExpression",
    "operator": "+",
    "left": {
      "type": "BinaryExpression",
      "operator": "+",
      "left": {
        "type": "Literal",
        "value": 2,
        "raw": "2"
      },
      "right": {
        "type": "Literal",
        "value": 3,
        "raw": "3"
      }
    },
    "right": {
      "type": "Literal",
      "value": 4,
      "raw": "4"
    }
  },
  "right": {
    "type": "Literal",
    "value": 5,
    "raw": "5"
  }
}

I know these are equivalent, but just to avoid extra parentheses when we try to rebuild the expression from the tree or when we try to show these in a tree form a bit better, could we generate the same thing as a 'list like' expression type where we could just show a n-ary tree instead of binary ('left' and 'right') one:

{
  "type": "ContinuousExpression",
  "operator": "+",
  "list": [
      {
        "type": "Literal",
        "value": 2,
        "raw": "2"
      },
      {
        "type": "Literal",
        "value": 3,
        "raw": "3"
      },
      {
        "type": "Literal",
        "value": 4,
        "raw": "4"
      },
      {
        "type": "Literal",
        "value": 5,
        "raw": "5"
      }
  ]
}

If nothing, could we write plugin which'd do the same thing?

@LeaVerou
Copy link
Collaborator

We can't do this by default, because our output needs to be compatible with other parsers, but it could easily be an optional plugin. It doesn't even need to modify the parser, it can just be an AST transformation (that's how we do it in Mavo that uses JSEP and also needs this).

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