Guest User

Untitled

a guest
Feb 3rd, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.72 KB | None | 0 0
  1. class RAST::Root is RAST::Node {
  2.     has str $!magic;
  3.     has int $!major_version;
  4.     has int $!minor_version;
  5.     has int $!format_type;
  6.     has %!misc;
  7.     has str $!label;
  8.     has str $!path;
  9.     has str $!absolute_path;
  10.     has int $!first_lineno;
  11.     has str $!type;
  12.     has @!locals;
  13.     has @!args;
  14.     has @!catch_table;
  15.     has @!bytecode;
  16.  
  17.     method BUILD(:@bytecode!, :$label!, :$path!, :$absolute_path!, :$type!) {
  18.         @!bytecode      := @bytecode;
  19.         $!magic         := "YARVInstructionSequence/SimpleDataFormat";
  20.         $!major_version := 1;
  21.         $!minor_version := 2;
  22.         $!format_type   := 1;
  23.         %!misc          := nqp::hash();
  24.         $!label         := $label;
  25.         $!path          := $path;
  26.         $!absolute_path := $absolute_path;
  27.         $!first_lineno  := 1;
  28.         $!type          := $type;
  29.         @!locals        := [];
  30.         @!args          := [];
  31.         @!catch_table   := [];
  32.     }
  33.  
  34.     method push_bytecode($bytecode) {
  35.         nqp::push(@!bytecode, $bytecode);
  36.     }
  37.  
  38.     method push_local($local) {
  39.         nqp::push(@!locals, $local);
  40.     }
  41.  
  42.     method push_arg($arg) {
  43.         nqp::push(@!args, $arg);
  44.     }
  45.  
  46.     method push_catch($catch) {
  47.         nqp::push(@!catch_table, $catch);
  48.     }
  49.  
  50.     method set_arity($arg_count) {
  51.         @!args = [$arg_count];
  52.     }
  53.  
  54.     method dump() {
  55.        
  56.         my @dumped = << '$magic' $major_version $minor_version $format_type >>;
  57.        
  58.         nqp::push(@dumped, dump_misc());
  59.        
  60.         nqp::push(@dumped, "'$!label'");
  61.        
  62.         nqp::push(@dumped, "'$!path'");
  63.  
  64.         if $!absolute_path ne '' {
  65.             nqp::push(@dumped, "'$!absolute_path'");
  66.         }
  67.         else {
  68.             nqp::push(@dumped, "nil");
  69.         }
  70.  
  71.         nqp::push(@dumped, $!first_lineno);
  72.        
  73.         nqp::push(@dumped, "\:$!type");
  74.        
  75.         my @locals;
  76.         for @!locals {
  77.             nqp::push(@locals, $_.dump());
  78.         }
  79.         nqp::push(@dumped, @locals);
  80.        
  81.         if @!args.elems == 1 {
  82.             nqp::push(@dumped, @!args[0]);
  83.         }
  84.         else
  85.         {
  86.             my @args;
  87.             for @!args {
  88.                 nqp::push(@args, $_);
  89.             }
  90.             nqp::push(@dumped, @args);
  91.         }
  92.        
  93.         my @catch_table;
  94.         for @!catch_table {
  95.             nqp::push(@catch_table, $_.dump());
  96.         }
  97.         nqp::push(@dumped, @catch_table);
  98.        
  99.         my @bytecode;
  100.         for @!bytecode {
  101.             nqp::push(@bytecode, $_.dump());
  102.         }
  103.         nqp::push(@dumped, @bytecode);
  104.  
  105.         return '[' ~ @dumped.join(', ') ~ ']';
  106.        
  107.     }
  108.  
  109.     method dump_misc() {
  110.         "\{ arg_size:   $!argsize, " ~
  111.           " local_size: $!local_size, " ~
  112.         " stack_max:  $!stack_max\}"
  113.     }
  114. }
  115.  
  116. =begin ValidRASTRootNode
  117.     This is a empty compiled program from ruby, this is our goal output for a blank nqp file
  118.  
  119.     [
  120.         "YARVInstructionSequence/SimpleDataFormat",
  121.         1,
  122.         2,
  123.         1,
  124.         {arg_size: 0, local_size: 1, stack_max: 1},
  125.         "<main>",
  126.         "/path/to/file.nqp".
  127.         nil,
  128.         1,
  129.         :top,
  130.         [],
  131.         0,
  132.         [],
  133.         [
  134.             2,
  135.             [
  136.                 :putnil
  137.             ],
  138.             [
  139.                 :leave
  140.             ]
  141.         ]
  142.     ]
  143. =end ValidRASTRootNode
  144. # vim: syn=perl6 ts=3 sts=3
Advertisement
Add Comment
Please, Sign In to add comment