Is there an interpreter that returns an enlarged AST?

Asked

Viewed 57 times

2

I’m using an interpreter that returns a AST I don’t know how I’ll read.

{
    clauses: [
        {
            body: [/* ...*/],
            condition: {/* ...*/},
            type: "IfClause"
        }
    ],
    type: "IfStatement"
}

I have to make a unique loop to deal with this tree and a lot of conditions to memorize the property I went through inside, etc. I’m sure I’m doing it the wrong way, so I would like another kind of tree.

{
    type: "IfClause"
},
{
    type: "Literal",
    value: false
},
{
    type: "BlockStatement"
},
{
    type: "Endif"
}

Or has another solution?

  • Your question sounds very interesting but it’s unclear, I don’t really understand what you want... take a look here: https://astexplorer.net/ is something similar you are looking for?

  • @Sergio She is different yet... returns block of commands inside an object of a block of commands. I wanted one that did not have this property body in objects, a very wide.

  • Okay, you can give an example of JS and how you want it to appear analyzed?

1 answer

2


Basically what you are wanting will hardly be supported natively since the very concept of tree is hierarchical by nature.

What is your goal? Is it just to "look" and see what you want more easily? Or is it to process the generated tree via code?

A list is simpler for you to see, but it is much more complicated to process, since you will have to recreate all the contexts of each block.

If you have difficulties with these blocks, you may just need to get a little more used to recursive algorithms, for example.

Transforming the list

If you really need a list flat can be generated by processing the AST.

Suppose you have the following fictitious tree:

var astTree = {
    type: "IfClause",
    base:
    {
        type: "Boolean",
        value: false
    },
    body: [
        { type: "Var" },
        { 
            type: "ForClause",
            body: [
                { type: "VarIncrement" }
            ]
        },
        { type: "Return" },
    ]
};

Then you can rely on a recursive function that runs through all subelements that have a body and add to an array. Example:

var flatten = function(node) {
    var list = [];
    function consume(node) {
        list.push(node);
        if ('body' in node) {
            var endNode = { type: node.type + 'End' }
            node.type += "Start";
            node.body.forEach(consume);
            list.push(endNode);
            delete node.body;
        }
    }
    consume(node);
    return list;
}

And the result is:

[
  {
    "type": "IfClauseStart",
    "base": {
      "type": "Boolean",
      "value": false
    }
  },
  {
    "type": "Var"
  },
  {
    "type": "ForClauseStart"
  },
  {
    "type": "VarIncrement"
  },
  {
    "type": "ForClauseEnd"
  },
  {
    "type": "Return"
  },
  {
    "type": "IfClauseEnd"
  }
]

See the functional example

  • 1

    This conversion seems solution to my problem, although it remains to call the function consume on the estates base in your example, because a function can be returned there and then the body of the function would have to be consumed.

  • 1

    @Davislf You are right, I did not pay attention to this detail, although my goal was to give only one example is to imagine that there would be more cases that would need a specific treatment. With luck will not have many different situations and with few adjustments you can convert more complex scripts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.