Sam Ruby <[EMAIL PROTECTED]> wrote:

> ...  Suggestions welcome, in
> particular, a PIR equivalent to the Perl would be most helpful.

It could be something like below. Some remarks:

* we don't have a notion to create a Closure PMC, so these closures are
  handcrafted. (NB: a subroutine with a .yield inside gets already
  created as a Coroutine PMC constant)
* the import statement is simulated too by storing the lexicals into the
  caller's frame. This would very likely be another Python opcode.
* the builtins would be at the top-level pad. The lexical opcodes do not
  fully comply with Python's name lookup, which is something like this:

    for d in (locals, globals, builtins):
      try:
         f = d[wrapped_name]:
            return f
      except KeyError:
         pass
      except:
         raise
    # raise NameError

# scope1.pir

.namespace [""]
.sub __main__ @MAIN
    new_pad 0
# from scope2 import *
    load_bytecode "scope2.pir"
    $P0 = find_global "scope2", "_scope2__import"
    $P0()
# print f(), foo
    .local pmc f
    f = find_lex "f"
    $P2 = f()
    .local pmc foo
    foo = find_lex "foo"
    print_item $P2
    print_item foo
    print_newline
# foo = 1
    $P4 = new Undef
    $P4 = 1
    store_lex -1, "foo", $P4
    foo = find_lex "foo"
#print f(), foo
    $P3 = f()
    print_item $P3
    print_item foo
    print_newline
.end

# scope2.pir

.namespace ["scope2"]
.sub _scope2__init @LOAD
    new_pad -1
# foo = 2
    $P0 = new Undef
    $P0 = 2
    store_lex -1, "foo", $P0
# def f(): return foo
    .local pmc f
    f = new Closure
    set_addr f, _f
    store_lex -1, "f", f
    $P1 = new Closure
    set_addr $P1, _scope2__import
    store_global "scope2", "_scope2__import", $P1
.end
.sub _scope2__import    # @CLOSURE
    $P0 = find_lex "foo"
    store_lex -2, "foo", $P0
    $P0 = find_lex "f"
    store_lex -2, "f", $P0
.end
.sub _f                 # @CLOSURE
   $P0 = find_lex "foo"
   .pcc_begin_return
   .return $P0
   .pcc_end_return
.end

leo

Reply via email to