Esempio n. 1
0
 private void ResolveSuperExpression(SuperExpression expr)
 {
     if (_currentClass == ClassType.None)
     {
         Error(expr.Keyword, "cannot use 'super' outside a class");
     }
     else if (_currentClass != ClassType.SubClass)
     {
         Error(expr.Keyword, "cannot use 'super' in a class with no superclass.");
     }
     ResolveLocal(expr, expr.Keyword);
 }
Esempio n. 2
0
        private object EvaluateSuperExpression(SuperExpression expr)
        {
            int         distance   = _locals[expr];
            LoxClass    superclass = (LoxClass)_environment.GetAt(distance, new Token(TokenType.Super, "super", null, 0));
            LoxInstance obj        = (LoxInstance)_environment.GetAt(distance - 1, new Token(TokenType.This, "this", null, 0));

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

            if (method == null)
            {
                throw new RuntimeError(expr.Method, $"Undefine property {expr.Method.Lexeme}");
            }
            return(method.Bind(obj));
        }