예제 #1
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxEnvironment environment = new LoxEnvironment(closure);

            for (int i = 0; i < declaration.Params.Count; i++)
            {
                environment.Define(declaration.Params[i].Lexeme, arguments[i]);
            }

            try
            {
                interpreter.ExecuteBlock(declaration.Body, environment);
            }
            catch (ReturnException returnValue)
            {
                if (isInitializer)
                {
                    return(closure.GetAt(0, "this"));
                }

                return(returnValue.Value);
            }

            if (isInitializer)
            {
                return(closure.GetAt(0, "this"));
            }

            return(null);
        }
예제 #2
0
        public object VisitSuperExpr(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 RuntimeException(expr.Method, $"Undefined property '{expr.Method.Lexeme}'.");
            }

            return(method.Bind(@object));
        }