Exemplo n.º 1
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int distance;

            if (locals.TryGetValue(expr, out distance))
            {
                LoxClass    superclass = (LoxClass)environment.GetAt(distance, "super");
                LoxInstance obj        = (LoxInstance)environment.GetAt(distance - 1, "this");

                LoxFunction method = null;
                if (expr.Method != null)
                {
                    method = superclass.FindMethod(expr.Method.Lexeme);
                }
                else
                {
                    method = superclass.FindMethod(superclass.Name);
                }

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

                return(method.Bind(obj));
            }

            return(null);
        }
Exemplo n.º 2
0
        public LoxFunction FindMethod(string name)
        {
            if (methods.ContainsKey(name))
            {
                return(methods[name]);
            }

            if (superclass != null)
            {
                return(superclass.FindMethod(name));
            }

            return(null);
        }
Exemplo n.º 3
0
        public object Get(Token name)
        {
            if (fields.ContainsKey(name.Lexeme))
            {
                return(fields[name.Lexeme]);
            }

            var method = loxClass.FindMethod(name.Lexeme);

            if (method != null)
            {
                return(method.Bind(this));
            }

            throw new InterpretingException(name,
                                            $"Undefined property '{name.Lexeme}'.");
        }