Пример #1
0
        public void Exercise4()
        {
            //Dim a As String = "Hello"
            //Dim b As Integer = 6
            var Dim          = new StartsWithWord("Dim");
            var whitespace   = new Many(new StartsWithAnyChar(new char[] { ' ', '\t' })).Apply(ListToString);
            var As           = new StartsWithWord("As");
            var varString    = new StartsWithWord("String");
            var varInteger   = new StartsWithWord("Integer");
            var equals       = new StartsWithChar('=');
            var doubleQuotes = new StartsWithChar('"');
            var anyChar      = new StartsWith(c => c != '"');
            var identifier   = (new Many(new StartsWith(c => char.IsLetterOrDigit(c)))).Apply(ListToString);
            var stringLit    = (doubleQuotes + (new Many(anyChar)) + doubleQuotes).Apply(ListToString);
            var isDigit      = new StartsWithAnyChar("0123456789".ToCharArray());
            var isInteger    = new Many(isDigit).Apply(ListToString);

            var varType      = varString | varInteger;
            var defaultValue = stringLit | isInteger;
            var declaration  = (Dim + whitespace + identifier + whitespace + As + whitespace + varType + whitespace + equals + whitespace + defaultValue).Apply(a => { var l = (List <object>)a; return(new Declaration((string)l[2], (string)l[6], (string)l[10])); });

            var parseResult = declaration.Parse(@"Dim a As String = ""Hello""");

            Assert.AreEqual(true, parseResult.Success);

            parseResult = declaration.Parse(@"Dim b As Integer = 6");
            Assert.AreEqual(true, parseResult.Success);
        }
Пример #2
0
        public void Exercise2()
        {
            var isOut        = new StartsWithWord("Out");
            var isPut        = new StartsWithWord("put");
            var isExactlyOut = isOut + new Not(isPut);

            Do_Parser_Test(isOut, "Out", true, "Out", "");
            Do_Parser_Test(isOut, "Output", true, "Out", "put");

            Do_Parser_Test(isExactlyOut, "Out", true, new List <object>()
            {
                "Out"
            }, "");
            Do_Parser_Test(isExactlyOut, "Output", false, null, "Output");
        }
Пример #3
0
        public void Exercise1()
        {
            var print        = new StartsWithWord("Print");
            var whitespace   = new StartsWithAnyChar(new char[] { ' ', '\t' });
            var openBracket  = new StartsWithChar('(');
            var closeBracket = new StartsWithChar(')');
            var doubleQuotes = new StartsWithChar('"');
            var anyChar      = new StartsWith(c => c != '"');
            var stringLit    = doubleQuotes + (new Many(anyChar)) + doubleQuotes;
            var semiColon    = new StartsWithChar(';');
            var command      = print + (new Many(whitespace)) + openBracket + stringLit + closeBracket + semiColon;

            var parseResult = command.Parse(@"Print     (""Hello World"");");

            Assert.AreEqual(true, parseResult.Success);
        }
Пример #4
0
        public void Exercise3()
        {
            var print        = new StartsWithWord("Print");
            var whitespace   = new Many(new StartsWithAnyChar(new char[] { ' ', '\t' })).Apply(ListToString);
            var openBracket  = new StartsWithChar('(');
            var closeBracket = new StartsWithChar(')');
            var doubleQuotes = new StartsWithChar('"');
            var anyChar      = new StartsWith(c => c != '"');
            var stringLit    = (doubleQuotes + (new Many(anyChar)) + doubleQuotes).Apply(ListToString);
            var semiColon    = new StartsWithChar(';');
            var command      = print + whitespace + openBracket + stringLit + closeBracket + semiColon;

            command.Apply6((a, b, c, d, e, f) => new PrintCommand((string)d));
            var parseResult = command.Parse(@"Print     (""Hello World"");");

            Assert.AreEqual(true, parseResult.Success);
            Assert.IsInstanceOfType(parseResult.Parsed, typeof(PrintCommand));
        }
Пример #5
0
        public void Exercise5()
        {
            var pWhitespace = new Many(new StartsWithAnyChar(new char[] { ' ', '\t' })).Apply(ListToString);
            var pDigit      = new StartsWithAnyChar("0123456789".ToCharArray());
            var pInteger    = new Many(pDigit).Apply(ListToInteger);
            var pNewLine    = new StartsWithWord(System.Environment.NewLine).ApplyString(a => new BlankLine());
            var pMoveDown   = (new StartsWithWord("Move Down") + pWhitespace + pInteger).Apply3((a, b, c) => new MoveDown((int)c));
            var pMoveUp     = (new StartsWithWord("Move Up") + pWhitespace + pInteger).Apply3((a, b, c) => new MoveUp((int)c));;
            var pMoveLeft   = (new StartsWithWord("Move Left") + pWhitespace + pInteger).Apply3((a, b, c) => new MoveLeft((int)c));;
            var pMoveRight  = (new StartsWithWord("Move Right") + pWhitespace + pInteger).Apply3((a, b, c) => new MoveRight((int)c));;
            var pCommand    = pMoveDown | pMoveUp | pMoveLeft | pMoveRight | pNewLine;
            var pProgram    = new Many((pCommand + new OneOrMany(pNewLine)).Apply2((a, b) => a));

            var parseResult = pProgram.Parse(@"

Move Down 123
Move Left 123
Move Up 123
Move Right 123
");
            var theProgram  = ((List <object>)parseResult.Parsed).Cast <Command>();
            var output      = new StringBuilder();

            foreach (var cmd in theProgram)
            {
                if (cmd is MoveDown)
                {
                    var command = (MoveDown)cmd;
                    output.AppendLine($"newX = X;");
                    output.AppendLine($"newY = Y + {command.Distance};");
                    output.AppendLine($"g.DrawLine(Pens.Blue, X, Y, newX, newY);");
                    output.AppendLine($"X = newX;");
                    output.AppendLine($"Y = newY;");
                }

                if (cmd is MoveUp)
                {
                    var command = (MoveUp)cmd;
                    output.AppendLine($"newX = X;");
                    output.AppendLine($"newY = Y - {command.Distance};");
                    output.AppendLine($"g.DrawLine(Pens.Green, X, Y, newX, newY);");
                    output.AppendLine($"X = newX;");
                    output.AppendLine($"Y = newY;");
                }

                if (cmd is MoveLeft)
                {
                    var command = (MoveLeft)cmd;
                    output.AppendLine($"newX = X - {command.Distance};");
                    output.AppendLine($"newY = Y;");
                    output.AppendLine($"g.DrawLine(Pens.Red, X, Y, newX, newY);");
                    output.AppendLine($"X = newX;");
                    output.AppendLine($"Y = newY;");
                }

                if (cmd is MoveRight)
                {
                    var command = (MoveRight)cmd;
                    output.AppendLine($"newX = X + {command.Distance};");
                    output.AppendLine($"newY = Y;");
                    output.AppendLine($"g.DrawLine(Pens.Black, X, Y, newX, newY);");
                    output.AppendLine($"X = newX;");
                    output.AppendLine($"Y = newY;");
                }
            }

            var filename = @"C:\CodeDojo\York\Parsers\David\Generated\Form1.cs";
            var template = System.IO.File.ReadAllText(filename);
            var markedUp = template.Replace("//ADD_CODE_HERE", output.ToString());

            System.IO.File.WriteAllText(filename, markedUp);
        }