Well, solved both problems. Hope this can help others.
Problem 1
As far as I understand from this discussion thread of irony , true and false constants should be considered as predefined global variables, and not be implemented directly as part of the language. As such, I define them as global when creating the ScriptInterpreter .
It should be remembered that by doing this in this way they can be changed using a script, since they are not constants, but simply global variables. This may be the best way to do this, but it will do now:
var interpreter = new Irony.Interpreter.ScriptInterpreter( new ExpressionEvaluatorGrammar()); interpreter.Globals["true"] = true; interpreter.Globals["false"] = false; interpreter.Evaluate(parsedSample);
Problem 2
First, the <> operator must appear before the < and > operators in the binary operator rule:
BinOp.Rule = ToTerm("+") | "-" | "*" | "/" | "**" | "<>" | "==" | "<=" | ">=" | "<" | ">" | "!="; // added comparison operators
Then I created a custom implementation of the LanguageRuntime class, which implements the necessary operators.
public class CustomLanguageRuntime : LanguageRuntime { public CustomLanguageRuntime(LanguageData data) : base(data) { } public override void InitOperatorImplementations() { base.InitOperatorImplementations(); AddImplementation("<>", typeof(bool), (x, y) => (bool)x != (bool)y); AddImplementation("!=", typeof(bool), (x, y) => (bool)x != (bool)y); AddImplementation("==", typeof(bool), (x, y) => (bool)x == (bool)y); } }
In ExpressionEvaluatorGrammar, override the CreateRuntime method to return an instance of CustomLanguageRuntime :
public override LanguageRuntime CreateRuntime(LanguageData data) { return new CustomLanguageRuntime(data); }