Hi,
I have trouble understanding how lexicals work in the code that is generated
by PCT.

Consider the following Lua snippet:

function foo()

    local a = 2

    function bar()
        print(a)
    end
end

foo()
bar()

What happens here is, a function foo is defined, in which a  local var. "a"
is initialized to the value "2". Another function is defined, called "bar",
which prints the value of variable a. As "bar" is lexically nested within
function foo, "bar" has access to "a". Note that "bar" is only defined, it's
not run when "foo" is active. (function foo() ... can be rewritten as "foo =
function()..." )

Now, after defining "foo", foo is invoked: foo(). This will actually create
the lexical "a", and define "bar". After running foo, there should be a
function "bar".
So now we can invoke "bar": bar(). When running bar, the value of the
lexical "a" should be printed.

This doesn't work. It seems to me that the generated PIR is correct. I'm not
clear why this doesn't work.
Anybody who sees an obvious error I made?

thanks in advance,
kjs


The generated PIR looks like this:
=========================================
.namespace
.sub "_block10"
    get_global $P24, "_block11"
    set_global "foo", $P24
    get_global $P25, "foo"
    unless_null $P25, vivify_30
    new $P25, "Undef"
  vivify_30:
    $P25()
    get_global $P26, "bar"
    unless_null $P26, vivify_31
    new $P26, "Undef"
  vivify_31:
    $P27 = $P26()
    .return ($P27)
.end


.namespace
.sub "_block11"  :outer("_block10")
    get_global $P23, "_block12"
    newclosure $P23, $P23
    $P22 = $P23()
    .return ($P22)
.end

## this is the translation for function "foo":
.namespace
.sub "_block12"  :outer("_block11")
    new $P13, "Float"
    assign $P13, 2
    .lex "a", $P13
    get_global $P21, "_block14" ### here function "bar" is defined and
stored as a global.
    set_global "bar", $P21
    .return ($P21)
.end


.namespace
.sub "_block14"  :outer("_block12")
    get_global $P20, "_block15"
    newclosure $P20, $P20
    $P19 = $P20()
    .return ($P19)
.end

## this is thre translation for function "bar"
.namespace
.sub "_block15"  :outer("_block14")
    get_global $P16, "print"
    unless_null $P16, vivify_28
    new $P16, "Undef"
  vivify_28:
    find_lex $P17, "a"
    unless_null $P17, vivify_29
    new $P17, "Undef"
  vivify_29:
    $P18 = $P16($P17)
    .return ($P18)
.end

Reply via email to