예제 #1
0
 public object visit_Super_Expr(GExpr.Super expr)
 {
     if (currentClass == ClassType.NONE)
     {
         Program.error(expr.keyword, "Cannot use 'super' outside of a class");
     }
     else if (currentClass != ClassType.SUBCLASS)
     {
         Program.error(expr.keyword, "Cannot use 'super' in a class with no superclass");
     }
     resolveLocal(expr, expr.keyword);
     return(null);
 }
예제 #2
0
        public object visit_Super_Expr(GExpr.Super expr)
        {
            int      distance   = locals[expr];
            LoxClass superClass = (LoxClass)environment.getAt(distance, "super");
            // "this" is always 1 level nearer than "super's" environment

            LoxInstance obj = (LoxInstance)environment.getAt(distance - 1, "this");

            LoxFunction method = superClass.findMethod(obj, expr.method.lexeme);

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

            return(method);
        }