Irony: AST node assessment tutorial?

I defined a simple grammar in Irony and created a nice compact AST.

fyFmr.png

Now I'm trying to figure out how to evaluate it. The problem is that I cannot find tutorials on how to do this.

I defined only 2 AST nodes:

class TagListNode : AstNode { public override void Init(ParsingContext context, ParseTreeNode treeNode) { base.Init(context, treeNode); AsString = "TagList"; foreach (var node in treeNode.ChildNodes) AddChild(null, node); } public override void EvaluateNode(Irony.Interpreter.EvaluationContext context, AstMode mode) { foreach (var node in ChildNodes) node.EvaluateNode(context, AstMode.Read); } } class TagBlockNode : AstNode { public AstNode Content; public override void Init(ParsingContext context,ParseTreeNode treeNode) { base.Init(context, treeNode); AsString = treeNode.ChildNodes[0].FindTokenAndGetText(); Content = AddChild(null, treeNode.ChildNodes[1]); } public override void EvaluateNode(EvaluationContext context, AstMode mode) { context.Write(string.Format("<{0}>", AsString)); Content.EvaluateNode(context, AstMode.Read); context.Write(string.Format("</{0}>", AsString)); } } 

This will result in the following output:

  <html><head><title></title></head><body><h1></h1><p></p><p></p></body></html>3.14159265358979 

While the weekend I want:

 <html> <head> <title>page title</title> </head> <body> <h1>header</h1> <p>paragraph 1</p> <p>3.14159265358979</p> </body> </html> 

I do not think I should use Context.Write() . The samples show pushing stuff onto context.Data and pushing them ... but I'm not quite sure how this works.

I assume that pi gets the anchored end at the end, because it automatically moved to context.Data , and then one element popped up at the end? I'm not sure.

Some pointers or a link to a tutorial will be nice.

Also, how should I handle different types of node? Each "tag" can have 4 different types of content: another tag, string literal, variable, or number. Should I write things like if(node is StringLiteral) .... in the EvaluateNode method or what?


I found this one , but they just iterate over AST and don't use EvaluateNode .

And then this one , which replaces a single value in the data stack ... but doesn’t really explain how this is output or anything.


To be clear, I specifically want to know how to override the EvaluateNode methods in Irony.Ast.AstNode to do what I want.


Ok, I traced this tidbit at the end of this line:

  if (EvaluationContext.HasLastResult) EvaluationContext.Write(EvaluationContext.LastResult + Environment.NewLine); 

What is included in the standard evaluation procedure ... perhaps it works well for the calculator application, but not so much in mine. Trying to figure out how to get around the script interpreter now, but then I don't know how to set global variables.

+7
source share
1 answer

The best way to iterate through the AST structure is to implement the visitor pattern .

Perhaps this link will help you.

0
source

All Articles