Skip to content

Boolean negation/NOT prepends "null ", causing JS syntax error #61

Closed
@willheslam

Description

@willheslam
import GLSL from "glsl-transpiler";

const glsl = new GLSL({ });

const result = glsl(`!foo;`);

result === '\nnull ! foo;'

! isn't included in the "examplary source, containing all possible tokens" inside test/api.js or test/fixture/*.glsl

Under lib/operators.js, boolean complement and negation have been commented out since
d68a9ed
so it looks like this has been this way for a while.

Uncommenting out either of the operators causes the tests to fail with

~/glsl-transpiler$ npm test

> [email protected] test
> node test/index.js

undefined:3
out[0] = a[0] ! b[0]
              ^

SyntaxError: Unexpected token '!'
    at new Function (<anonymous>)
    at createOperations (file:///home/willheslam/glsl-transpiler/lib/stdlib.js:99:30)

My guess is this is because ! and ~ are unary operators, not binary.

This gives a clue as to why null is being prepended - perhaps glsl-transpiler is treating the ! token as though it's binary, and null is the missing first operand! Hopefully it's a straight forward fix because GLSL and JS are 1:1 here, at least syntactically.

processOperation in operators.js already seems to handle unary operators like i++ and -x so hopefully it can also apply to ! and ~.

A workaround for now is just to negate manually, i.e. foo ? false : true or foo == false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions