ckknight / gorillascript

GorillaScript is a compile-to-JavaScript language designed to empower the user while attempting to prevent some common errors.
MIT License
300 stars 34 forks source link

Macros with InvocationArguments and returning the arguments #141

Closed kkirby closed 11 years ago

kkirby commented 11 years ago

Hello,

I have this macro:

macro A
    syntax args as InvocationArguments
        ASTE console.log $args

One would expect this code:

A \testing,\one,\two,\three

To output:

console.log('testing','one','two','three')

However, the output is actually:

console.log('three')

Is there anyway to return the arguments in the AST expression?

ckknight commented 11 years ago

The problem is that args is in fact an array of AST nodes, and when injected in a macro, it turns into a block. In the expression syntax, this is done through the comma operator.

So what you're doing is essentially console.log(('testing', 'one', 'two', 'three')), and since the first three are no-ops in that position, they are ignored, leaving just console.log('three').

What you need to do is convert the args into an array using @internal-call \array, args, then you can use it as an array node in your macro.

Here is a full example:

macro A
  syntax args as InvocationArguments
    let arr = @internal-call \array, args
    ASTE console.log ...$arr

A \testing,\one,\two,\three