Domanda

When parsing AST with visitor, how could visitor detect when scope changes? For example, when we are in Class node, we create Class scope, but how to detect when we leaving a class node, to close the scope?

1: Stmt_Class(
    type: 0
    extends: null
    implements: array(
    )
    stmts: array(
        0: Stmt_ClassMethod(
            type: 1
            byRef: false
            params: array(
                0: Param(
                    name: str
                    default: null
                    type: null
                    byRef: false
                )
            )
            stmts: array(
                0: Stmt_Return(
                    expr: Expr_FuncCall(
                        name: Name(
                            parts: array(
                                0: mysql_real_escape_string
                            )
                            name: null
                        )
                        args: array(
                            0: Arg(
                                value: Expr_Variable(
                                    name: str
                                )
                                byRef: false
                                name: null
                            )
                        )
                    )
                    name: null
                )
            )
            name: clear
        )
    )
    name: Filter
)
È stato utile?

Soluzione

Use a simple scope stack. When you enter a new scope, push a new scope representation to the stack, when you leave it - pop it.

The first scope on the stack would be pre-defined and hold things like language constants.

When looking up a reference iterate through the stack from top down.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top