Exemple #1
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int distance;

            if (locals.TryGetValue(expr, out distance))
            {
                LoxClass    superclass = (LoxClass)environment.GetAt(distance, "super");
                LoxInstance obj        = (LoxInstance)environment.GetAt(distance - 1, "this");

                LoxFunction method = null;
                if (expr.Method != null)
                {
                    method = superclass.FindMethod(expr.Method.Lexeme);
                }
                else
                {
                    method = superclass.FindMethod(superclass.Name);
                }

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

                return(method.Bind(obj));
            }

            return(null);
        }
        void superCall(Expr.Call sCallExpr)
        {
            Expr.Super superExpr = (Expr.Super)sCallExpr.callee;

            captureToken(superExpr.keyword);

            if (currentClass == null)
            {
                error("Cannot use 'super' outside of a class.");
            }
            else if (!currentClass.hasSuperclass)
            {
                error("Cannot use 'super' in a class with no superclass.");
            }

            captureToken(superExpr.method);

            byte name = identifierConstant(superExpr.method);

            getNamedVariable(syntheticToken("this"));

            byte argCount = compile_argumentList(sCallExpr.arguments);

            getNamedVariable(superExpr.keyword);

            emitBytes((byte)OpCode.OP_SUPER_INVOKE, name);
            emitByte(argCount);
        }
Exemple #3
0
        public object visitSuperExpr(Expr.Super expr)
        {
            if (currentClass == ClassType.NONE)
            {
                Interpreter.error(expr.keyword, "Cannot use 'super' outside of a class.");
            }
            else if (currentClass != ClassType.SUBCLASS)
            {
                Interpreter.error(expr.keyword, "Cannot use 'super' in a class with no superclass.");
            }

            resolveLocal(expr, expr.keyword);
            return(null);
        }
Exemple #4
0
        public Unit VisitSuperExpr(Expr.Super expr)
        {
            if (CurrentClass == ClassType.NONE)
            {
                CSLox.Error(expr.Keyword, "Can't use 'super' outside of a class.");
            }
            else if (CurrentClass != ClassType.SUBCLASS)
            {
                CSLox.Error(expr.Keyword, "Can't use 'super' in a class with no superclass");
            }

            ResolveLocal(expr, expr.Keyword);
            return(new Unit());
        }
        public object VisitSuperExpr(Expr.Super expr)
        {
            if (_currentClass == ClassType.None)
            {
                Lox.Error(expr.keyword, "Can't use 'super' outside of a class");
            }
            if (_currentClass == ClassType.Class)
            {
                Lox.Error(expr.keyword, "Can't use 'super' in a class with no superclass");
            }

            ResolveLocal(expr, expr.keyword, true);

            return(null);
        }
Exemple #6
0
    public object 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);
    }
        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 RuntimeException(expr.method, "Undefined property '" + expr.method.Lexeme + "'.");
            }

            return(method.Bind(obj));
        }
Exemple #8
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            if (currentClass == ClassType.NONE)
            {
                throw new InterpretingException(expr.Keyword,
                                                "Can't use 'super' outside of a class.");
            }
            else if (currentClass != ClassType.SUBCLASS)
            {
                throw new InterpretingException(expr.Keyword,
                                                "Can't use 'super' in class with no superclass.");
            }

            ResolveLocal(expr, expr.Keyword);
            return(null);
        }
Exemple #9
0
    public object VisitSuperExpr(Expr.Super expr)
    {
        int         distance   = Locals[expr];
        LoxClass    superclass = (LoxClass)Environment.GetAt(distance, "super");
        LoxInstance instance   = (LoxInstance)Environment.GetAt(
            distance - 1, "this");
        LoxFunction method = superclass.FindMethod(expr.Method.Lexeme);

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

        return(method.Bind(instance));
    }
Exemple #10
0
        /// <summary>
        /// Resolve a superclass access
        /// </summary>
        /// <param name="expr">The expression</param>
        public object Visit(Expr.Super expr)
        {
            if (_current_class == ClassType.NONE)
            {
                _error_handler.Error(expr.keyword, "Cannot use 'super' outside of a class.");
            }
            else if (_current_class != ClassType.SUBCLASS)
            {
                _error_handler.Error(expr.keyword, "Cannot use 'super' in a class with no superclass.");
            }



            ResolveLocal(expr, expr.keyword);
            return(null);
        }
Exemple #11
0
        public object visitSuperExpr(Expr.Super expr)
        {
            int      distance   = locals[expr];
            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(expr.method.lexeme());

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

            return(method.bind(object_));
        }
Exemple #12
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int distance = _locals[expr];

            if (_environment.GetAt(distance, "super") is LoxClass superClass)
            {
                if (_environment.GetAt(distance - 1, "this") is LoxInstance instance)
                {
                    Option <LoxFunction> methodOption = superClass.FindMethod(instance, expr.Method.Lexeme);
                    return(methodOption.Match(
                               some: method => method,
                               none: () => throw new RuntimeError(expr.Method, $"Super method {expr.Method} not found")
                               ));
                }
            }

            throw new RuntimeError(expr.Method, $"Super method {expr.Method} not found");
        }
Exemple #13
0
        public LoxVoid VisitSuperExpr(Expr.Super expr)
        {
            ResolveLocal(expr, expr.Keyword);

            switch (_currentClass)
            {
            case ClassType.None:
                Lox.Error(expr.Keyword, "Cannot use 'super' outside of class.");
                break;

            case ClassType.SubClass:
                break;

            default:
                Lox.Error(expr.Keyword, "Cannot use 'super' in a class with not superclass.");
                break;
            }
            return(null);
        }
Exemple #14
0
        public object Visit(Expr.Super expr)
        {
            // Look up the superclass
            int?     distance   = _locals.Get(expr);
            LoxClass superclass = (LoxClass)_environment.GetAt(distance.Value, "super");

            // "this" is always one level nearer than "super"
            LoxInstance obj = (LoxInstance)_environment.GetAt(distance.Value - 1, "this");

            // Lookup the method
            LoxFunction method = superclass.FindMethod(obj, expr.Method.Lexeme);

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

            return(method);
        }
        public object visitSuperExpr(Expr.Super superExpr)
        {
            captureToken(superExpr.keyword);

            if (currentClass == null)
            {
                error("Cannot use 'super' outside of a class.");
            }
            else if (!currentClass.hasSuperclass)
            {
                error("Cannot use 'super' in a class with no superclass.");
            }

            captureToken(superExpr.method);

            byte name = identifierConstant(superExpr.method);

            getNamedVariable(syntheticToken("this"));

            getNamedVariable(syntheticToken("super"));
            emitBytes((byte)OpCode.OP_GET_SUPER, name);

            return(null);
        }
Exemple #16
0
 public Token visitSuperExpr(Expr.Super super)
 {
     return(super.keyword);
 }
Exemple #17
0
 public string VisitSuperExpr(Expr.Super expr)
 {
     throw new NotImplementedException();
 }