Ejemplo n.º 1
0
        public bool VisitFunctionDeclaration(FunctionDeclaration function)
        {
            string name  = function.Name.Lexeme;
            object value = new LoxFunction(function, _env);

            _env.Define(name, value);
            return(true);
        }
Ejemplo n.º 2
0
        public bool VisitReturnStatement(ReturnStatement returnStatement)
        {
            //TODO: the overhead of using an exception to hack stack unwinding makes me cringe :/
            object value = returnStatement.Value?.Accept(this);

            LoxFunction.Return(value);
            return(true);
        }
Ejemplo n.º 3
0
        public object VisitFunction(Expr.Function expr)
        {
            // Capture the environment at declaration time.
            string name     = string.Format("<fun @{0}>", expr.Fun.Line);
            var    function = new LoxFunction(name, _environment, expr.Parameters, expr.Body);

            return(function);
        }
Ejemplo n.º 4
0
        public Void Visit(Stmt.Function stmt)
        {
            var function = new LoxFunction(stmt, environment, false);

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

            return(Void.Instance);
        }
Ejemplo n.º 5
0
        public Unit VisitFunction(Stmt.Function stmt)
        {
            // Capture the environment at declaration time.
            string name     = string.Format("<fun {0}>", stmt.Name.Lexeme);
            var    function = new LoxFunction(name, _environment, stmt.Parameters, stmt.Body);

            _environment.Define(stmt.Name.Lexeme, function);
            return(Unit.Default);
        }
Ejemplo n.º 6
0
        public override int Arity()
        {
            LoxFunction initializer = methods["init"];

            if (initializer == null)
            {
                return(0);
            }
            return(initializer.Arity());
        }
Ejemplo n.º 7
0
        public override object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = methods["init"];

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, arguments);
            }
            return(instance);
        }
Ejemplo n.º 8
0
        object Expr.IVisitor <object> .VisitSuperExpr(Expr.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(obj, expr.method.lexeme);

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

            return(method);
        }
Ejemplo n.º 9
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 + "'.");
        }
Ejemplo n.º 10
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            environment.Define(stmt.Name.Lexeme, null);

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

            stmt.Methods.ForEach(m =>
            {
                var function = new LoxFunction(m, environment, m.name.Equals("init"));
                methods.Add(m.name.Lexeme, function);
            });

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

            environment.Assign(stmt.Name, klass);
            return(null);
        }
Ejemplo n.º 11
0
        object Stmt.IVisitor <object> .VisitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

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

            environment.Define(stmt.name.lexeme, null);

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

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

            foreach (Stmt.Function method in stmt.methods)
            {
                LoxFunction function = new LoxFunction(method, environment, method.name.lexeme.Equals("init"));
                methods[method.name.lexeme] = function;
            }


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

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

            environment.Assign(stmt.name, klass);
            return(null);
        }
Ejemplo n.º 12
0
        public Void Visit(Stmt.Class stmt)
        {
            environment.Define(stmt.Name.Lexeme, null);

            object superclass = null;

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

                environment = new Environment(environment);
                environment.Define("super", superclass);
            }

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

            foreach (var method in stmt.Methods)
            {
                var function = new LoxFunction(method, environment, method.Name.Lexeme.Equals("this"));
                methods[method.Name.Lexeme] = function;
            }

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

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

            environment.Assign(stmt.Name, klass);

            return(null);
        }