public void ShouldReUseTermsInRewrite()
        {
            // Arrange
            const string testStatements = "root: t<a,b>(1,2); t<a,b>(1,2) => t<b,c>(1,2); t<b,c>(1,2) => t<c,d>(1,2);";
            var          termDatabase   = TestUtilities.LoadStatements(testStatements);

            termDatabase.ExecuteRewriteRules();
            var metrics = termDatabase.GetDatabaseMetrics();

            // Act
            for (int i = 0; i < 100; i++)
            {
                var parser      = new TrlParser();
                var parseResult = parser.ParseToAst(testStatements);
                if (!parseResult.Succeed)
                {
                    throw new Exception(parseResult.Errors.First());
                }
                termDatabase.Writer.StoreStatements(parseResult.Statements);
                termDatabase.ExecuteRewriteRules();
            }

            // Assert
            var result = termDatabase.Reader.ReadStatementsForLabel("root");

            Assert.True(StringComparer.InvariantCulture.Equals("root: t<c,d>(1,2);", result.ToSourceCode()));
            var metricsAssert = termDatabase.GetDatabaseMetrics();

            Assert.Equal(metrics.LabelCount, metricsAssert.LabelCount);
            Assert.Equal(metrics.RewriteRuleCount, metricsAssert.RewriteRuleCount);
            Assert.Equal(metrics.StringCount, metricsAssert.StringCount);
            Assert.Equal(metrics.TermCount, metricsAssert.TermCount);
        }
        public static TermDatabase LoadStatements(string statements)
        {
            var termDatabase = new TermDatabase();
            var parser       = new TrlParser();
            var parseResult  = parser.ParseToAst(statements);

            if (!parseResult.Succeed)
            {
                throw new Exception(parseResult.Errors.First());
            }
            termDatabase.Writer.StoreStatements(parseResult.Statements);
            return(termDatabase);
        }
Esempio n. 3
0
        static void Main()
        {
            var input = "0; 0 => inc(0);";

            // Parse input
            var parser      = new TrlParser();
            var parseResult = parser.ParseToAst(input);

            if (!parseResult.Succeed)
            {
                Console.WriteLine("Syntax error.");
                return;
            }

            // Execute substitutions
            var termDatabase = new TermDatabase();

            termDatabase.Writer.StoreStatements(parseResult.Statements);

            // Track changes
            termDatabase.Writer.SetTermReplacementObserver(replacement =>
            {
                var originalTerm = replacement.OriginalRootTerm.ToSourceCode(termDatabase);
                var newTerm      = replacement.NewRootTerm.ToSourceCode(termDatabase);
                Console.WriteLine($"{replacement.RewriteIteration}> Replaced {originalTerm} with {newTerm}");
            });

            // Execute the rewrite rules
            termDatabase.ExecuteRewriteRules(4);

            // Print output
            Console.WriteLine();
            Console.WriteLine("Output:");
            var output = termDatabase.Reader.ReadCurrentFrame();

            Console.WriteLine(output.ToSourceCode(true));
        }
 public TrlParserTests()
 {
     _parser = new TrlParser();
 }
 internal StringToObjectTranslator(NameAndTypeMappings nameAndTypeMappings)
 {
     _parser              = new TrlParser();
     _objectCache         = new Dictionary <(ulong, string), object>(EqualityComparer <ValueTuple <ulong, string> > .Default);
     _nameAndTypeMappings = nameAndTypeMappings;
 }
Esempio n. 6
0
 public TermDatabaseTests()
 {
     _termDatabase = new TermDatabase();
     _parser       = new TrlParser();
 }