Exemple #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            try {
                List <string> lines   = textBox3.Lines.ToList();
                List <string> cleared = _calcBuilder.getCleaned(lines);

                cleared.Insert(0, "{");
                cleared.Add("}");

                CalculationItem      item  = _calcBuilder.Create(cleared);
                CalculationItemStack stack = new CalculationItemStack();
                item.Calculate(stack);

                EquationBlock block = new EquationBlock();
                foreach (CalculationItem stackItem in stack.Items)
                {
                    if (stackItem is Equation)
                    {
                        block.Equations.Add(stackItem);
                    }
                }

                _multiElement = _converter.Convert(block);
                panel2.Invalidate();
            } catch (Exception exc) {
                MessageBox.Show(exc.ToString(), "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public CalculationItem Create(List <string> lines)
        {
            CalculationItem item = null;

            while (lines.Count > 0)
            {
                foreach (CalculationPattern pattern in _patterns)
                {
                    if (!pattern.TryCreate(lines, this, out item))
                    {
                        continue;
                    }

                    return(item);
                }
            }

            return(null);
        }
Exemple #3
0
        public Element Convert(CalculationItem item)
        {
            if (item is EquationBlock)
            {
                EquationBlock bl = (EquationBlock)item;

                EquationBlockElement blockElement = new EquationBlockElement();
                foreach (CalculationItem eq in bl.Equations)
                {
                    Element eqElem = (Element)Convert(eq);
                    blockElement.Arguments.Add(eqElem);
                }

                return(blockElement);
            }

            if (item is Equation)
            {
                Equation eq = (Equation)item;

                EquationElement eqElem = new EquationElement();
                if (null != eq.Symbol)
                {
                    eqElem.Arguments.Add(Convert(eq.Symbol));
                }
                if (null != eq.Symbolic)
                {
                    eqElem.Arguments.Add(Convert(eq.Symbolic));
                }
                if (null != eq.Numbers)
                {
                    eqElem.Arguments.Add(Convert(eq.Numbers));
                }
                if (null != eq.Value)
                {
                    eqElem.Arguments.Add(Convert(eq.Value));
                }
                return(eqElem);
            }

            return(null);
        }
        public override bool TryCreate(List <string> lines, CalculationBuilder builder, out CalculationItem item)
        {
            bool isMatch = (lines.First() == "{");

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


            EquationBlock block = new EquationBlock();

            int level = 0;

            builder.RemoveLines(lines, 1);
            for ( ; ;)
            {
                CalculationItem eq = builder.Create(lines);
                block.Equations.Add(eq);

                string currentLine = lines.First();
                if (currentLine == "}")
                {
                    if (level == 0)
                    {
                        break;
                    }
                    else
                    {
                        level--;
                    }
                }
            }

            builder.RemoveLines(lines, 1);
            item = block;
            return(true);
        }
        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);
        }
        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);
        }
 public abstract bool TryCreate(List <string> lines, CalculationBuilder builder, out CalculationItem item);