Пример #1
0
        public object visitSuperExpr(Expr.Super expr)
        {
            int      distance   = locals[expr];
            LoxClass superClass = (LoxClass)env.getAt(
                distance, "super");
            LoxInstance instance = (LoxInstance)env.getAt(
                distance - 1, "this");

            LoxFunction method = superClass.findMethod(expr.Method.Lexeme);

            if (method == null)
            {
                throw new RuntimeError(expr.Method,
                                       $"Undefined propert '{expr.Method.Lexeme}'");
            }
            return(method.bind(instance));
        }
Пример #2
0
        public object call(Interpreter interpreter, List <object> args)
        {
            Envir envir = new Envir(Closure);

            for (int i = 0; i < Declaration.Parameters.Count; i++)
            {
                envir.define(Declaration.Parameters[i].Lexeme, args[i]);
            }
            try {
                interpreter.executeBlock(Declaration.Body, envir);
            } catch (LoxReturn returnValue) {
                if (IsInitializer)
                {
                    return(Closure.getAt(0, "this"));
                }
                return(returnValue.Value);
            }
            if (IsInitializer)
            {
                return(Closure.getAt(0, "this"));
            }
            return(null);
        }