On 11/9/06, Patrick R. Michaud <[EMAIL PROTECTED]> wrote:
> Opinions welcome.  Personally I think I favor the "a compiler is
> an object with a 'compile' method" model, and that C<compreg> gives
> us back a compiler object as opposed to a subroutine-like thing.

For the record, it was decided (Allison++) during today's 
#parrotsketch meeting that the convention would be to have 
the 'compreg' opcode return an object with a 'compile' method, 
as opposed to returning an invokable sub.

Of course, individual language implementors may choose to
defy convention, and for the forseeable future the "PIR" 
and "PASM" compilers that come with Parrot will continue 
to be subroutine-like.  All of this just means that
callers to compreg need to know what they are getting back
or else figure it out at runtime.

I'm hoping to formalize some of the details for creating
compilers into a "compilers pdd" at some not-too-distant date.
Comments and suggestions welcomed.  At the moment we have
the following:

  - There's a HLLCompiler class (loadlib 'Parrot/HLLCompiler.pbc')
    that can be used to quickly create compiler objects.

  - To register a new compiler using HLLCompiler:

        load_bytecode 'Parrot/HLLCompiler.pbc'
        .local pmc compile_sub, mycompiler

        ##   get the compilation subroutine
        compile_sub = get_global 'compile'

        ##   create a new compiler object
        mycompiler = new [ 'HLLCompiler' ]

        ##   register the language and compiler subroutine
        mycompiler.'register'('MyCompiler', compile_sub)

  - To perform a compile:

        mycompiler = compreg 'MyCompiler'
        $P0 = mycompiler.'compile'('...source code...')

In addition, HLLCompiler provides a 'command_line' method
for acting as a standalone compiler; thus a .pir/.pbc can
simply have its :main sub delegate control directly
to HLLCompiler.  For example:

        .sub main :main
            .param pmc args

            .local pmc mycompiler
            mycompiler = compreg 'MyCompiler'
            mycompiler.'command_line'(args)
        .end

When invoked in this manner, compiler object will compile 
and execute any source file given on the command line, or 
enter an interactive mode if no source file is given.  
The result of the compilation can be controlled by the
--target command line (assuming the compilers involved
support this):

    --target=parse       # output parse tree
    --target=past        # output ast (PAST)
    --target=post        # output opcode tree (POST)
    --target=pir         # output PIR

HLLCompiler also understands '--encoding' (for languages that
have specific character encoding requirements), and '--output'
to specify a file where the output should be placed.

Comments and other feedback are greatly appreciated.

Thanks!

Pm

Reply via email to