コード例 #1
0
ファイル: LoxClass.cs プロジェクト: djangbahevans/CsLox
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = FindMethod("init");

            initializer?.Bind(instance).Call(interpreter, arguments);
            return(instance);
        }
コード例 #2
0
ファイル: Interpreter.cs プロジェクト: sirgru/CsLox
        public Object Call(Interpreter interpreter, List <Object> arguments)
        {
            LoxInstance instance = new LoxInstance(this);

            LoxFunction initializer = FindMethod("init");

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

            return(instance);
        }
コード例 #3
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int?     distance   = _locals[expr];
            LoxClass superclass = (LoxClass)_environment.GetAt((int)distance, "super");

            LoxInstance @object = (LoxInstance)_environment.GetAt((int)distance - 1, "this");

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

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

            return(method.Bind(@object));
        }
コード例 #4
0
ファイル: LoxInstance.cs プロジェクト: ashcolecarr/CsLox
        public object Get(Token name)
        {
            if (fields.TryGetValue(name.Lexeme, out object value))
            {
                return(value);
            }

            LoxFunction method = @class.FindMethod(name.Lexeme);

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

            throw new RuntimeException(name, $"Undefined property '{name.Lexeme}'.");
        }
コード例 #5
0
ファイル: LoxInstance.cs プロジェクト: djangbahevans/CsLox
        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 RuntimeError(name, $"Undefined property '{name.Lexeme}'.");
        }
コード例 #6
0
ファイル: Interpreter.cs プロジェクト: ashcolecarr/CsLox
        public object VisitSuperExpr(Super expr)
        {
            int      distance   = locals[expr];
            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(expr.Method.Lexeme);

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

            return(method.Bind(@object));
        }
コード例 #7
0
ファイル: Interpreter.cs プロジェクト: sirgru/CsLox
        public Object Visit_SuperExpr(SuperExpr 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");

            string      name   = (string)expr.method.value;
            LoxFunction method = superclass.FindMethod(name);

            if (method == null)
            {
                throw new RuntimeException(expr.method.line, expr.method.column, "Undefined property '" + name + "'.");
            }

            return(method.Bind(obj));
        }
コード例 #8
0
ファイル: Interpreter.cs プロジェクト: sirgru/CsLox
        public Object Get(Token nameToken)
        {
            var name   = (string)nameToken.value;
            var hasKey = _fields.TryGetValue(name, out Object value);

            if (hasKey)
            {
                return(value);
            }

            LoxFunction method = _klass.FindMethod(name);

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

            throw new RuntimeException(nameToken.line, nameToken.column, "Undefined property '" + name + "'.");
        }