Exemplo n.º 1
0
 public void WriteOperator(AstOperator a)
 {
     Write(a.Source);
     WriteGlobal(a.DocComment);
     WriteAttributes(a.Attributes);
     WriteModifiers(a.Modifiers, a.OptionalCondition);
     Write(a.ReturnType);
     Write((byte)a.Operator);
     WriteParameters(a.Parameters);
     Write(a.OptionalBody);
 }
        public void SecondPassSimpleTest()
        {
            var compiler = new Compiler();
            var t2       = new AstOperator("/", new AstOperator("-", new AstOperator("+", new AstOperator("*", new AstOperand("imm", 6), new AstOperand("arg", 0)), new AstOperator("*", new AstOperand("imm", 5), new AstOperand("arg", 1))), new AstOperator("*", new AstOperand("imm", 3), new AstOperand("arg", 2))), new AstOperand("imm", 8));
            var p2       = compiler.SecondPass(compiler.FirstPass("[ x y z ] ( 2*3*x + 5*y - 3*z ) / (1 + 3 + 2*2)"));

            Simulator.PolishNotation = "";
            Simulator.NodesToPolishNotation(t2);
            var pNt2 = Simulator.PolishNotation;

            Simulator.PolishNotation = "";
            Simulator.NodesToPolishNotation(p2);
            var pNp2 = Simulator.PolishNotation;

            pNp2.Should().Be(pNt2);
        }
        public void FirstPassTest()
        {
            var compiler = new Compiler();
            var prog     = "[ x y z ] ( 2*3*x + 5*y - 3*z ) / (1 + 3 + 2*2)";
            var t1       = new AstOperator("/", new AstOperator("-", new AstOperator("+", new AstOperator("*", new AstOperator("*", new AstOperand("imm", 2), new AstOperand("imm", 3)), new AstOperand("arg", 0)), new AstOperator("*", new AstOperand("imm", 5), new AstOperand("arg", 1))), new AstOperator("*", new AstOperand("imm", 3), new AstOperand("arg", 2))), new AstOperator("+", new AstOperator("+", new AstOperand("imm", 1), new AstOperand("imm", 3)), new AstOperator("*", new AstOperand("imm", 2), new AstOperand("imm", 2))));
            var p1       = compiler.FirstPass(prog);

            Simulator.PolishNotation = "";
            Simulator.NodesToPolishNotation(t1);
            var pNt1 = Simulator.PolishNotation;

            Simulator.PolishNotation = "";
            Simulator.NodesToPolishNotation(p1);
            var pNp1 = Simulator.PolishNotation;

            pNp1.Should().Be(pNt1);
        }
            public List <Token> BuildTokenStream(string source)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    char c = source[i];
                    if (c == '(')
                    {
                        SetTokenType(TokenType.BracketOpen);
                    }
                    else
                    if (c == ')')
                    {
                        SetTokenType(TokenType.BracketClose);
                    }
                    else

                    if (c == ' ' || c == '\t')
                    {
                        SetTokenType(TokenType.None);
                    }
                    else
                    if (char.IsNumber(c) && !symbolbusy)
                    {
                        SetTokenType(TokenType.Number);

                        if (numberbusy)
                        {
                            if (behinddecimalpoint > -1)
                            {
                                behinddecimalpoint++;
                                num += char.GetNumericValue(c) / Math.Pow(10, behinddecimalpoint);
                            }
                            else
                            {
                                num *= 10.0;
                                num += char.GetNumericValue(c);
                            }
                        }
                        else
                        {
                            num = char.GetNumericValue(c);
                        }
                        numberbusy = true;
                    }
                    else
                    {
                        if (c == '.')
                        {
                            SetTokenType(TokenType.Number);
                            behinddecimalpoint = 0;
                        }
                        else
                        {
                            if (char.IsLetter(c) || (char.IsNumber(c) && symbolbusy))
                            {
                                SetTokenType(TokenType.Symbol);
                                if (!symbolbusy)
                                {
                                    CurrentSymbol = "";
                                }
                                CurrentSymbol += c;
                                symbolbusy     = true;
                            }
                            else
                            {
                                string operators = "-+/*^";

                                if (operators.Contains(c))
                                {
                                    SetTokenType(TokenType.Operator);
                                    if (c == '+')
                                    {
                                        CurrentOperator = AstOperator.Add;
                                    }
                                    if (c == '-')
                                    {
                                        CurrentOperator = AstOperator.Sub;
                                    }
                                    if (c == '/')
                                    {
                                        CurrentOperator = AstOperator.Div;
                                    }
                                    if (c == '*')
                                    {
                                        CurrentOperator = AstOperator.Mult;
                                    }
                                    if (c == '^')
                                    {
                                        CurrentOperator = AstOperator.Pow;
                                    }
                                    SetTokenType(TokenType.None);
                                }
                            }
                        }
                    }
                }

                SetTokenType(TokenType.None);
                //  PrintTokens(Res);
                return(Res);
            }
 private AstOperator TokenToAst(AstOperator astOperator)
 {
     return(astOperator);
 }