Пример #1
0
        public object VisitFunctionStmt(FunctionStmt stmt)
        {
            var func = new LoxFunction(stmt, _scope, false);

            _scope.Define(stmt.Name.Lexeme, func);

            return(null);
        }
Пример #2
0
        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);
        }
Пример #3
0
        public object VisitFunctionStmt(Function stmt)
        {
            LoxFunction function = new LoxFunction(stmt, environment, false);

            environment.Define(stmt.Name.Lexeme, function);

            return(null);
        }
Пример #4
0
        public int Arity()
        {
            LoxFunction initializer = FindMethod("init");

            return(initializer?.Arity() ?? 0);

            ;
        }
Пример #5
0
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            LoxFunction function = new LoxFunction(stmt, _environment, false);

            if (stmt.Name != null)
            {
                _environment.Define(stmt.Name.Lexeme, function);
            }
            return(null);
        }
Пример #6
0
        public int Arity()
        {
            LoxFunction initializer = FindMethod("init");

            if (initializer == null)
            {
                return(0);
            }

            return(initializer.Arity());
        }
Пример #7
0
        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);
        }
Пример #8
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));
        }
Пример #9
0
        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}'.");
        }
Пример #10
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 RuntimeError(name, $"Undefined property '{name.Lexeme}'.");
        }
Пример #11
0
        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));
        }
Пример #12
0
        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));
        }
Пример #13
0
        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 + "'.");
        }
Пример #14
0
        public Void Visit_ClassStmt(ClassStmt stmt)
        {
            Object superclass = null;

            if (stmt.superclass != null)
            {
                superclass = Evaluate(stmt.superclass);
                if (!(superclass is LoxClass))
                {
                    throw new RuntimeException(stmt.superclass.name.line, stmt.superclass.name.column, "Superclass must be a class.");
                }
            }

            var name = (string)stmt.name.value;

            _environment.Define(name, null);

            if (stmt.superclass != null)
            {
                _environment = new Environment(_environment);
                _environment.Define("super", superclass);
            }

            Dictionary <String, LoxFunction> methods = new Dictionary <string, LoxFunction>();

            foreach (var method in stmt.methods)
            {
                LoxFunction function = new LoxFunction(method, _environment, name == "init");
                methods.Add((string)method.name.value, function);
            }

            LoxClass klass = new LoxClass((string)stmt.name.value, (LoxClass)superclass, methods);

            if (superclass != null)
            {
                _environment = _environment.enclosing;
            }

            _environment.Assign(stmt.name, klass);
            return(null);
        }
Пример #15
0
        public object VisitClassStmt(Class stmt)
        {
            object superclass = null;

            if (stmt.Superclass != null)
            {
                superclass = Evaluate(stmt.Superclass);
                if (!(superclass is LoxClass))
                {
                    throw new RuntimeException(stmt.Superclass.Name, "Superclass must be a class.");
                }
            }

            environment.Define(stmt.Name.Lexeme, null);

            if (stmt.Superclass != null)
            {
                environment = new LoxEnvironment(environment);
                environment.Define("super", superclass);
            }

            Dictionary <string, LoxFunction> methods = new Dictionary <string, LoxFunction>();

            foreach (Function method in stmt.Methods)
            {
                LoxFunction function = new LoxFunction(method, environment, method.Name.Lexeme.Equals("init"));
                methods.Add(method.Name.Lexeme, function);
            }

            LoxClass @class = new LoxClass(stmt.Name.Lexeme, (LoxClass)superclass, methods);

            if (superclass != null)
            {
                environment = environment.Enclosing;
            }

            environment.Assign(stmt.Name, @class);

            return(null);
        }
Пример #16
0
        public object VisitClassStmt(ClassStmt stmt)
        {
            _scope.Define(stmt.Name.Lexeme, null);
            var superClass = default(LoxClass);

            if (stmt.SuperClass != null)
            {
                superClass = Evaluate(stmt.SuperClass) as LoxClass;
                if (superClass == null)
                {
                    throw new LoxRuntimeException(stmt.SuperClass.Name, "Base class must be a class.");
                }

                _scope = new Scope(_scope);
                _scope.Define("super", superClass);
            }

            var methods = new Dictionary <string, LoxFunction>();

            foreach (var method in stmt.Methods)
            {
                var func = new LoxFunction(method, _scope, method.Name.Lexeme.Equals("init"));
                methods.Add(method.Name.Lexeme, func);
            }

            LoxClass klass = new LoxClass(stmt.Name.Lexeme, superClass, methods);

            if (superClass != null)
            {
                _scope = _scope.Parent;
            }

            _scope.Assign(stmt.Name, klass);

            return(null);
        }