Version 7 (modified by tene, 13 years ago)

Add more to the usage example

General Issues

  • Until case mangling issues in Parrot are fixed, you must use an all-lowercase HLL name (someone plz add TT#)
  • Due to current bugs in Parrot, the 'load_language' opcode *must* be called from the 'parrot' HLL

Library Loading

To provide other languages access to libraries written in your language, you must add a 'load_library' method to your compiler object. This method must accept one positional parameter, a list (or ::-delimited string?) representing the name of the library to load, and an arbitrary quantity of other named parameters (:named :slurpy), which it is (currently) free to ignore. This method should return a hash containing the following attributes:

symbols:       # The only required attribute
    DEFAULT:
        # a map of names to symbols
    ALL:
        # a map of names to symbols
    # other named sets of symbols can go here...
    # the only required items are DEFAULT and ALL
# You can include other additional information
filename: "/path/to/filename.ext"
version: "v1.0.1"
author: "adent"

For existing implementations of this, look at runtime/parrot/languages/parrot/parrot.pir in the Parrot repository and perl6.pir in the rakudo repository.

An example implementation of a 'foreign_load' function:

.HLL 'example'

.sub 'foreign_load'
    .param string lang
    .param string module
    .local pmc compiler, name, library, imports, callerns, foreignlibns
    $P0 = getinterp
    callerns = $P0['namespace';1]
    'load-language'(lang)          # This sub just calls the load_language opcode, but from the 'parrot' HLL, to avoid a parrot bug
    compiler = compreg lang
    name = split '::', module
    library = compiler.'load_library'(name)
    imports = library['symbols']
    imports = imports['DEFAULT']
    .local pmc iter, item
    iter = new 'Iterator', imports
  import_loop:
    unless iter goto import_loop_end
    $S0 = shift iter
    $P0 = imports[$S0]
    callerns[$S0] = $P0
    goto import_loop
  import_loop_end:
    foreignlibns = library['namespace']
    if null foreignlibns goto no_foreign_ns
    $S0 = pop name
    set_hll_global name, $S0, foreignlibns
  no_foreign_ns:
    .return (library)
.end

.HLL 'parrot'

# Workaround for a Parrot bug
.sub 'load-language'
    .param pmc name
    load_language name
.end

# Switch back
.HLL 'example'