This project has moved and is read-only. For the latest updates, please go here.

Looking for if/then/else statement examples

Sep 18, 2014 at 5:05 AM
Edited Sep 18, 2014 at 5:05 AM
Could you please provide few examples of switch, if/else if/else statements because I'm having challenge in making it work.

Thanks,
Salman Zulfiqar
Sep 23, 2014 at 10:07 AM
Please post some sample code you are having difficulty with.

For one - you don't have to put a break between cases in switch, I think. LINQ Expressions automatically adds a break after each statement/code block in a switch case. I don't know if I have fixed that issue yet (by trying to remove any breaks), but this is a known issue.

Also, return is not currently supported at all.
Sep 25, 2014 at 7:29 PM
I also have the same question.

I can evaluate basic math without issue
 var expRowMath = new CompiledExpression()
                {
                    StringToParse = "(lost/gain)",
                    TypeRegistry = registry
                };

                var x =  expRowMath.Eval();

or

 var expRowMath = new CompiledExpression()
                {
                    StringToParse = "total > 1000",
                    TypeRegistry = registry
                };

var x expRowMath.Eval();
but when i try and evaluate the following:
 var expRowMath = new CompiledExpression()
                {
                    StringToParse = "if(total> 1000) { self; } else { (lost/gain); }",
                    TypeRegistry = registry
                };

                var x =  expRowMath.Eval();
I get a null exception object not set. All of the variables are on the TypeRegister correctly and i can evaluate each part of the statement without issue, just putting the entire if else statement together causes the issue.
Sep 25, 2014 at 11:32 PM
Here is the code I tried and got Null reference exception. Below is the complete exception stack.

Code Snippet:
            var exp = "if (x==20) x=x+20; else x= x-20;";
            var x = 20;
            ExpressionEvaluator.CompiledExpression ce = new CompiledExpression(exp);
            ExpressionEvaluator.TypeRegistry reg = new TypeRegistry();
            reg.RegisterSymbol("x", x);
            ce.TypeRegistry = reg;
            ce.Eval();
Exception Stack:

System.NullReferenceException: Object reference not set to an instance of an object.
at ExpressionEvaluator.Parser.ExpressionParseException..ctor(String message, ITokenStream tokenStream)
at ExpressionEvaluator.Parser.ExprEvalParser.ReportError(RecognitionException e)
at ExpressionEvaluator.Parser.ExprEvalParser.expression()
at ExpressionEvaluator.Parser.AntlrParser.Parse(Expression scope, Boolean isCall)
at ExpressionEvaluator.ExpressionCompiler.BuildTree(Expression scopeParam, Boolean isCall)
at ExpressionEvaluator.CompiledExpression.Compile()
at ExpressionEvaluator.CompiledExpression.Eval()
Sep 29, 2014 at 7:49 PM
Edited Sep 30, 2014 at 4:36 PM
I have had no luck with even basic combinations of an if statement. I get the same response with my own example as Sal.

I have downloaded the project and am debugging that way. When doing the following:
expression = "if(total> 1000) { self; } else { (lost/gain); }";
var expRowMath = new CompiledExpression();
                expRowMath.StringToParse = Expression;
                expRowMath.RegisterType("List", this); //need to register itself

                foreach (var d in List)
                {
                    expRowMath.RegisterType(d.Key, d.Value);
                }
                expRowMath.Compile(); //throws
From what i can, it figures out that it is an IF token, but since isScope is false it never en-queue the token since it can not identify it.

This is thrown in the parser.cs on line 453, then throws the exception up to 686
Exception Stack
An unhandled exception of type 'System.Exception' occurred in ExpressionEvaluator.dll

Additional information: Parser error at position 2: Unknown type or identifier 'if'
Sep 30, 2014 at 7:27 PM
Edited Sep 30, 2014 at 8:07 PM
I was getting the master branch when getting the Antrl3 branch i get the following error

"Error parsing token 'if'"

This is when the enum type is CompiledExpressionType.Expression;

When i change the code to be the following:
 
expression = "if(total> 1000) { List.ExpressionResult = self; } else { List.ExpressionResult = (lost/gain); }";
var register = new TypeRegistry();
                var expRowMath = new CompiledExpression();

                expRowMath.StringToParse = Expression;
                register.Add("List", this); //need to register itself

                //should count vars in formula and compare to vars in 
                //list to confirm they match otherwise throw error
                foreach (var d in List)
                {
                    register.Add(d.Key, d.Value);
                }
                expRowMath.TypeRegistry = register;
                expRowMath.ExpressionType = CompiledExpressionType.Statement;
                expRowMath.Compile();
                expRowMath.Eval();

                return this.ExpressionResult;
It works!!!! do all expressions with logic need to be set up as a statement?

Please advise,
-Stephen
Oct 3, 2014 at 12:55 AM
Glad it worked for you. I think that "if" and other code blocks are considered as statements and not simple expressions. Expressions usually have a value, and do not end in a semicolon. I will try to come up with a better answer for you based on how the code actually works.