Пример #1
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            var distance   = _locals[expr];
            var superclass = (LoxClass)_environment.GetAt(distance, "super");

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

            var method = superclass.FindMethod(@object, expr.Method.Lexeme);

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

            return(method);
        }
Пример #2
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            var environment = new EnvironmentScope(_closure);

            for (var i = 0; i < _declaration.Parameters.Count; i++)
            {
                environment.Define(_declaration.Parameters[i].Lexeme, arguments[i]);
            }

            try
            {
                interpreter.ExecuteBlock(_declaration.Body, environment);
            }
            catch (Return @return)
            {
                return(@return.Value);
            }

            if (_isInitializer)
            {
                return(_closure.GetAt(0, "this"));
            }
            return(null);
        }