Beispiel #1
0
        public LoxVoid VisitVarStmt(Stmt.Var stmt)
        {
            stmt.Initializer.Match(
                some: init => _environment.Define(stmt.Name.Lexeme, Evalutate(init)),
                none: () => _environment.Declare(stmt.Name.Lexeme)
                );

            return(null);
        }
Beispiel #2
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            Environment environment = new Environment(_closure);

            for (int i = 0; i < _declaration.Parameter.Count; i++)
            {
                environment.Define(_declaration.Parameter[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);
        }
Beispiel #3
0
        public LoxFunction Bind(LoxInstance instance)
        {
            Environment environment = new Environment(_closure);

            environment.Define("this", instance);
            return(new LoxFunction(_declaration, environment, _isInitializer));
        }
Beispiel #4
0
        internal Interpreter()
        {
            Globals      = new Environment();
            _environment = Globals;

            Globals.Define("clock", new Globals.Clock() as ILoxCallable);
        }
Beispiel #5
0
        public LoxVoid VisitClassStmt(Stmt.Class stmt)
        {
            Option <LoxClass> superclassOption = stmt.Superclass.Match(
                some: variableStmt =>
            {
                object superclassObj = Evalutate(variableStmt);
                if (superclassObj is LoxClass superclass)
                {
                    return(superclass.Some());
                }
                throw new RuntimeError(variableStmt.Name, "Superclass must be a class");
            },
                none: Option.None <LoxClass>
                );

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

            superclassOption.MatchSome(super =>
            {
                _environment = new Environment(_environment);
                _environment.Define("super", super);
            });

            Dictionary <string, LoxFunction> methods = stmt.Methods
                                                       .Select(method => (
                                                                   name: method.Name.Lexeme,
                                                                   method: new LoxFunction(method, _environment, method.Name.Lexeme == "init")
                                                                   ))
                                                       .ToDictionary(
                x => x.name,
                x => x.method
                );

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

            superclassOption.MatchSome(super =>
            {
                _environment = _environment.Enclosing.Match(
                    some: env => env,
                    none: () => throw new Exception("Expected enclosing scope none found")
                    );
            });

            _environment.Assign(stmt.Name, @class);
            return(null);
        }