sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.58k stars 208 forks source link

How to import macros by one macro, maybe we can name it meta-macro? #738

Closed rynkis closed 7 years ago

rynkis commented 7 years ago

Hi, thanks for the write-up, but I have got some problems. Here is my example.sjs:

'lang sweet.js'

export operator add left 1 = (left, right) => #`${left}.push(${right})`
export operator pip left 1 = (left, right) => {
  return right.type === 'CallExpressionE'
  ? #`${right.callee}(${left}, ${right.arguments})`
  : #`${right}(${left})`
}
export operator == left 10 = (left, right) => #`${left} === ${right}`
export operator != left 10 = (left, right) => #`${left} !== ${right}`

In usual, I can import these macros by:

import {add, pip, ==, !=} from 'example.sjs'

It works.

But in the future, maybe it will become:

import {add, pip, log, a, s, do, ==, !=} from 'example.sjs'

or longer.

So, I have tried to import all macros by another macro:

'lang sweet.js'

...

export syntax patch = ctx => {
  let dummy = #`dummy`.get(0)
  return #`import { add, pip, ==, != } from ${fromStringLiteral(dummy, __dirname)}`
}

then:


import { patch } from 'example.sjs'
patch

...

But failed.

In past version, we can use sjs -m to import all macros from specified .sjs file. So I can use cat export.sjs import.sjs > temp && sjs temp and remove export keyword to get the same effect. But is there any way for a more elegant, or new feature in plan such as:

import * from 'example.sjs' by sweet
import syntaxes from 'example.sjs' by sweet
import operators from 'example.sjs' by sweet
import some_macro_set from 'example.sjs' by sweet

to import macros by one macro? Thanks.

gabejohnson commented 7 years ago

@Shy07 your use case should be covered by extending the utility of the 'lang ...' directive.

The idea is to allow extending the reader. This would allow you to create custom punctuators among other things. It would also allow you to specify a set of macros to be implicitly imported.

I think your patch example fails to work because Sweet currently only has a single macro expansion phase.

@disnet please correct me if I'm wrong on any of these points.

rynkis commented 7 years ago

@gabejohnson , thanks. I have found the 'lang ...' directive in #697. That all sounds amazing, thank you.