Example #1
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            var environment = new LoxEnvironment(_closure);

            for (int i = 0; i < _declaration.Parameters.Count; i++)
            {
                environment.Define(_declaration.Parameters[i].Lexeme, arguments[i]);
            }
            try
            {
                interpreter.ExecuteBlock(_declaration.Body, environment);
            }
            catch (Return returnValue)
            {
                if (_isInitializer)
                {
                    return(_closure.GetAt(0, "this"));
                }

                return(returnValue.Value);
            }

            if (_isInitializer)
            {
                return(_closure.GetAt(0, "this"));
            }

            return(null);
        }
Example #2
0
        public LoxFunction Bind(LoxInstance loxInstance)
        {
            var environment = new LoxEnvironment(_closure);

            environment.Define("this", loxInstance);
            return(new LoxFunction(_declaration, environment, _isInitializer));
        }
Example #3
0
        public void ExecuteBlock(List <Stmt> statements, LoxEnvironment environment)
        {
            var previous = _environment;

            try
            {
                _environment = environment;

                foreach (var statement in statements)
                {
                    Execute(statement);
                }
            }
            finally
            {
                _environment = previous;
            }
        }
Example #4
0
        public Void 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 LoxEnvironment(_environment);
                _environment.Define("super", superclass);
            }

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

            foreach (var method in stmt.Methods)
            {
                var isInitializer = method.Name.Lexeme.Equals("init", StringComparison.Ordinal);
                var function      = new LoxFunction(method, _environment, isInitializer);
                methods[method.Name.Lexeme] = function;
            }

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

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

            _environment.Assign(stmt.Name, lclass);
            return(null);
        }
Example #5
0
 public Interpreter()
 {
     Globals.Define("clock", new Clock());
     _environment = Globals;
 }
Example #6
0
 public LoxFunction(Stmt.Function declaration, LoxEnvironment closure, bool isInitializer)
 {
     _declaration   = declaration;
     _closure       = closure;
     _isInitializer = isInitializer;
 }
Example #7
0
 public LoxAnonymousFunction(Expr.AnonymousFunction declaration, LoxEnvironment closure)
 {
     _declaration = declaration;
     _closure     = closure;
 }