문제

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
)
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top