Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation FOR statement #10

Open
andmattia opened this issue Jun 14, 2019 · 0 comments
Open

Implementation FOR statement #10

andmattia opened this issue Jun 14, 2019 · 0 comments

Comments

@andmattia
Copy link

I try to implement the Loop Expression for "FOR statement". Below the rule and the code
using System; using NRules.RuleSharp.IntegrationTests.TestAssets; rule ""Metadata Rule 1"" description ""Rule with description and priority"" priority 10 when var fact = TestFact1(); then var tfi =Convert.ToInt32(0); for(var a=0;a<5;a=a+1) tfi = tfi+1; ";
I change the code in ExpressionParser.cs

 public override Expression VisitForStatement(ForStatementContext context)
        {
            var loopVar = Expression.Parameter(typeof(string), "loopVar");
            var initValue = Visit(context.for_initializer());
            var condition = Visit(context.expression());
            var increment = Visit(context.for_iterator());

            var loopContent = Visit(context.embedded_statement());
            var initAssign = Expression.Assign(loopVar, initValue);

            var breakLabel = Expression.Label("LoopBreak");

            var loop = Expression.Block(new[] { loopVar },
                initAssign,
                Expression.Loop(
                    Expression.IfThenElse(
                        condition,
                        Expression.Block(
                            loopContent,
                            increment
                        ),
                        Expression.Break(breakLabel)
                    ),
                    breakLabel)
            );
            return loop;
        }

I try to debug using unit test class and the first step work well (initValue return 0) but when I call the second step (var condition = Visit(context.expression());) I catch an exception
image
The complete stack trace is
in NRules.RuleSharp.PrimaryExpressionBuilder.GetExpression() in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\PrimaryExpressionBuilder.cs:riga 29 in NRules.RuleSharp.ExpressionParser.VisitPrimary_expression(Primary_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 249 in NRules.RuleSharp.Parser.RuleSharpParser.Primary_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2661 in NRules.RuleSharp.ExpressionParser.VisitUnary_expression(Unary_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 262 in NRules.RuleSharp.Parser.RuleSharpParser.Unary_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2503 in NRules.RuleSharp.ExpressionParser.VisitMultiplicative_expression(Multiplicative_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 490 in NRules.RuleSharp.Parser.RuleSharpParser.Multiplicative_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2419 in NRules.RuleSharp.ExpressionParser.VisitAdditive_expression(Additive_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 517 in NRules.RuleSharp.Parser.RuleSharpParser.Additive_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2342 in Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(IRuleNode node) in NRules.RuleSharp.Parser.RuleSharpParserBaseVisitor`1.VisitShift_expression(Shift_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParserBaseVisitor.cs:riga 897 in NRules.RuleSharp.Parser.RuleSharpParser.Shift_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2262 in NRules.RuleSharp.ExpressionParser.VisitRelational_expression(Relational_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 381 in NRules.RuleSharp.Parser.RuleSharpParser.Relational_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2159 in NRules.RuleSharp.ExpressionParser.VisitEquality_expression(Equality_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 467 in NRules.RuleSharp.Parser.RuleSharpParser.Equality_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 2065 in NRules.RuleSharp.ExpressionParser.VisitAnd_expression(And_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 456 in NRules.RuleSharp.Parser.RuleSharpParser.And_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1992 in NRules.RuleSharp.ExpressionParser.VisitExclusive_or_expression(Exclusive_or_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 445 in NRules.RuleSharp.Parser.RuleSharpParser.Exclusive_or_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1929 in NRules.RuleSharp.ExpressionParser.VisitInclusive_or_expression(Inclusive_or_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 434 in NRules.RuleSharp.Parser.RuleSharpParser.Inclusive_or_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1866 in NRules.RuleSharp.ExpressionParser.VisitConditional_and_expression(Conditional_and_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 423 in NRules.RuleSharp.Parser.RuleSharpParser.Conditional_and_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1803 in NRules.RuleSharp.ExpressionParser.VisitConditional_or_expression(Conditional_or_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 412 in NRules.RuleSharp.Parser.RuleSharpParser.Conditional_or_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1736 in NRules.RuleSharp.ExpressionParser.VisitNull_coalescing_expression(Null_coalescing_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 370 in NRules.RuleSharp.Parser.RuleSharpParser.Null_coalescing_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1673 in NRules.RuleSharp.ExpressionParser.VisitConditional_expression(Conditional_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 358 in NRules.RuleSharp.Parser.RuleSharpParser.Conditional_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1612 in Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(IRuleNode node) in NRules.RuleSharp.Parser.RuleSharpParserBaseVisitor`1.VisitNon_assignment_expression(Non_assignment_expressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParserBaseVisitor.cs:riga 765 in NRules.RuleSharp.Parser.RuleSharpParser.Non_assignment_expressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1375 in Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(IRuleNode node) in NRules.RuleSharp.Parser.RuleSharpParserBaseVisitor`1.VisitExpression(ExpressionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParserBaseVisitor.cs:riga 754 in NRules.RuleSharp.Parser.RuleSharpParser.ExpressionContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 1311 in NRules.RuleSharp.ExpressionParser.VisitForStatement(ForStatementContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 322 in NRules.RuleSharp.Parser.RuleSharpParser.ForStatementContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 6685 in Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(IRuleNode node) in NRules.RuleSharp.Parser.RuleSharpParserBaseVisitor`1.VisitEmbedded_statement(Embedded_statementContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParserBaseVisitor.cs:riga 1370 in NRules.RuleSharp.Parser.RuleSharpParser.Embedded_statementContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 6415 in Antlr4.Runtime.Tree.AbstractParseTreeVisitor`1.VisitChildren(IRuleNode node) in NRules.RuleSharp.Parser.RuleSharpParserBaseVisitor`1.VisitEmbeddedStatement(EmbeddedStatementContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParserBaseVisitor.cs:riga 311 in NRules.RuleSharp.ExpressionParser.VisitEmbeddedStatement(EmbeddedStatementContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 24 in NRules.RuleSharp.Parser.RuleSharpParser.EmbeddedStatementContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 6216 in NRules.RuleSharp.ExpressionParser.VisitStatement_list(Statement_listContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\ExpressionParser.cs:riga 57 in NRules.RuleSharp.Parser.RuleSharpParser.Statement_listContext.Accept[TResult](IParseTreeVisitor`1 visitor) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 8198 in NRules.RuleSharp.RuleParserListener.ParseActionExpression(Rule_actionContext context, IEnumerable`1 parameters) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\RuleParserListener.cs:riga 151 in NRules.RuleSharp.RuleParserListener.EnterRule_action(Rule_actionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\RuleParserListener.cs:riga 141 in NRules.RuleSharp.Parser.RuleSharpParser.Rule_actionContext.EnterRule(IParseTreeListener listener) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 18631 in Antlr4.Runtime.Tree.ParseTreeWalker.Walk(IParseTreeListener listener, IParseTree t) in NRules.RuleSharp.RuleSharpParserListener.EnterRule_definition(Rule_definitionContext context) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\RuleSharpParserListener.cs:riga 42 in NRules.RuleSharp.Parser.RuleSharpParser.Rule_definitionContext.EnterRule(IParseTreeListener listener) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\obj\Debug\net45\RuleSharpParser.cs:riga 18691 in Antlr4.Runtime.Tree.ParseTreeWalker.Walk(IParseTreeListener listener, IParseTree t) in NRules.RuleSharp.RuleRepository.Load(AntlrInputStream input) in C:\Repos\andmattia\NRules.Language\src\NRules.RuleSharp\NRules.RuleSharp\RuleRepository.cs:riga 148

I don't know if is not the correct way to build that rule but I adopt similar step used for IF. Any idea? Or if exist some document that can I use to understand how to identify / fix this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant