1

I'm trying to define some macros which allow a sequence of function calls to be declared for functions which are really asynchronous. The macros convert these sequence of calls into continuations; namely, a function which takes two parameters: the location in the function to continue at, and the local state of the function.

So, the idea is:

function test() {
    f(1)
    f(2)
}

=>

function test(_,local) {
    switch (_) {
    case 0: f(1,function() { test(1,local) }); return
    case 1: f(2,function() { test(2,local) }); return
    }
}

To do this, I have two macros defined, function and f. However in the general case there will be a whole list of functions besides f which can be arbitrarily used and intermingled with other (non-macro) code in the function definitions. This question seems to be asking a much more narrow version of the question, and I am not sure if the accepted answer (named patterns) would work (I can't see how to make it work), because of the need to allow an arbitrary number of sequential sub-macros selected from a wide variety of choices.

The problem is, this needs to work for nested anonymous functions, so I need some way to assign these sequential statement numbers in the case statements relative to the current function. How do I do this?

I've got an example here.

For the first case things work fine: each case statement passes the number of the next case statement in the callback function. But for the second case this fails, for instance, the first case statement is trying to access the number of the first case statement in the nested function. This is because we are using a global variable to track the case number, but I'm at a loss of how to track this variable relative to the current scope. Using console.log I've traced evaluation and have found that the individual macros are not evaluated in a nested fashion. How can I establish a value in one macro and then access that value from any macros which are lexical children to that macro?

Community
  • 1
  • 1
Michael
  • 9,060
  • 14
  • 61
  • 123

0 Answers0