Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic examples not working #750

Open
danielo515 opened this issue Jun 14, 2018 · 6 comments
Open

Basic examples not working #750

danielo515 opened this issue Jun 14, 2018 · 6 comments

Comments

@danielo515
Copy link

Hello,

Taking several of the basic examples and trying to run them on the online editor produces errors.
For example:

syntax let = function (ctx) {
  let ident = ctx.next().value;
  ctx.next(); // eat `=`
  let init = ctx.expand('expr').value;
  return #`
    (function (${ident}) {
      ${ctx} // <2>
    }(${init}))
  `
};

let bb8 = new Droid('BB-8', 'orange');
console.log(bb8.beep());

yields the following error:

Goto  Error: Unknown object: {"value":{"token":{"type":{"klass":{"name":"String"},"name":""},"typeCode":4,"str":"BB-8","octal":null,"slice":{"text":"'BB-8'","start":229,"startLocation":{"line":12,"column":21,"filename":"","position":229},"end":235}},"bindings":{"_map":{}},"scopesets":{"all":[{"name":"outsideEdge_35"}],"phase":{"0":[{"name":"insideEdge0_36"}]}}},"type":"RawSyntax","loc":null} 

Same thing happens if I install sweet from npm and try to compile it.
I also tried to build a basic macro, but I'm only getting errors related to unknow objects to JSON.parse and that stuff.

The project seems to be a great potential, but I think it does not play well with the latest JS features like spread operator and template literals.

Regards

@gabejohnson
Copy link
Member

It looks like the arguments to Droid aren't being enforested. It will work if you wrap the constructor call in parens:

let bb8 = (new Droid('BB-8', 'orange'));
console.log(bb8.beep());

@danielo515
Copy link
Author

Hello @gabejohnson
Thanks for your answer.

Seems that you know how to workaround the possible problems that could emerge. Could you please help me building a very simple macro ? I just want to simulate the apply operator from haskell. So:

add $ 1 2
// becomes
add (1) (2)

I made several attempts, but none of them succeed compiling.
Regards

@gabejohnson
Copy link
Member

@danielo515 You're going to need to use the operator keyword as outlined https://www.sweetjs.org/doc/tutorial#sweet-operators

This is what I came up with

operator $ right 0 = (left, right) =>
  #`
    ${left}(${right})
  `;

log $ 1; // log(1);

You're not going to be able to support something like log $ 1 2 though as the second argument to $ is an already fully expanded expression. log $ 1 $ 2 should work though.

@vendethiel
Copy link

@gabejohnson "space" does not play in precedence resolution, right?

@gabejohnson
Copy link
Member

@vendethiel not certain what you mean, but the space b/w 1 2 should just result in a syntax error. But sweet resolves it by putting 2 in a separate statement.

@danielo515
Copy link
Author

Hello @gabejohnson ,
That's something I tried already. Since writing one dollar sing instead of two parents is less typing, the final result will be equally ugly.
This seems to be a great challenge for everything I tried so far: babel-macros, livescript, babel plugins, sweet....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants