public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            switch (symbol.Type)
            {
            case SymbolType.Number:
            case SymbolType.Identifier:
                return(new ErrorState(symbol));

            case SymbolType.Operator:
                var @operator = OperatorFactory.CreateOperator(symbol);
                stack.Push(@operator);

                switch (@operator)
                {
                case ClosingBracket _:
                    return(new ClosingBracketOperatorState());

                case CommaOperator _:
                case OpeningBracket _:
                case AssignmentOperator _:
                    return(new ErrorState(symbol));

                default:
                    return(new BinaryOperatorState());
                }

            default:
                return(new ErrorState(symbol));
            }
        }
        public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            switch (symbol.Type)
            {
            case SymbolType.Number:
            case SymbolType.Identifier:
                return(new FunctionArgumentReadingState(_functionSignature, symbol));

            case SymbolType.Operator:
                var @operator = OperatorFactory.CreateOperator(symbol, false);

                switch (@operator)
                {
                case OpeningBracket _:
                case PlusOperator _:
                case MinusOperator _:
                    return(new FunctionArgumentReadingState(_functionSignature, symbol));

                default:
                    return(new ErrorState(symbol));
                }

            default:
                return(new ErrorState(symbol));
            }
        }
Example #3
0
        public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            var validatorState = _functionBodyValidator.AppendSymbol(symbol);

            if (validatorState is ErrorState)
            {
                return(validatorState);
            }

            _functionBodyBuilder.Append(symbol);

            if (symbol.IsLast)
            {
                if (_functionBodyValidator.IsValid())
                {
                    _function.Body = _functionBodyBuilder.Body;
                    context._functionsRepository.Add(_function);
                    return(new ClosingBracketOperatorState());
                }
                else
                {
                    return(new ErrorState(symbol));
                }
            }

            return(this);
        }
Example #4
0
        public static InterpreterState StartFromInitialState(ILexemesStack stack)
        {
            var openingBracket = new OpeningBracket(SymbolFactory.CreateSymbol('(', -1));

            stack.Push(openingBracket);

            return(new OpeningBracketOperatorState());
        }
        public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            switch (symbol.Type)
            {
            case SymbolType.Number:
            case SymbolType.Identifier:
                _argumentBuilder.Append(symbol);
                return(this);

            case SymbolType.Operator:
                var @operator = OperatorFactory.CreateOperator(symbol);

                switch (@operator)
                {
                case OpeningBracket _:
                    _bracketsBalance++;
                    _argumentBuilder.Append(symbol);
                    return(this);

                case ClosingBracket _:
                    _bracketsBalance--;
                    if (_bracketsBalance == 0)
                    {
                        _functionSignature.AppendArgument(_argumentBuilder);
                        return(new FunctionSignatureEndState(_functionSignature));
                    }

                    _argumentBuilder.Append(symbol);
                    return(this);

                case CommaOperator _:
                    if (_bracketsBalance != 1)
                    {
                        return(new ErrorState(symbol));
                    }

                    if (_argumentBuilder.IsEmpty)
                    {
                        return(new ErrorState(symbol));
                    }

                    _functionSignature.AppendArgument(_argumentBuilder);
                    return(new FunctionArgumentReadingState(_functionSignature, null));

                default:
                    _argumentBuilder.Append(symbol);
                    return(this);
                }

            default:
                return(new ErrorState(symbol));
            }
        }
Example #6
0
        public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            switch (symbol.Type)
            {
            case SymbolType.Number:
            case SymbolType.Identifier:
                if (symbol.Value.Equals(NumberFactory.DecimalSeparator))
                {
                    return(new ErrorState(symbol));
                }

                _variableBuilder.Append(symbol);
                return(this);

            case SymbolType.Operator:
                var @operator  = OperatorFactory.CreateOperator(symbol);
                var identifier = VariableFactory.CreateVariable(_variableBuilder);

                if (@operator is OpeningBracket)
                {
                    return(new FunctionSignatureStartState(identifier));
                }

                stack.Push(identifier);
                stack.Push(@operator);

                switch (@operator)
                {
                case AssignmentOperator _:
                    return(new AssignmentOperatorState());

                case ClosingBracket _:
                    return(new ClosingBracketOperatorState());

                case CommaOperator _:
                    return(new ErrorState(symbol));

                default:
                    return(new BinaryOperatorState());
                }

            default:
                return(new ErrorState(symbol));
            }
        }
        public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
        {
            switch (symbol.Type)
            {
            case SymbolType.Number:
            case SymbolType.Identifier:
                return(new ErrorState(symbol));

            case SymbolType.Operator:
                var @operator = OperatorFactory.CreateOperator(symbol);
                if (@operator is OpeningBracket || @operator is CommaOperator)
                {
                    return(new ErrorState(symbol));
                }

                if (@operator is AssignmentOperator)
                {
                    if (_functionSignature.IsValidFunctionDeclaration())
                    {
                        return(new FunctionBodyReadingState(_functionSignature.ConvertToFunction()));
                    }

                    return(new ErrorState(symbol));
                }

                var function = context._functionsRepository.Get(_functionSignature.Name.Body);
                var result   = FunctionExecutor.Execute(_functionSignature, function, context);

                stack.Push(new Number(result.Value));
                stack.Push(@operator);

                if (@operator is ClosingBracket)
                {
                    return(new ClosingBracketOperatorState());
                }

                return(new BinaryOperatorState());

            default:
                return(new ErrorState(symbol));
            }
        }
Example #8
0
 public virtual void Execute(ILexemesStack stack, ExecutorContext context)
 {
 }
Example #9
0
 public abstract InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context);
Example #10
0
        public InterpreterState MoveToFinalState(int lastSymbolIndex, ILexemesStack stack, ExecutorContext context)
        {
            var lastSymbol = SymbolFactory.CreateSymbol(')', lastSymbolIndex);

            return(MoveToNextState(lastSymbol, stack, context));
        }
Example #11
0
 public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public override void Execute(ILexemesStack stack, ExecutorContext context)
 {
     throw new SyntaxException(_errorSymbol);
 }
 public override void Execute(ILexemesStack stack, ExecutorContext context)
 {
     stack.PopOperators(context);
 }
Example #14
0
 public Interpreter(ExecutorContext context)
 {
     _stack   = new CalculationLexemesStack();
     _context = context;
 }