문제

I want to implement a language construct similiar to Ruby´s blocks. So

main()
{
    i = 123
    array.function(|x| { puts x + i; }) //the block needs access to the local variable i
}

should compile fine. I looked at the source code for 1 hour but since I´m not that good at C I still have no idea how they implement it. My first thought was it is a nested function which address gets passed as a parameter, but I´m not sure how the nested function has access to the local variable because it seems in most languages this isnt possible. If anyone can give me an idea of how to implement this I would be very glad

도움이 되었습니까?

해결책

Here is implementation of code blocks and yields in C, it may help you. http://tilarids.blogspot.com/2012/03/ruby-like-blocks-and-yield-keyword-in-c.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top