예제 #1
0
        public object visitSuperExpr(Expr.Super super)
        {
            int distance = -1;

            locals.TryGetValue(super, out distance);
            List <LoxClass> superClasses = (List <LoxClass>)environment.getAt(distance, "super");
            LoxInstance     _object      = (LoxInstance)environment.getAt(distance - 1, "this");
            LoxFunction     method       = null;
            LoxClass        foundInClass = null;

            foreach (LoxClass superClass in superClasses)
            {
                LoxFunction methodFind = superClass.findMethod(super.method.lexeme);
                if (methodFind != null)
                {
                    if (method != null)
                    {
                        throw new Exceptions.RuntimeError(super.method, "Error: Found '" + super.method.lexeme + "' in " + foundInClass.name + " and " + superClass.name + ".");
                    }
                    method       = methodFind;
                    foundInClass = superClass;
                }
            }
            if (method == null)
            {
                throw new Exceptions.RuntimeError(super.method, "Undefined property '" + super.method.lexeme + "'.");
            }
            return(method.bind(_object));
        }
예제 #2
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            if (currentClass != ClassType.SUBCLASS)
            {
                Lox.Error(expr.keyword, "Cannot use 'super' outside of a class that has a superclass.");
            }

            ResolveLocal(expr, expr.keyword);
            return(null);
        }
예제 #3
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int distance = locals[expr];

            // Find reference to the superclass and current instance ("this").
            IClass      superclass   = (IClass)environment.GetAt(distance, "super");
            LoxInstance thisInstance = (LoxInstance)environment.GetAt(distance, "this");
            // Find method from superclass.
            LoxFunction method = superclass.FindMethod(thisInstance, expr.method.lexeme);

            return(method);
        }
예제 #4
0
 public object visitSuperExpr(Expr.Super super)
 {
     if (currentClass == ClassType.NONE)
     {
         Lox.error(super.keyword, "Can't use 'super' outside of a class.");
     }
     else if (currentClass != ClassType.SUBCLASS)
     {
         Lox.error(super.keyword, "Can't use 'super' in a class with no superclass.");
     }
     resolveLocal(super, super.keyword);
     return(null);
 }
예제 #5
0
 public Void VisitSuperExpr(Expr.Super expr)
 {
     if (_currentClass == ClassType.NONE)
     {
         Lox.Error(expr.Keyword, "Can't use 'super' outside of a class");
     }
     else if (_currentClass != ClassType.SUBCLASS)
     {
         Lox.Error(expr.Keyword, "Can't use 'super' in a class with no superclass.");
     }
     ResolveLocal(expr, expr.Keyword);
     return(null);
 }
예제 #6
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            var distance   = _locals[expr];
            var superclass = _environment.GetAt(distance, "super") as LoxClass;
            var obj        = _environment.GetAt(distance - 1, "this") as LoxInstance;
            var method     = superclass.FindMethod(expr.Method.Lexeme);

            if (method == null)
            {
                throw new RuntimeError(expr.Method, $"Undefined property '{expr.Method.Lexeme}'.");
            }

            return(method.Bind(obj));
        }
예제 #7
0
        public object Visit(Expr.Super _super)
        {
            if (_currentClass == ClassType.NONE)
            {
                Lox.Error(_super.keyword, "Cannot use 'super' outside of a class.");
            }
            else if (_currentClass != ClassType.SUBCLASS)
            {
                Lox.Error(_super.keyword, "Cannot use 'super' in a class with no superclass.");
            }


            ResolveLocal(_super, _super.keyword);

            return(null);
        }
예제 #8
0
        object Expr.IVisitor <object> .Visit(Expr.Super _super)
        {
            int      distance   = _locals[_super];
            LoxClass superclass = (LoxClass)_environment.GetAt(distance, "super");

            // "this" is always one level nearer than "super"'s environment.
            LoxInstance _object = (LoxInstance)_environment.GetAt(distance - 1, "this");

            LoxFunction method = superclass.FindMethod(_super.method.lexeme);

            if (method == null)
            {
                throw new RuntimeError(_super.method, "Undefined property '" + _super.method.lexeme + "'.");
            }

            return(method.Bind(_object));
        }
예제 #9
0
 string Expr.IVisitor <string> .Visit(Expr.Super _super)
 {
     throw new NotImplementedException();
 }
예제 #10
0
 public string visitSuperExpr(Expr.Super super)
 {
     return("(super)");
 }
예제 #11
0
 public string VisitSuperExpr(Expr.Super expr)
 {
     throw new NotImplementedException();
 }