I'm trying to implement ruby BEGIN and END blocks.

I can generate the blocks just fine. I just need to generate pir so they get called at the right time.
BEGIN{} and END {} semantic are described below.

I think that these are general use cases that should be provided by parrot.

Maybe the solution is two stacks of subs in the interpreter structure. One for initialization and one for finalization which are traversable and modifiable using opcodes or a specialized pmc.

My take is that he initialization stack gets executed just before :main and the finalization stack gets executed just before interpreter termination. Initialization should occur in the order that compilation units are loaded and secondarily in the order that BEGIN or initialization blocks occur within a compilation unit.

Kevin


From
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#BEGIN_proc


       BEGIN

Examples:

        BEGIN {
          ...
        }

Syntax:

        BEGIN '{'
          expr..
        '}'

Registers the initialize routine. The block followed after |BEGIN| is evaluated before any other statement in that file (or string). If multiple |BEGIN| blocks are given, they are evaluated in the appearing order.

The |BEGIN| block introduce new local-variable scope. They don't share local variables with outer statements.

The |BEGIN| statement can only appear at the toplevel.


     END

Examples:

        END {
          ...
        }

Syntax:

        END '{' expr.. '}'

Registers finalize routine. The block followed after |END| is evaluated just before the interpreter termination. Unlike |BEGIN|, |END| blocks shares their local variables, just like blocks.

The |END| statement registers its block only once at the first execution. If you want to register finalize routines many times, use at_exit <http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html#at_exit>.

The |END| statement can only appear at the toplevel. Also you cannot cancel finalize routine registered by |END|.

Reply via email to