Beispiel #1
0
    protected object EvaluateAssignmentExpr(AstAssignmentExpr expr)
    {
        if (HadErrorOrReturn())
        {
            return(null);
        }

        if (expr.m_lhs.m_instance != null)
        {
            object   instanceObj = EvaluateExpr(expr.m_lhs.m_instance);
            Instance instance    = instanceObj as Instance;

            if (instance != null)
            {
                object value = EvaluateExpr(expr.m_rhs);
                if (HadErrorOrReturn())
                {
                    return(null);
                }

                instance.SetMember(expr.m_lhs.m_identifier.m_identifier, value);
                return(value);
            }
            else
            {
                m_runtimeError = true;
                Lox.Error(expr.m_startLine, "Only class instances can access properties with '.'");
                return(null);
            }
        }
        else
        {
            object value = EvaluateExpr(expr.m_rhs);
            if (HadErrorOrReturn())
            {
                return(null);
            }

            bool success = m_environment.Assign(expr.m_lhs.m_identifier, value);

            if (success)
            {
                return(value);
            }
            else
            {
                m_runtimeError = true;
                Lox.Error(expr.m_startLine, "Undefined variable \"" + expr.m_lhs.m_identifier + "\"");
                return(null);
            }
        }
    }
Beispiel #2
0
 protected void ResolveAssignmentExpr(AstAssignmentExpr expr)
 {
     ResolveExpr(expr.m_rhs);
     ResolveVarExpr(expr.m_lhs);
 }