sweet-js/sweet-core

Issue with dummy.fromParens(expr) not putting circular brackets.

Closed this issue · 10 comments

syntax m = ctx => {
  let dummy = #`dummy`.get(0);
  let expr = #`5 * 5`;
  return #`1 + ${dummy.fromParens(expr)}`;
}
m

when I run it it gives output
1 + 5 * 5;

Expeceted output is
1 + (5 * 5)

As mentioned in Reference documentation

syntax cond = function (ctx) {
  let bodyCtx = ctx.next().value.inner(); 
  let result = #``;
  let dummy = #`dummy`.get(0);
  for (let stx of bodyCtx) { 
    if (stx && stx.val() === "|") { 
      var c = bodyCtx.expand('expr').value;
      bodyCtx.next();
      let r = bodyCtx.expand('expr').value;
      result = result.concat(#`if(${c}){ ${r}(state, global) }`);
    }
  }
  return result;
}

cond {
  | condition1  : f1,
	|	condition2  : f2,
	| condition3  : f3,
	| condition4 : f4,
}

This gives output


if (condition1) {
  f1 state, global;
}
if (condition2) {
  f2 state, global;
}
if (condition3) {
  f3 state, global;
}
if (condition4) {
  f4 state, global;
}

Here f1 f2 f3 are function calls. how do add those parenthesis.

Humm, that does look like a bug

@disnet this is totally broken on master. Issues with RawSyntax and setLineNumber.

How should I fix it? Any pointers. I was not able to find RawSyntax and setLineNumber is not called in my code.

@iamnewspecies Those are references to the internal implementation of sweet.js. You've discovered a bug that will have to be fixed. Hopefully in the next release.

@gabejohnson what were you seeing broken on master? I just tried @iamnewspecies's cond example and after updating to the breaking changes we made the output looks fine.

syntax cond = function (ctx) {
  let bodyCtx = ctx.contextify(ctx.next().value);
  let result = #``;
  let dummy = #`dummy`.get(0);
  for (let stx of bodyCtx) {
    if (stx && stx.value.val() === "|") {
      var c = bodyCtx.expand('expr').value;
      bodyCtx.next();
      let r = bodyCtx.expand('expr').value;
      result = result.concat(#`if(${c}){ ${r}(state, global) }`);
    }
  }
  return result;
}

cond {
    | condition1  : f1,
    |	condition2  : f2,
    | condition3  : f3,
    | condition4 : f4,
}

produces:

;
if (condition1) {
  f1(state, global);
}
if (condition2) {
  f2(state, global);
}
if (condition3) {
  f3(state, global);
}
if (condition4) {
  f4(state, global);
}

@disnet I was referring to the first example. Sorry about the confusion.

I figured out the solution for this.

syntax ON_END = function (ctx) {
  let bodyCtx = ctx.next().value.inner(); 
  let result = #``;
  let dummy = #`dummy`.get(0);
  for (let stx of bodyCtx) { 
    if (stx && stx.val() === "|") { 
      var c = bodyCtx.expand('expr').value;
      bodyCtx.next();
      let r = bodyCtx.expand('expr').value;
      result = result.concat(#`if(${c}(state,global))${dummy.fromKeyword('return')} (${r}(state, global));`);
    }
  }
  return result;
}




ON_END {
  | condition1  : f1,
  | condition2  : f2,
  | condition3  : f3,
  | condition4 : f4,
};

I just had to add circular brackets outside it (${r}(state, global));)

Now I have one more issue

How do I put whatever output this generate inside a function call

syntax ON_END = function (ctx) {
  let bodyCtx = ctx.next().value.inner(); 
  let result = #``;
  let dummy = #`dummy`.get(0);
  for (let stx of bodyCtx) { 
    if (stx && stx.val() === "|") { 
      var c = bodyCtx.expand('expr').value;
      bodyCtx.next();
      let r = bodyCtx.expand('expr').value;
      result = result.concat(#`if(${c}(state,global))${dummy.fromKeyword('return')} (${r}(state, global));`);
    }
  }
  return #`f(state, global)${dummy.fromBraces(result)}`;
}




ON_END {
  | condition1  : f1,
  | condition2  : f2,
  | condition3  : f3,
  | condition4 : f4,
};

I tried the above code but it gives assertion error.

Error: [assertion error]: Not a primary expression
    at assert (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/errors.js:26:11)
    at Enforester.enforestPrimaryExpression (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:1201:24)
    at Enforester.enforestLeftHandSideExpression (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:1235:26)
    at Enforester.enforestAssignmentExpression (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:1113:19)
    at Enforester.enforestExpressionLoop (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:1021:23)
    at Enforester.enforestExpression (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:996:21)
    at Enforester.enforestExpressionStatement (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:984:21)
    at Enforester.enforestStatement (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:412:17)
    at Enforester.enforestModuleItem (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:124:17)
    at Enforester.enforestBody (/Users/newspecies/Documents/juspay/flow_framework/node_modules/sweet.js/dist/enforester.js:110:17)

How should I fix this?

Sorry my mistake. I should have done something like this let construct = #function ${name} () {};