Skip to content

Instantly share code, notes, and snippets.

@akfish
Last active August 29, 2015 13:57
Show Gist options
  • Save akfish/9401974 to your computer and use it in GitHub Desktop.
Save akfish/9401974 to your computer and use it in GitHub Desktop.
Sarcasm AST Mapping Syntax

Sarcasm AST Mapping Syntax

Goal

  • Specify and generate AST node class
  • Map and automatically intialize AST node's fields

The Syntax

For NonTerminal

We start with current rule syntax:

AddOpNode Add := Value "+" Value;

We will need manually initialize an AddOpNode instance:

class AddOpNode : AstNode
{
  public ValueNode X;
  public ValueNode Y;
  
  public void Init(AstContext context, ParseTreeNode node)
  {
    var children = node.getMappedChildren();
    X = (ValueNode)AddChild("x", node[0]);  
    Y = (ValueNode)AddChild("x", node[2]);  
  }
}

How about a syntax that does it all:

ValueNode Value := ID | STRING; // ID and STRING are terminals
AddOpNode Add := Value->.X "+" Value->.Y;

List initialization:

ValueListNode ValueList := Value{","}->.[];

For Terminal

Terminals should be considered as atom. So it is unlikely a good practice to have fields within. (TBD)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment