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)); } }
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); }
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)); } }
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)); } }
public virtual void Execute(ILexemesStack stack, ExecutorContext context) { }
public abstract InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context);
public InterpreterState MoveToFinalState(int lastSymbolIndex, ILexemesStack stack, ExecutorContext context) { var lastSymbol = SymbolFactory.CreateSymbol(')', lastSymbolIndex); return(MoveToNextState(lastSymbol, stack, context)); }
public override InterpreterState MoveToNextState(Symbol symbol, ILexemesStack stack, ExecutorContext context) { throw new NotImplementedException(); }
public override void Execute(ILexemesStack stack, ExecutorContext context) { throw new SyntaxException(_errorSymbol); }
public override void Execute(ILexemesStack stack, ExecutorContext context) { stack.PopOperators(context); }
public Interpreter(ExecutorContext context) { _stack = new CalculationLexemesStack(); _context = context; }