Exemple #1
0
    protected void ExecuteClassDeclStmt(AstClassDeclStmt stmt)
    {
        if (HadErrorOrReturn())
        {
            return;
        }

        // Define even though we aren't finished initializing it so that methods can
        //  reference the class.

        bool success = m_environment.Define(stmt.m_identifier.m_identifier, null);

        if (!success)
        {
            m_runtimeError = true;
            Lox.Error(stmt.m_startLine, "Redifinition of identifier " + stmt.m_identifier);
            return;
        }

        var methods = new Dictionary <string, Function>();

        foreach (AstFunDeclStmt funDecl in stmt.m_funDecls)
        {
            methods.Add(funDecl.m_identifier.m_identifier, new Function(funDecl, m_environment));
        }

        if (HadErrorOrReturn())
        {
            return;
        }

        success = m_environment.Assign(stmt.m_identifier, new Class(stmt.m_identifier.m_identifier, methods));
    }
Exemple #2
0
    protected void ResolveClassDeclStmt(AstClassDeclStmt stmt)
    {
        var scope = Scope();

        if (scope != null)      // TODO: Better global handling
        {
            if (scope.ContainsKey(stmt.m_identifier.m_identifier))
            {
                m_error = true;
                Lox.Error(stmt.m_startLine, "Redefinition of identifier \"" + stmt.m_identifier.m_identifier + "\"");
                return;
            }

            scope[stmt.m_identifier.m_identifier] = true;
        }

        scope         = PushScope();
        scope["this"] = true;

        foreach (AstFunDeclStmt funDecl in stmt.m_funDecls)
        {
            ResolveFunDeclStmt(funDecl);
        }

        PopScope();
    }