Advertisement
TheBirkIsReal

Untitled

Dec 15th, 2018
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.15 KB | None | 0 0
  1. print_node :: proc(out: os.Handle, node: ^Node) {
  2.     if node == nil do fmt.fprintf(out, "6;nil;;");
  3.     switch n in node.kind {
  4.         case NodeNull:
  5.             fmt.fprintf(out, "6;Null;;");
  6.         case NodeTrue:
  7.             fmt.fprintf(out, "6;True;;");
  8.         case NodeFalse:
  9.             fmt.fprintf(out, "6;False;;");
  10.         case NodeIdent:
  11.             fmt.fprintf(out, "6;Ident;%s;", n.name);
  12.         case NodeNumber:
  13.             fmt.fprintf(out, "6;Number;%v;", n.value);
  14.         case NodeString:
  15.             fmt.fprintf(out, "6;String;%v;", n.value);
  16.         case NodeIndex:
  17.             fmt.fprintf(out, "7;Index;");
  18.             //print_node()
  19.             fmt.fprintf(out, ";");         
  20.         case NodeField:
  21.             fmt.fprintf(out, "6;Field;Stub;");
  22.         case NodeCall:
  23.             fmt.fprintf(out, "6;Call;Stub;");
  24.         case NodeBinary:
  25.             fmt.fprintf(out, "7;Binary %v;", n.op);
  26.             fmt.fprintf(out, "7;lhs;");
  27.             print_node(out, n.lhs);
  28.             fmt.fprintf(out, ";");
  29.  
  30.             fmt.fprintf(out, "7;rhs;");
  31.             print_node(out, n.rhs);
  32.             fmt.fprintf(out, ";");
  33.  
  34.             fmt.fprintf(out, ";");
  35.         case NodeUnary:
  36.             fmt.fprintf(out, "6;Unary;Stub;");
  37.         case NodeAssignment:
  38.             fmt.fprintf(out, "6;Assignment;Stub;");
  39.         case NodeBlock:
  40.             fmt.fprintf(out, "7;Block (%v);", len(n.stmts));
  41.             for i in 0..len(n.stmts)-1 {
  42.                 print_node(out, n.stmts[i]);
  43.             }
  44.             fmt.fprintf(out, ";");
  45.         case NodeVariableDecl:
  46.             fmt.fprintf(out, "7;VariableDecl: %v;", n.name);
  47.             fmt.fprintf(out, "6;Name;%v;", n.name);
  48.             fmt.fprintf(out, "7;Expr;");
  49.             print_node(out, n.expr);
  50.             fmt.fprintf(out, ";");
  51.             fmt.fprintf(out, ";");
  52.         case NodeFn:
  53.             fmt.fprintf(out, "7;Fn %s;", n.name);
  54.             fmt.fprintf(out, "6;Name;%s;", n.name);
  55.             fmt.fprintf(out, "7;Args (%v);", len(n.args));
  56.             for a, i in n.args {
  57.                 fmt.fprintf(out, "6;#%v;%v;", i, a);
  58.             }
  59.             fmt.fprintf(out, ";");
  60.             print_node(out, n.block);
  61.             fmt.fprintf(out, ";");
  62.         case NodeIf:
  63.             fmt.fprintf(out, "6;If;Stub;");
  64.         case NodeForType:
  65.             fmt.fprintf(out, "6;ForType;Stub;");
  66.         case NodeFor:
  67.             fmt.fprintf(out, "6;For;Stub;");
  68.         case NodeReturn:
  69.             fmt.fprintf(out, "7;Return;");
  70.             print_node(out, n.expr);
  71.             fmt.fprintf(out, ";");
  72.         case NodeImport:
  73.             fmt.fprintf(out, "6;Import;Stub;");
  74.         case: panic("Invalid node in print_node");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement