Advertisement
Guest User

simple AST handling example

a guest
Feb 14th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 0.45 KB | None | 0 0
  1. enum AST
  2. {
  3.   Number(n:Float);
  4.   Sum(n1:AST, n2:AST);
  5.   Mult(n1:AST, n2:AST);
  6. }
  7.  
  8. class ASTTools
  9. {
  10.   public static function execute(a:AST)
  11.   {
  12.     return switch(a)
  13.     {
  14.       case Number(n): n;
  15.       case Sum(n1, n2): execute(n1) + execute(n2);
  16.       case Mult(n1, n2): execute(n1) * execute(n2);
  17.     }
  18.   }
  19. }
  20.  
  21. class Main
  22. {
  23.   static function main()
  24.   {
  25.     trace(ASTTools.execute( Sum( Mult(Number(20), Number(2)), Number(2) ) ));
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement