Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        public int CalculatePostfixString(string[] values)
        {
            int result = 0;
            var stack  = new Stack <int>();

            foreach (string value in values)
            {
                // if value == number
                //	then push to the stack
                if (IsNumeric(value))
                {
                    stack.Push(int.Parse(value));
                }
                else if (IsOperator(value))
                {
                    // if value == operator
                    //	then pop first two values
                    //	then $result = operator(second popped value, first popped value)
                    //	then push $result to the stack
                    var rightValue = stack.Pop();
                    var leftValue  = stack.Pop();

                    var @operator = OperatorFactory.CreateOperator(value);
                    result = @operator.Operate(leftValue, rightValue);

                    stack.Push(result);
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        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));
            }
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
0
        public static List <BaseOperator> CreateListOperator(List <Relationship> relationshipKeyJoins)
        {
            var operators = new List <BaseOperator>();
            var factory   = new OperatorFactory();

            relationshipKeyJoins.ForEach(join =>
            {
                if (join.OperatorName != null)
                {
                    operators.Add(factory.CreateOperator(join.OperatorName));
                }
                if (join.UnaryOperator != null)
                {
                    operators.Add(factory.CreateOperator(join.UnaryOperator));
                }
                if (join.Value.HasValue)
                {
                    operators.Add(factory.CreateValueOperator(join.Value.Value));
                }
            });
            return(operators);
        }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 7
0
        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));
            }
        }
Ejemplo n.º 8
0
 private static bool IsOperator(string value)
 {
     return(OperatorFactory.CreateOperator(value) is IOperator);
 }
Ejemplo n.º 9
0
        public ISolvable Parse(string candidate)
        {
            if (!candidate.StartsWith("? "))
            {
                throw new ArgumentException("Expression missing marker");
            }

            candidate = candidate.Substring(2);

            // Split the string based on the format of operators
            var tokens = Regex.Split(candidate, @"(\s+[*]\s+)|(\s+[/]\s+)|(\s+[+]\s+)|(\s+[-]\s+)");

            // Since we dont support parenthesis, we can check based on expected number
            // of operands and operator.
            if (tokens.Length != 1 && ((tokens.Length - 1) % 2 != 0))
            {
                throw new ArgumentException("Incomplete expression");
            }

            Expression root     = null;
            Expression previous = null;

            // Short circuit for single operand
            if (tokens.Length == 1)
            {
                return(Operand.Parse(tokens[0]));
            }

            // Iterate through pottential operators
            for (int i = 1; i <= tokens.Length - 2; i += 2)
            {
                var newExpr = new Expression(OperatorFactory.CreateOperator(tokens[i]), previous);
                var op2     = tokens[i + 1];
                newExpr.Right = Operand.Parse(op2);

                if (root == null)
                {
                    root = newExpr;

                    var op1 = tokens[i - 1];
                    newExpr.Left = Operand.Parse(op1);
                }
                // Previous had a higher priority e.g. * over +
                else if (previous.Operator.Priority > newExpr.Operator.Priority)
                {
                    newExpr.Left   = previous.Right;
                    previous.Right = newExpr;
                }
                // Previous is lower priority, so we need to switch parents
                else
                {
                    newExpr.Left = previous;

                    // new root
                    if (previous.Parent == null)
                    {
                        root = newExpr;
                    }
                    else
                    {
                        previous.Parent.Right = newExpr;
                        newExpr.Parent        = previous.Parent; // fix the parent since we attaching higher in the tree
                    }
                }

                previous = newExpr;
            }

            return(root);
        }