develooper Front page | perl.perl6.internals.api.parser | Postings from December 2000

Re: Now, to try again...

Thread Previous | Thread Next
From:
Simon Cozens
Date:
December 23, 2000 12:46
Subject:
Re: Now, to try again...
Message ID:
20001223204609.A11515@deep-dark-truthful-mirror.perlhacker.org
On Mon, Dec 18, 2000 at 10:12:35AM -0500, Andy Dougherty wrote:
> The issues of 'use Python' or 'use Pythonish' are a quite different issue.
> I don't think anyone believes it ought to be easy to *write* the Pythonish
> module.  But it ought to be *possible*.

Incidentally, it's possible in Perl 5. Very possible; I'm just about to
finish off a module called B::Generate which allows you to create your
own op trees - it's basically B but with get/set methods instead of get
methods, plus constructors for all the classes. 

Although I haven't tested this yet, if you have a source filter which
implements a parser for your favourite language and uses B::Generate to spit
out an op tree and point Perl at the root of it, instant Python interpreter!

Well, maybe not exactly *instant*, but... :)

As a very simple example, the following program prints "-5" by replacing 
"add" with "subtract":

#!/usr/bin/perl
use B::Generate;

CHECK{
    my ($x, $y,$z);
    $x = B::main_start;
    for ($x = B::main_start; $x->type != 62; $x=$x->next){ # Find "add"
        $y=$x;  # $y is the op before "add"
    };
    $z = new B::BINOP("subtract",0,$x->first, $x->last); # Create replacement
    $z->next($x->next); # Copy add's "next" across.
    $y->next($z);       # Tell $y to point to replacement op.
}

$a = 10; $b = 15; print $a + $b; 


-- 
Only two things are infinite: the Universe and human stupidity, and I'm
not sure about the former - Albert Einstein

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About