Hi all,

Trying to work through the tutorial
(http://www.parrotblog.org/search/label/tutorial) and am trying to
finish Episode 4 with catch blocks.  The grammar in Episode 3 has this:

  try-statement ::= 'try' block 'catch' identifier
                    block
                    'end'

I've translated it to this:

  rule try_statement {
    'try'   <block>
    'catch' <identifier> $<catch_block>=<block>
    'end'
    {*}
 }

In searching through code, I've come up with this method for that:

  method try_statement($/) {
      my $try        := $( $<block> );
      my $identifier := $( $<identifier> );
      my $catch      := $( $<catch_block> );
  
      my $past := PAST::Op.new(
          :pasttype('try'),
          :node($/)
      );
      $past.push($try);
  
      # Create the catch 
      my $catchpir := "    .get_results (%r, $S0)\n    store_lex '" ~
$identifier.name() ~ "', %r";
      $catch.unshift( PAST::Op.new( :inline( $catchpir ) ) );
  
      $past.push($catch);
      make $past;
  }

However, when I try it with this: "try x=4 catch e y=2 end", my pir
doesn't appear to have any logic to catch an exception (or I just know
pir very well).  Also, how do I know that the exception object is in
$S0?  My pir is below.  Is this actually correct and all will become
clear later?

Cheers,
Ovid

.namespace 
.sub "_block10" 
    push_eh catch_11
    get_global $P15, "_block12"
    newclosure $P15, $P15
    $P14 = $P15()
    pop_eh 
    goto catch_11_end
  catch_11:
    get_global $P19, "_block16"
    newclosure $P19, $P19
    $P19()
  catch_11_end:
    .return ($P14)
.end


.namespace 
.sub "_block16"  :outer("_block10")
    .get_results ($P17, $S0)
    store_lex 'e', $P17
    new $P18, "Integer"
    assign $P18, 2 
    set_global "y", $P18
    .return ($P18)
.end


.namespace 
.sub "_block12"  :outer("_block10")
    new $P13, "Integer"
    assign $P13, 4 
    set_global "x", $P13
    .return ($P13)
.end


--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/

Reply via email to