示例#1
0
        public CoxFunction Bind(CoxInstance self)
        {
            Environment environment = new Environment(Closure);

            environment.Define("this", self);
            return(new CoxFunction(Declaration, environment, IsInitializer));
        }
示例#2
0
文件: CoxClass.cs 项目: gajcowan/cox
        public Object Call(Interpreter interpreter, List <Object> arguments)
        {
            CoxInstance instance = new CoxInstance(this);

            CoxFunction initializer = Methods["init"];

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, arguments);
            }

            return(instance);
        }
示例#3
0
文件: CoxClass.cs 项目: gajcowan/cox
        public CoxFunction FindMethod(CoxInstance instance, String name)
        {
            CoxClass klass = this;

            while (klass != null)
            {
                if (klass.Methods.ContainsKey(name))
                {
                    return(klass.Methods[name].Bind(instance));
                }

                klass = klass.Superclass;
            }

            return(null);
        }
示例#4
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);
        }