Example #1
0
        public LoxFunction findMethod(LoxInstance instance, string name)
        {
            if (methods.ContainsKey(name))
            {
                return(methods[name].bind(instance));
            }

            if (superclass != null)
            {
                return(superclass.findMethod(instance, name));
            }

            return(null);
        }
Example #2
0
        public object get(Token name)
        {
            if (fields.ContainsKey(name.lexeme))
            {
                return(fields[name.lexeme]);
            }
            LoxFunction method = _class.findMethod(name.lexeme);

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

            throw new Exceptions.RuntimeError(name, "Undefined property '" + name.lexeme + "'.");
        }
Example #3
0
        public object get(Token name)
        {
            if (fields.ContainsKey(name.lexeme))
            {
                return(fields[name.lexeme]);
            }

            LoxFunction method = klass.findMethod(this, name.lexeme);

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

            throw new RuntimeError(name, $"Undefined properrty '{name.lexeme}'.");
        }
Example #4
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);
        }