示例#1
0
        public LoxFunction FindMethod(LoxInstance instance, string name)
        {
            LoxFunction ret = null;

            if (methods.ContainsKey(name))
            {
                ret = methods[name].Bind(instance);
            }
            else if (superclass != null)
            {
                ret = superclass.FindMethod(instance, name);
            }

            return(ret);
        }
示例#2
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 property '" + name.lexeme + "'.");
        }
示例#3
0
        public object Visit(Expr.Super expr)
        {
            int      distance   = locals[expr];
            LoxClass superclass = (LoxClass)environment.GetAt(distance, "super");

            // "this" is always one 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);
        }
示例#4
0
        public void Visit(Stmt.Function stmt)
        {
            LoxFunction function = new LoxFunction(stmt, environment, false);

            environment.Define(stmt.name.lexeme, function);
        }