Evaluating an expression with identifiers

Mar 13, 2010 at 8:02 AM
Edited Mar 14, 2010 at 7:44 AM

I've created a simple BNF for testing purpose:

Expr.Rule = Term | BinExpr;

Term.Rule = identifier | number | Expr;

BinExpr.Rule = Expr + BinOp + Expr;

BinOp.Rule = ToTerm(">") | "<";

Program.Rule = MakePlusRule(Program, NewLine, Expr);

I have successfully parsed "WIDTH > HEIGHT", but how do I evaluate the Ast (with the provided tree implementations for operators etc.) ?

I always get "Unknow vairable WIDTH" How do I push a symbol value name WIDTH with value to evaluation context?

context.SetValue(context.Symbols.TextToSymbol("WIDTH"), 10); <--- this doesn't work,

I'm getting error Is there something else one should do to be able to push values to context?

BTW. Awesome project :)

Coordinator
Mar 13, 2010 at 4:39 PM

you're right, something is broken here, I'll look into this and will try to fix it in the next drop

Mar 14, 2010 at 7:43 AM

Ok sounds great.

So this is the way to go then:

context.SetValue(context.Symbols.TextToSymbol("WIDTH"), 10);

Coordinator
Mar 15, 2010 at 5:21 AM

Fixed in latest source drop. As an example, ExprEvaluatorGrammar adds "pi" variable to globals:

 

 

    public override string RunSample(ParseTree parsedSample) {
      var interpreter = new Irony.Interpreter.ScriptInterpreter(this);
      interpreter.Globals["pi"] = Math.PI;
      interpreter.Evaluate(parsedSample);
      return interpreter.GetOutput(); 
    }

 

 

    public override string RunSample(ParseTree parsedSample) {

      var interpreter = new Irony.Interpreter.ScriptInterpreter(this);

      interpreter.Globals["pi"] = Math.PI;

      interpreter.Evaluate(parsedSample);

      return interpreter.GetOutput(); 

    }