Exemplo n.º 1
0
        public void Rewriter_TwoTrueRules_LastRuleResult()
        {
            var tree = MockRepository.GenerateStub<ISyntacticNode>();
            tree.Stub(t => t.Nodes).Return(Enumerable.Empty<ISyntacticNode>());
            tree.Stub(t => t.Rewrite(Arg<IEnumerable<ISyntacticNode>>.Is.Anything))
                .Return(tree);

            var tree1 = MockRepository.GenerateStub<ISyntacticNode>();
            tree1.Stub(t => t.Nodes).Return(Enumerable.Empty<ISyntacticNode>());
            tree1.Stub(t => t.Rewrite(Arg<IEnumerable<ISyntacticNode>>.Is.Anything))
                .Return(tree1);

            var rule1 = MockRepository.GenerateStub<ISyntaxRewriter>();
            rule1.Stub(r => r.Filter(tree)).Return(true);
            rule1.Stub(r => r.Visit(Arg<ISyntacticNode>.Is.Anything,
                Arg<IEnumerable<ISyntacticNode>>.Is.Anything)).Return(tree1);

            var tree2 = MockRepository.GenerateStub<ISyntacticNode>();

            var rule2 = MockRepository.GenerateStub<ISyntaxRewriter>();
            rule2.Stub(r => r.Filter(tree1)).Return(true);
            rule2.Stub(r => r.Visit(Arg<ISyntacticNode>.Is.Anything,
                Arg<IEnumerable<ISyntacticNode>>.Is.Anything)).Return(tree2);

            var translator = new Translator(new[] { rule1, rule2 });
            Assert.That(translator.Rewrite(tree), Is.SameAs(tree2));
        }
Exemplo n.º 2
0
 public void CheckResult_BlockNode_Throw()
 {
     var node = new BlockSyntacticNode(
         MockRepository.GenerateStub<IBlock>(),
         Enumerable.Empty<ISyntacticNode>());
     var translator = new Translator(Enumerable.Empty<ISyntaxRewriter>());
     translator.CheckResult(node);
 }
Exemplo n.º 3
0
 public void CheckResult_TermNode_DoesNotThrow()
 {
     var node = new TermSyntacticNode(
         MockRepository.GenerateStub<ITerm>(),
         Enumerable.Empty<ISyntacticNode>());
     var translator = new Translator(Enumerable.Empty<ISyntaxRewriter>());
     Assert.DoesNotThrow(() => translator.CheckResult(node));
 }
Exemplo n.º 4
0
 public void Rules_SomeList_Same()
 {
     var rules = new[]
     {
         MockRepository.GenerateStub<ISyntaxRewriter>(),
         MockRepository.GenerateStub<ISyntaxRewriter>()
     };
     var translator = new Translator(rules);
     Assert.That(translator.Rules, Is.SameAs(rules));
 }
Exemplo n.º 5
0
 public void Rewrite_SpecialExpression2_TargetNodeCount()
 {
     var lexical = new LexicalAnalyzer(AnalyzerFacade.LexicalRules);
     var syntactic = new SyntacticAnalyzer(AnalyzerFacade.SyntacticRules);
     var translator = new Translator(AnalyzerFacade.TranslateRules);
     const string text = "(((-1+1+3+pi)))";
     var tokens = lexical.Parse(text);
     var tree = syntactic.Parse(tokens, BlockOf<ExpressionBlock>());
     var root = translator.Rewrite(tree);
     Assert.That(root.Nodes.Count(), Is.EqualTo(2));
 }
Exemplo n.º 6
0
 public void Rewrite_Expressiion_DoesNotThrow(string text)
 {
     var lexical = new LexicalAnalyzer(AnalyzerFacade.LexicalRules);
     var syntactic = new SyntacticAnalyzer(AnalyzerFacade.SyntacticRules);
     var translator = new Translator(AnalyzerFacade.TranslateRules);
     Assert.DoesNotThrow(
         () =>
         {
             var tokens = lexical.Parse(text);
             var tree = syntactic.Parse(tokens, BlockOf<ExpressionBlock>());
             translator.Rewrite(tree);
         });
 }
        public void Translate_SpecialExpression2_TargetNodeCount()
        {
            var lexical = new LexicalAnalyzer(StandardProcessorBuilder.LexicalRules);
            var syntactic = new SyntacticAnalyzer(StandardProcessorBuilder.SyntacticRules);
            var translator = new Translator(StandardProcessorBuilder.TranslateRules);
            const string text = "(((-1+1+3+pi)))";

            var tokens = lexical.Parse(text);
            var tree = syntactic.Parse(tokens, StandardProcessorBuilder.SyntacticTarget);
            var result = translator.Translate(tree);

            Assert.That(result.Nodes.Count(), Is.EqualTo(2));
        }
 public void TranslateAndCheck_Expressiion_DoesNotThrow(string text)
 {
     var lexical = new LexicalAnalyzer(StandardProcessorBuilder.LexicalRules);
     var syntactic = new SyntacticAnalyzer(StandardProcessorBuilder.SyntacticRules);
     var translator = new Translator(StandardProcessorBuilder.TranslateRules);
     Assert.DoesNotThrow(
         () =>
         {
             var tokens = lexical.Parse(text);
             var tree = syntactic.Parse(tokens, StandardProcessorBuilder.SyntacticTarget);
             var result = translator.Translate(tree);
             translator.CheckResult(result);
         });
 }
        public void Resolve_UnresolverIdentifier_Throw(string text)
        {
            var lexical = new LexicalAnalyzer(StandardProcessorBuilder.LexicalRules);
            var syntactic = new SyntacticAnalyzer(StandardProcessorBuilder.SyntacticRules);
            var translator = new Translator(StandardProcessorBuilder.TranslateRules);
            var linkedLibraryFactory = new StaticLinkedLibraryFactory(typeof(StandardProcessorBuilder.LinkedMath));
            var linker = new Linker(StandardProcessorBuilder.LinkerRules, linkedLibraryFactory.CreateLinkedLibrary());

            var tokens = lexical.Parse(text);
            var tree = syntactic.Parse(tokens, StandardProcessorBuilder.SyntacticTarget);
            var translatedTree = translator.Translate(tree);
            translator.CheckResult(translatedTree);
            linker.Resolve(translatedTree);

        }
        public void Translate_SumOfProduct_SumTerm()
        {
            var lexical = new LexicalAnalyzer(StandardProcessorBuilder.LexicalRules);
            var syntactic = new SyntacticAnalyzer(StandardProcessorBuilder.SyntacticRules);
            var translator = new Translator(StandardProcessorBuilder.TranslateRules);
            const string text = "2*3+3*4.7";

            var tokens = lexical.Parse(text);
            var tree = syntactic.Parse(tokens, StandardProcessorBuilder.SyntacticTarget);
            var result = translator.Translate(tree);

            var termNode = (TermSyntacticNode) result;
            Assert.That(termNode.Term, Is.TypeOf<BinaryOperatorTerm>());
            var operatorTerm = (BinaryOperatorTerm) termNode.Term;
            Assert.That(operatorTerm.Operator, Is.TypeOf<PlusOperator>());
        }
        public void Compile_Expressiion_RightResult(string text, double value)
        {
            var linkedLibraryFactory = new StaticLinkedLibraryFactory(typeof(StandardProcessorBuilder.LinkedMath));

            var lexical = new LexicalAnalyzer(StandardProcessorBuilder.LexicalRules);
            var syntactic = new SyntacticAnalyzer(StandardProcessorBuilder.SyntacticRules);
            var translator = new Translator(StandardProcessorBuilder.TranslateRules);
            var linker = new Linker(StandardProcessorBuilder.LinkerRules, 
                linkedLibraryFactory.CreateLinkedLibrary());
            var tokens = lexical.Parse(text);
            var tree = syntactic.Parse(tokens, StandardProcessorBuilder.SyntacticTarget);
            var translatedTree = translator.Translate(tree);
            translator.CheckResult(translatedTree);
            var linkedTree = linker.Resolve(translatedTree);
            linker.CheckResult(linkedTree);
            var compilator = new Compiler();
            var lambda = compilator.CreateExpression(linkedTree);
            var result = lambda.Compile()();
            Assert.That(result, Is.EqualTo(value).Within(1e-1));
        }
Exemplo n.º 12
0
 public void Rules_Empty_Empty()
 {
     var rules = Enumerable.Empty<ISyntaxRewriter>();
     var translator = new Translator(rules);
     Assert.That(translator.Rules, Is.SameAs(rules));
 }
Exemplo n.º 13
0
 public void Rules_Null_Null()
 {
     var translator = new Translator(null);
     Assert.That(translator.Rules, Is.Null);
 }