Skip to content

Commit

Permalink
use return 0,<exp> to try to deal with comments.
Browse files Browse the repository at this point in the history
The problem is JavaScript's idiotic automatic semicolon insertion.

If you have an expression like

```js
    return
     2
```

That's just returns undefined. So, if the user enters

```
// hello
t
```

That gets converted to

```js
return // hello
t
```

We used to try to strip linefeeds and comments and it mostly worked
but the comment stripper was simple because who the F wants to write
a whole effing parser just to strip comments.

As a try, if we change the generated code to

```js
return 0,// hello
t
```

The automatic semicolon insertion doesn't happen and it correctly
returns t.
  • Loading branch information
greggman committed Feb 4, 2025
1 parent d99fde3 commit 3bf5615
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ByteBeatCompiler.js
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@ export default class ByteBeatCompiler {

static removeCommentsAndLineBreaks(x) {
// remove comments (hacky)
x = x.replace(/\/\/.*/g, ' ');
x = x.replace(/\n/g, ' ');
x = x.replace(/\/\*.*?\*\//g, ' ');
//x = x.replace(/\/\/.*/g, ' ');
//x = x.replace(/\n/g, ' ');
//x = x.replace(/\/\*.*?\*\//g, ' ');
return x;
}

@@ -326,7 +326,7 @@ export default class ByteBeatCompiler {
} else { // infix
x = `
return function(t, i, stack, window, extra) {
return ${ByteBeatCompiler.strip(x)};
return 0,${ByteBeatCompiler.strip(x)};
}`;
}
}

2 comments on commit 3bf5615

@jan-ale
Copy link

@jan-ale jan-ale commented on 3bf5615 Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks all postfix examples, except for "glitch machine" and "sansdetta?"

@greggman
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fixed now.

Please sign in to comment.