Exemple #1
0
        public Object GetProperty(Token name)
        {
            if (Fields.ContainsKey(name.Lexeme))
            {
                return(Fields[name.Lexeme]);
            }

            CoxFunction method = Klass.FindMethod(this, name.Lexeme);

            if (method != null)
            {
                return(method);
            }

            throw new RuntimeError(name, "Undefined property '" + name.Lexeme + "'.");
        }
Exemple #2
0
        public object VisitSuperExpr(Super expr)
        {
            int      distance   = Locals[expr];
            CoxClass superclass = (CoxClass)Environment.GetAt(distance, "super");

            // "this" is always one level nearer than "super"'s environment.
            CoxInstance receiver = (CoxInstance)Environment.GetAt(distance - 1, "this");

            CoxFunction method = superclass.FindMethod(receiver, expr.Method.Lexeme);

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

            return(method);
        }