0

tl;dr

when i have macro's in separate files i get an error, but if macro's are in same file it seems to work.

test-macro-1.js:

macro genVar {
  case {$name ($varName, $varVal, $name2)} => {
    letstx $ident = [makeIdent(unwrapSyntax(#{$varName}), #{$name2})];
    return #{var $ident = $varVal}
  }
}

export genVar;

test-macro-2.js:

macro someVars {
  case {$name ()} => {
    return #{
      genVar('foo', 'hello world', $name);
      genVar('bar', 'goodbye cruel world', $name)
    }
  }
}

export someVars;

test.js:

someVars();

console.log('foo=%s', foo);
console.log('bar=%s', bar);

results:

$ sjs -r --module ./test-macro-1.js --module ./test-macro-2.js test.js
/usr/local/lib/node_modules/sweet.js/lib/sweet.js:90
                    throw new SyntaxError(syn.printSyntaxError(source$2, err))
                          ^
SyntaxError: [macro] Macro `someVars` could not be matched with `...`
1: someVars();
   ^
    at expand$2 (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:90:27)
    at parse (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:123:29)
    at Object.compile (/usr/local/lib/node_modules/sweet.js/lib/sweet.js:129:19)
    at Object.exports.run (/usr/local/lib/node_modules/sweet.js/lib/sjs.js:85:27)
    at Object.<anonymous> (/usr/local/lib/node_modules/sweet.js/bin/sjs:7:23)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

but...if i place the macro's in the same file like so:

test-macro-1.js:

macro genVar {
  case {$name ($varName, $varVal, $name2)} => {
    letstx $ident = [makeIdent(unwrapSyntax(#{$varName}), #{$name2})];
    return #{var $ident = $varVal}
  }
}

export genVar;

macro someVars {
  case {$name ()} => {
    return #{
      genVar('foo', 'hello world', $name);
      genVar('bar', 'goodbye cruel world', $name)
    }
  }
}

export someVars;

results:

$ sjs -r --module ./test-macro-1.js test.js
var foo = 'hello world';
var bar = 'goodbye cruel world';
console.log('foo=%s', foo);
console.log('bar=%s', bar);

so does anyone with some experience have any guidance around the proper way to arrange macros into files to achieve both modularity and functionality?

tony_k
  • 1,983
  • 2
  • 20
  • 27

1 Answers1

1

A limitation of our current so-called "module" system unfortunately. Macros exported from a module will only be bound in the file being exported and the compilation target (so genVar is only bound in test-macro-1.js and test.js). Usually what people do is define all of their macros in a single file macros.js and just use that to compile stuff.

We're working on a real module system that solves this and other pain points but it's not ready yet.

timdisney
  • 5,287
  • 9
  • 35
  • 31
  • holy crap, that's one monster pull-request...! – tony_k Feb 24 '15 at 02:27
  • but, seriously, at a glance, the module system looks good. for what i am doing, i can put both macros in the same file, but generally, it would be great to be able to compose macros from diff packages. if you get a moment, it would be awesome to see how the new syntax would look to resolve this sample use case. if you point me in the right direction, i would be happy to test it out with your p/r. my particular use-case is pretty trivial, but the more i get acclimated, the more impressed i am with sweet, very powerful. great work tim, thx! – tony_k Feb 24 '15 at 02:40