discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: StepTalk Segfaults


From: Matthew D Swank
Subject: Re: StepTalk Segfaults
Date: Tue, 22 Feb 2005 21:47:51 -0600
User-agent: Mozilla Thunderbird 1.0 (X11/20050209)

There are two problems with blocks. One that I can identify, and one that still eludes me.

1. Formal parameters to blocks are global in the (unnamed) method; they are indexed by 'NSMutableArray *temporaries' in the home MethodContext of the block.

        This makes code like:
                |x y z|
                addMaker := [:addend| [:x|addend+x]].
                add3 := addMaker valueWith:3.
                x := 5.
                Transcript show: (add3 valueWith:4).

        throw: STCompilerGenericException, reason: Multiple definition of 
temporary variable.

If this were the only problem I could run my original example by picking unique variable names for the CPS style blocks:
        
        |main sub|
        sub :=
                [:my_other_continuation |
                Transcript  show: '\n   begin sub'.
                Transcript  show: '\n   |-- Objective C --|'.
                my_other_continuation value.
                Transcript  show: '\n   end sub'.].

        main :=
                [:my_continuation|
                Transcript show: '\n begin main'.
                Transcript show: '\n |-- StepTalk --|'.
                sub
                  valueWith:
                        [Transcript show: '\n |-- ANSI C --|'.
                                my_continuation value.].
                Transcript show: '\n end main'.].

        Transcript show: '\nbegin program'.
        main  valueWith: [Transcript show: '\nend program'.].

However this does not work. 'my_continuation' in:

[Transcript show: '\n |-- ANSI C --|'. my_continuation value.].
ends up being bound to its own containing block, resulting in an infinite loop.
This brings us to problem two.

2. Given two block parameters in _different_ blocks, but sharing the _same_ position in the parameter list, the one bound last can mask the value of the first one.

           An example should (hopefully) make this more clear.
           Given:

                |sub main|
                sub := [:block| block value.].
main := [:y| sub valueWith:[Transcript show:y.].]. main valueWith:3.

           'y' will end up bound to '[:block| block value.]' instead of '3'
           However, if instead we write:

                |sub main|
                sub := [:block| block value.].
main := [:x :y| sub valueWith:[Transcript show:y.].]. main valueWith:nil with:3.

           'y' is bound to '3' as expected.
        
In the end I can get my example to work like this:

        |main sub|
        sub :=
                [:continuation |
                Transcript  show: '\n   begin sub'.
                Transcript  show: '\n   |-- Objective C --|'.
                continuation value.
                Transcript  show: '\n   end sub'.].

        main :=
                [:dummy :continuation|
                Transcript show: '\n begin main'.
                Transcript show: '\n |-- StepTalk --|'.
                sub
                  valueWith:
                        [Transcript show: '\n |-- ANSI C --|'.
                                continuation value.].
                Transcript show: '\n end main'.].

        Transcript show: '\nbegin program'.
        main  valueWith: nil with: [Transcript show: '\nend program'.].

Note: it is ok that I use 'continuation' in both blocks.
I guess i still don't understand how block parameters are handled.  They share 
the same
name space as all the other variables local to the method, but when accessed inside (possibly nested) blocks, redefinition is not a problem, but position is.

Matt





reply via email to

[Prev in Thread] Current Thread [Next in Thread]