示例#1
0
        public object Call(Evaluator evaluator, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = FindMethod("init");

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

            return(instance);
        }
示例#2
0
        private object EvaluateSuperExpression(SuperExpression expr)
        {
            int         distance   = _locals[expr];
            LoxClass    superclass = _environment.GetAt(distance, new Token(SyntaxKind.Super, "super", null, 0)) as LoxClass;
            LoxInstance obj        = _environment.GetAt(distance - 1, new Token(SyntaxKind.This, "this", null, 0)) as LoxInstance;

            LoxFunction method = superclass.FindMethod(expr.Method.Lexeme);

            if (method is null)
            {
                throw new RuntimeError(expr.Method, $"Undefine property {expr.Method.Lexeme}");
            }
            return(method.Bind(obj));
        }
示例#3
0
        public object Get(Token name)
        {
            if (fields.ContainsKey(name.Lexeme))
            {
                return(fields[name.Lexeme]);
            }

            LoxFunction method = klass.FindMethod(name.Lexeme);

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

            throw new RuntimeError(name, "Undefined property '" + name.Lexeme + "'.");
        }
示例#4
0
        public object VisitSuperExpr(Super expr)
        {
            int      distance   = locals[expr];
            LoxClass superclass = (LoxClass)environment.GetAt(distance, "super");

            LoxInstance obj = (LoxInstance)environment.GetAt(distance - 1, "this");

            LoxFunction method = superclass.FindMethod(expr.Method.Lexeme);

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

            return(method.Bind(obj));
        }
示例#5
0
        object Expr.IVisitor <object> .Visit(Expr.Super _super)
        {
            int      distance   = _locals[_super];
            LoxClass superclass = (LoxClass)_environment.GetAt(distance, "super");

            // "this" is always one level nearer than "super"'s environment.
            LoxInstance _object = (LoxInstance)_environment.GetAt(distance - 1, "this");

            LoxFunction method = superclass.FindMethod(_super.method.lexeme);

            if (method == null)
            {
                throw new RuntimeError(_super.method, "Undefined property '" + _super.method.lexeme + "'.");
            }

            return(method.Bind(_object));
        }