> Where in the parrot code does the actual translation
> from byte code to binary code occur?

Parrot eq. an interpreter, all the byte codes are like
commands to tell it what actions to take... it doesn't
directly take byte codes and turn them into binary code.

Conversion would be compiling, but the benefit of using
bytecode is that if parrot compiles then the bytecode
can be executed.  This is not like gcc's internal
bytecode, which is just an intermediate step.

Parrot works rather like this simple RPN evaluator:

#!/usr/bin/perl -w

use strict;

my @stack;

# Setup function table and pattern
my %ops = ( '+' => sub { $stack[-2] += pop @stack       },
            '-' => sub { $stack[-2] -= pop @stack       },
            '*' => sub { $stack[-2] *= pop @stack       },
            '/' => sub { $stack[-2] /= pop @stack       },
            '^' => sub { $stack[-2] ^= pop @stack       },
            '!' => sub { $stack[-1]  = fact($stack[-1]) },
            'd' => sub { pop @stack                     },
            'p' => sub { print $stack[-1]               },
            'P' => sub { print pop @stack               },
            'r' => sub { return $stack[-1]              },
            's' => sub { @stack[-2,-1] = @stack[-1,-2]  },
            'c' => sub { @stack = ()                    }
          );

# Create re patterns
my $ops = join("|", map { quotemeta } keys %ops);
my $num = qr/\d+(?:\.\d+)?/;

# RPN Expression Evaluator
sub eval_RPN {
  local $_ = shift;

  while (/($ops|$num|\s+|.+)/go) {
    my $token = $1;

    if (exists $ops{$token}) {
      $ops{$token}();
    }

    elsif ($token =~ /\s+/) {
      # Do nothing
    }

    elsif ($token =~ /^$num$/) {
      push @stack, $token;
    }

    else {
      die "Don't know what to do with: $_";
    }
  }
  return pop @stack;
}

sub fact {
  my ($x, $e) = (abs int shift, 1);
  while ($x>1) { $e*=$x-- }
  return $e;
}

### TEST ###
print eval_RPN(join " ", @ARGV);

__END__

I wrote that one a while back... every programmer
has written one.  Try:

../rpn 1 2 '+' 4 '*' p

to get a feel of what it does.  It mimicks the dc
command.

Now, back to the topic:

> where does it get executed?

In C.

> Im having a hard time finding things in the code...

I haven't looked at the code, so don't feel bad :P

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

Reply via email to