예제 #1
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;
            string trimmed = pattern.Trim();



            bool isMatchRound   = Regex.IsMatch(trimmed, REGEX_ROUND);
            bool isMatchSquared = Regex.IsMatch(trimmed, REGEX_SQUARED);
            bool isAnyMatch     = isMatchRound || isMatchSquared;



            if (!isAnyMatch)
            {
                return(false);
            }

            item         = new BracketItem();
            item.Pattern = pattern;

            string args = null;

            if (isMatchRound)
            {
                args = RegexTools.GetValue(pattern, REGEX_ROUND, "args");
            }

            if (isMatchSquared)
            {
                args = RegexTools.GetValue(pattern, REGEX_SQUARED, "args");
            }

            FormulaItem argsItem = builder.Create(args);

            (item as BracketItem).Arguments.Add(argsItem);
            if (isMatchRound)
            {
                (item as BracketItem).TypeOfBracket = BracketShapeType.Round;
            }

            if (isMatchSquared)
            {
                (item as BracketItem).TypeOfBracket = BracketShapeType.Squared;
            }


            return(true);
        }
예제 #2
0
        public override bool TryCreate(List <string> lines, CalculationBuilder builder, out CalculationItem item)
        {
            string pattern = lines.First();

            bool isMatch = Regex.IsMatch(pattern, REGEX_PATTERN);

            if (!isMatch)
            {
                item = null;
                return(false);
            }

            string left  = RegexTools.GetValue(pattern, REGEX_PATTERN, "left");
            string right = RegexTools.GetValue(pattern, REGEX_PATTERN, "right");
            string sign  = RegexTools.GetValue(pattern, REGEX_PATTERN, "sign");


            If            ifStatement = new If();
            Condition     condition   = new Condition();
            List <string> leftList    = new List <string>();

            leftList.Add(left);
            condition.Left = (Equation)builder.Create(leftList);
            List <string> rightList = new List <string>();

            rightList.Add(right);
            condition.Right           = (Equation)builder.Create(rightList);
            condition.TypeOfCondition = getType(sign);

            ifStatement.Condition = condition;

            builder.RemoveLines(lines, 1);
            ifStatement.TrueBlock = (EquationBlock)builder.Create(lines);

            string nextLine = lines.First();

            if (nextLine == "else")
            {
                builder.RemoveLines(lines, 1);
                ifStatement.FalseBlock = (EquationBlock)builder.Create(lines);
            }


            item = ifStatement;
            return(true);
        }
예제 #3
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item    = null;
            pattern = pattern.Trim();
            string args = RegexTools.GetValue(pattern, REGEX, "args");

            if (string.IsNullOrEmpty(args))
            {
                return(false);
            }

            item         = new FunctionAbs();
            item.Pattern = pattern;
            FormulaItem arg = builder.Create(args);

            (item as FunctionAbs).Arguments.Add(arg);

            return(true);
        }
예제 #4
0
        public override bool TryCreate(List <string> lines, CalculationBuilder builder, out CalculationItem item)
        {
            string pattern = lines.First();

            bool isMatch = Regex.IsMatch(pattern, REGEX_PATTERN);

            if (!isMatch)
            {
                FormulaItem supposedFormula = _formulaBuilder.Create(pattern);
                if (supposedFormula is ValueItem)
                {
                    Equation iEquation = new Equation();
                    iEquation.Value = (ValueItem)supposedFormula;
                    item            = iEquation;
                    builder.RemoveLines(lines, 1);
                    return(true);
                }

                if (supposedFormula is SymbolItem)
                {
                    Equation iEquation = new Equation();
                    iEquation.Symbol = (SymbolItem)supposedFormula;
                    item             = iEquation;
                    builder.RemoveLines(lines, 1);
                    return(true);
                }

                item = null;
                return(false);
            }

            string left  = RegexTools.GetValue(pattern, REGEX_PATTERN, "left");
            string right = RegexTools.GetValue(pattern, REGEX_PATTERN, "right");

            FormulaItem symbol  = _formulaBuilder.Create(left);
            FormulaItem formula = _formulaBuilder.Create(right);

            Equation equation = new Equation();

            equation.Symbol = (SymbolItem)symbol;

            if (formula is ValueItem)
            {
                equation.Value = (ValueItem)formula;
            }
            else
            {
                if (formula.IsSymbol)
                {
                    equation.Symbolic = formula;
                }
                else
                {
                    equation.Numbers = formula;
                }
            }

            item = equation;

            builder.RemoveLines(lines, 1);

            return(true);
        }