3

I'm trying to create a pair of sweet js macros where an inner and outer macro share a local variable:

syntax cdo = function(ctx) {
    let call = ctx.next().value;
    let dummy = #`dummy`.get(0);
    let context_id = #`${dummy.fromIdentifier("$context$")}`;
    return #`let ${context_id} = ${call}`;
}

syntax context = function(ctx) {
     let func = ctx.next().value; // function keyword
     let func_name = ctx.next().value;
     let args = ctx.next().value; // all args
     let body = ctx.next().value; // function body

     let dummy = #`dummy`.get(0);
     let context_id = #`${dummy.fromIdentifier("$context$")}`;

     let body_result = #`let ${context_id} = 1234`;
     for (let stx of body.inner()) {
         body_result = body_result.concat(#`${stx}`);
     }

     return #`${func} ${func_name}${args}{${body_result}}`;
}

context function foo(a, b) {
    cdo bar();
}

What I want is for the "context" macro call to be able to assign a local variable that is accessible/assignable to the inner cdo macro call.

This is using sweet js 1.0, I've looked into using name() to access the calling context, however I can't seem to get this to work. What's the right way to do this?

Paul Johnson
  • 1,329
  • 1
  • 12
  • 25

0 Answers0