public static void Execute()
        {
            //Examples:
            //GAPoT biversor using terms form:
            //  -1.3<>, 1.2<1,2>, -4.6<3,4>

            var sourceText =
                "-1.3<>, 1.2<1,2>, -4.6<3,4>";

            var parsingResults = new IronyParsingResults(
                new GaPoTNumBiversorConstructorGrammar(),
                sourceText
                );

            var biversor = sourceText.GaPoTNumParseBiversor();

            var composer = new MarkdownComposer();

            composer
            .AppendLine(parsingResults.ToString());

            if (!parsingResults.ContainsErrorMessages && biversor != null)
            {
                composer
                .AppendLine()
                .AppendLine(biversor.TermsToText())
                .AppendLine()
                .AppendLine(biversor.TermsToLaTeX());
            }

            Console.WriteLine(composer.ToString());
        }
        public static GaPoTNumBiversor GaPoTNumParseBiversor(this string sourceText)
        {
            var parsingResults = new IronyParsingResults(
                new GaPoTNumBiversorConstructorGrammar(),
                sourceText
                );

            if (parsingResults.ContainsErrorMessages || !parsingResults.ContainsParseTreeRoot)
            {
                throw new SyntaxErrorException(parsingResults.ToString());
            }

            return(GaPoTNumParseBiversor(parsingResults, parsingResults.ParseTreeRoot));
        }
Exemple #3
0
        public static void Execute()
        {
            //Examples:
            //Single Phase GAPoT vector using terms form:
            //  -1.3<1>, 1.2<3>, -4.6<5>
            //
            //Single Phase GAPoT vector using polar form:
            //  p(233.92, −1.57) <1,2>, p(120, 0) <3,4>
            //
            //Single Phase GAPoT vector using rectangular form:
            //  r(10, 20) <1,2>, r(30, 0) <3,4>

            var sourceText =
                "-1.3<1>, 1.2<3>, -4.6<5>, p(233.92, −1.57) <7,8>, r(10, 20) <9,10>, r(30, 0) <11,12>";

            var parsingResults = new IronyParsingResults(
                new GaPoTNumVectorConstructorGrammar(),
                sourceText
                );

            var mpVector = sourceText.GaPoTNumParseVector();

            var composer = new MarkdownComposer();

            composer
            .AppendLine(parsingResults.ToString());

            if (!parsingResults.ContainsErrorMessages && mpVector != null)
            {
                composer
                .AppendLine()
                .AppendLine(mpVector.TermsToText())
                .AppendLine()
                .AppendLine(mpVector.TermsToLaTeX())
                .AppendLine()
                .AppendLine(mpVector.PolarPhasorsToText())
                .AppendLine()
                .AppendLine(mpVector.PolarPhasorsToLaTeX())
                .AppendLine()
                .AppendLine(mpVector.RectPhasorsToText())
                .AppendLine()
                .AppendLine(mpVector.RectPhasorsToLaTeX());
            }

            Console.WriteLine(composer.ToString());
        }
        private static GaPoTNumBiversor GaPoTNumParseBiversor(IronyParsingResults parsingResults, ParseTreeNode rootNode)
        {
            if (rootNode.ToString() != "biversor")
            {
                throw new SyntaxErrorException(parsingResults.ToString());
            }

            var biversor = new GaPoTNumBiversor();

            var vectorNode = rootNode;

            foreach (var vectorElementNode in vectorNode.ChildNodes)
            {
                if (vectorElementNode.ToString() == "biversorTerm0")
                {
                    //Scalar term
                    var value = double.Parse(vectorElementNode.ChildNodes[0].FindTokenAndGetText());

                    biversor.AddTerm(1, 1, value);
                }
                else if (vectorElementNode.ToString() == "biversorTerm2")
                {
                    //Biversor term
                    var value = double.Parse(vectorElementNode.ChildNodes[0].FindTokenAndGetText());
                    var id1   = int.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());
                    var id2   = int.Parse(vectorElementNode.ChildNodes[2].FindTokenAndGetText());

                    if (id1 < 0 || id2 < 0)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    biversor.AddTerm(id1, id2, value);
                }
                else
                {
                    throw new SyntaxErrorException(parsingResults.ToString());
                }
            }

            return(biversor);
        }
        private static GaPoTNumVector GaPoTNumParseVector(IronyParsingResults parsingResults, ParseTreeNode rootNode)
        {
            if (rootNode.ToString() != "spVector")
            {
                throw new SyntaxErrorException(parsingResults.ToString());
            }

            var vector = new GaPoTNumVector();

            var vectorNode = rootNode;

            foreach (var vectorElementNode in vectorNode.ChildNodes)
            {
                if (vectorElementNode.ToString() == "spTerm")
                {
                    //Term Form
                    var value = double.Parse(vectorElementNode.ChildNodes[0].FindTokenAndGetText());
                    var id    = int.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());

                    if (id < 0)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    vector.AddTerm(id, value);
                }
                else if (vectorElementNode.ToString() == "spPolarPhasor")
                {
                    //Polar Phasor Form
                    var magnitude = double.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());
                    var phase     = double.Parse(vectorElementNode.ChildNodes[2].FindTokenAndGetText());
                    var id1       = int.Parse(vectorElementNode.ChildNodes[3].FindTokenAndGetText());
                    var id2       = int.Parse(vectorElementNode.ChildNodes[4].FindTokenAndGetText());

                    if (id1 < 0 || id2 != id1 + 1)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    //Convert phase from degrees to radians
                    phase = phase.DegreesToRadians();

                    vector.AddPolarPhasor(id1, magnitude, phase);
                }
                else if (vectorElementNode.ToString() == "spRectPhasor")
                {
                    //Rectangular Phasor Form
                    var xValue = double.Parse(vectorElementNode.ChildNodes[1].FindTokenAndGetText());
                    var yValue = double.Parse(vectorElementNode.ChildNodes[2].FindTokenAndGetText());
                    var id1    = int.Parse(vectorElementNode.ChildNodes[3].FindTokenAndGetText());
                    var id2    = int.Parse(vectorElementNode.ChildNodes[4].FindTokenAndGetText());

                    if (id1 < 0 || id2 != id1 + 1)
                    {
                        throw new SyntaxErrorException(parsingResults.ToString());
                    }

                    vector.AddRectPhasor(id1, xValue, yValue);
                }
                else
                {
                    throw new SyntaxErrorException(parsingResults.ToString());
                }
            }

            return(vector);
        }