Skip to content

Nested .STATIC Sections #117

Description

@cspiel

.STATIC sections nested inside sections are interesting.

  • Problem 1: The documentation says (in section 4.15.1) that plain
    .STATIC targets export all of their variables. However, the
    export appears incomplete: we can access such a variable, but a
    function defined inside the same .STATIC block does not retrieve
    the variable's contents. No error is emitted, though.

  • Problem 2: If we export a function from a section and this
    function binds a variable from a nested .STATIC section, the
    binding dissappears outside of the defining section.

Example program (see comments for locations of Problem 1 and
Problem 2):

##  Uncomment the next line to see dynamic binding in action.
#public.b = $'top-level def'


section
        .STATIC:  # `.MEMO' works, too ;-)
                a = $'sectioned, static def'
                fa() =
                        return $(a)

                ##  Use `a' and `fa' inside of static block.
                println($"[section] [static]  a = <$(a)>")
                println($"[section] [static]  fa: <$(fa)>")


        ##  Use `a' and `fa' outside of static block.
        println($"[section]           a = <$(a)>")

        ##  Problem 1: No error here, but "empty" result.  We have
        ##  just proven that `a' is visible, so `fa' ought to return
        ##  `a's contents.
        println($"[section]           fa: <$(fa)>")


        ##  Copy static `a' to non-static `b'.
        b = $(a)
        println($"[section]           b = <$(b)>")

        ga() =
                return $(a)
        gb() =
                return $(b)


        ##  Use `ga' and `gb' in the same scope as their definitions.
        println($"[section]           ga: <$(ga)>")
        println($"[section]           gb: <$(gb)>")

        ##  `unbound variable' error triggered outside of `section'
        ##  disappears if we add `a' to the export list.  No need to
        ##  export `b', though because of dynamic binding.
        export ga gb


##  Sectioned `b' has been "captured" by `gb'.
println($"gb: <$(gb)>")

##  Problem 2: The next line fails with `unbound variable: public.a'.
println($"ga: <$(ga)>")

Output of osh example.om:

[section] [static]  a = <sectioned, static def>
[section] [static]  fa: <sectioned, static def>
[section]           a = <sectioned, static def>
[section]           fa: <>
[section]           b = <sectioned, static def>
[section]           ga: <sectioned, static def>
[section]           gb: <sectioned, static def>
gb: <sectioned, static def>
*** omake error:
   File example.om: line 30, characters 23-27
   unbound variable: public.a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions