Пример #1
0
        public void BuildIfStatement()
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState("if");

            Assert.That(grammer.if_keyword.Match(p), Is.True);

            p = new ParserState("if()");
            var rule = grammer.if_keyword + grammer.Delimiter("(") + grammer.Delimiter(")");
            Assert.That(rule.Match(p), Is.True);

            p = new ParserState("if (a == b)");
            Assert.That(grammer.if_condition.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            var if_block = grammer.if_condition + grammer.block;
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            if_block = grammer.if_condition + new RecursiveRule(() => grammer.block);
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            Assert.That(grammer.control_statement.Match(p), Is.True);
        }
Пример #2
0
        public void Block(string expression, bool expected)
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState(expression);

            Assert.That(grammer.block.Match(p), Is.EqualTo(expected));

            var printer = new CppStructuralOutputAsXml();
            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
Пример #3
0
        public Compiler(AndyStructuralGrammar grammar, ParserState parserState)
        {
            _grammar = grammar;

            var rules = grammar.GetRules<AndyStructuralGrammar>(throwOnMissing: false);
            rules = rules.Concat(grammar.GetRules<AndyBaseGrammar>(throwOnMissing: false));

            _codeGenerator = rules
                .Distinct()
                .ToDictionary(k => k, GetCodeGenerator);

            _root = parserState.GetRoot();
        }
Пример #4
0
        public void BuildElseStatement()
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState("else");

            Assert.That(grammer.else_keyword.Match(p), Is.True);

            p = new ParserState("else { b=10; }");
            Assert.That(grammer.else_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}");
            var if_block = grammer.if_condition + grammer.block + new OptRule(grammer.else_block);
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){a=20;}else{b=10;}");
            Assert.That(if_block.Match(p), Is.True);

            p = new ParserState("if(a == 10){ a=20; } else { b=10; }");
            Assert.That(grammer.if_statement.Match(p), Is.True);
        }
Пример #5
0
        public void ControlFlow(string source, uint result)
        {
            var grammar = new AndyStructuralGrammar();
            var parserState = new ParserState(source);

            var parseResult = grammar.block.Match(parserState);
            Assert.That(parseResult, Is.True);

            var compiler = new Andy.TinyOS.Compiler.Compiler(grammar, parserState);
            var program = compiler.Compile();

            var cpu = new Cpu();
            var r = Run(program, cpu);
            Assert.That(r.ExitCode, Is.EqualTo(result));

            var variable = cpu.Ram.GetStream(r.GlobalData.Pages.First());
            using (var reader = new BinaryReader(variable))
            {
                var value = reader.ReadUInt32();
                Assert.That(value, Is.EqualTo(result));
            }
        }
Пример #6
0
        public void SingleExpression(string source, uint result)
        {
            var grammar = new AndyStructuralGrammar();
            var parserState = new ParserState(source);

            var parseResult = grammar.expression.Match(parserState);
            Assert.That(parseResult, Is.True);

            var compiler = new Andy.TinyOS.Compiler.Compiler(grammar, parserState);
            var program = compiler.Compile();

            var r = Run(program, new Cpu());
            Assert.That(r.ExitCode, Is.EqualTo(result));
        }
Пример #7
0
        public void UnaryInteger(string integer, bool expected)
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState(integer);

            Assert.That(grammer.term.Match(p), Is.EqualTo(expected));

            var printer = new CppStructuralOutputAsXml();
            printer.Print(p.GetRoot());
            Console.WriteLine(printer.AsXml());
        }
Пример #8
0
        public void Integer(string integer, bool expected)
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState(integer);

            Assert.That(grammer.int_literal.Match(p), Is.EqualTo(expected));
        }
Пример #9
0
        public void BuildWhileStatement()
        {
            var grammer = new AndyStructuralGrammar();
            var p = new ParserState("while");

            Assert.That(grammer.while_keyword.Match(p), Is.True);

            p = new ParserState("while()");
            var rule = grammer.while_keyword + grammer.Delimiter("(") + grammer.Delimiter(")");
            Assert.That(rule.Match(p), Is.True);

            p = new ParserState("while (a == b)");
            Assert.That(grammer.while_condition.Match(p), Is.True);

            p = new ParserState("while(a == 10){a=20;}");
            var while_block = grammer.while_condition + grammer.block;
            Assert.That(while_block.Match(p), Is.True);

            p = new ParserState("while(a == 10){a=20;}");
            Assert.That(grammer.while_statement.Match(p), Is.True);

            p = new ParserState("while(a == 10){a=20;}");
            Assert.That(grammer.control_statement.Match(p), Is.True);
        }