diff --git a/lib/operators.js b/lib/operators.js index 79a1da6..9370ff3 100644 --- a/lib/operators.js +++ b/lib/operators.js @@ -51,11 +51,12 @@ function processOperation(left, right, operator) { var rightType = right.type; var operatorName = operators[operator]; - //0. unary: i++, -x + //0. unary: i++, -x, !x, ~x if (!left) { // scalar if (this.types[rightType].length === 1) { var a = null, b = right; + var res = Descriptor(calculate(a, b, operator), { components: [calculate(a, b, operator)], type: leftType, @@ -64,7 +65,6 @@ function processOperation(left, right, operator) { return res } - // complex var outType = rightType; var vec = right; @@ -325,6 +325,10 @@ function processOperation(left, right, operator) { //x + 0 if (right == 0) opResult = left; } + // !a, ~a + else if (operator == '~' || operator == '~') { + opResult = operator + right + } else if (operator == '*') { //0 * x if (left == 0 || left.value === 0 || right == 0) opResult = 0; @@ -343,7 +347,7 @@ function processOperation(left, right, operator) { if (opResult == null) { opResult = '' - // embrace complex operators + // complex operators if (operator != '+' && operator != '-') { if (/\s/.test(left) && !(left[0] == '(' && left[left.length - 1] == ')')) opResult += '(' + left + ')' else opResult += left diff --git a/package-lock.json b/package-lock.json index b9c751a..d34b576 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,6 @@ }, "devDependencies": { "almost-equal": "^1.1.0", - "cln": "^1.1.0", "gl-matrix": "^2.3.2", "glslify": "^7.1.1", "glslify-promise": "^1.0.2", @@ -138,12 +137,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/cln": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/cln/-/cln-1.1.0.tgz", - "integrity": "sha512-hpXT0qDhgAP8RvpgrEfHFlRL5j+1wM3mASZQPDXCeDBuyhB71LRthpqxQNNGTAavrvfKO7g8Y60HJ9BN1Jk18w==", - "dev": true - }, "node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -2289,12 +2282,6 @@ "get-intrinsic": "^1.0.2" } }, - "cln": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/cln/-/cln-1.1.0.tgz", - "integrity": "sha512-hpXT0qDhgAP8RvpgrEfHFlRL5j+1wM3mASZQPDXCeDBuyhB71LRthpqxQNNGTAavrvfKO7g8Y60HJ9BN1Jk18w==", - "dev": true - }, "commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", diff --git a/package.json b/package.json index b1ef75d..a6a5df3 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ }, "devDependencies": { "almost-equal": "^1.1.0", - "cln": "^1.1.0", "gl-matrix": "^2.3.2", "glslify": "^7.1.1", "glslify-promise": "^1.0.2", diff --git a/test/api.js b/test/api.js index b0839cd..63b8009 100644 --- a/test/api.js +++ b/test/api.js @@ -6,7 +6,7 @@ import CompileStream from '../stream.js' import test from 'tape' import StringStream from 'stream-array' import { Writable } from 'stream' -import clean from 'cln' +import clean from './util/clean.js' var compile = GLSL({}) diff --git a/test/functions.js b/test/functions.js index 5ffe324..b2d9f26 100644 --- a/test/functions.js +++ b/test/functions.js @@ -1,7 +1,7 @@ import test from 'tape' import GLSL, { compile } from '../index.js' import evaluate from './util/eval.js' -import clean from 'cln' +import clean from './util/clean.js' import almost from './util/almost.js' diff --git a/test/index.html b/test/index.html index 9c1f226..3db300e 100644 --- a/test/index.html +++ b/test/index.html @@ -6,7 +6,7 @@ diff --git a/test/index.js b/test/index.js index dd7e224..0d7bf97 100644 --- a/test/index.js +++ b/test/index.js @@ -1,7 +1,7 @@ import GLSL from '../index.js' import test from 'tape' import evaluate from './util/eval.js' -import clean from 'cln' +import clean from './util/clean.js' import glsl from 'glslify' import './functions.js' @@ -608,6 +608,13 @@ test.skip('varying mat3 m[3]; m[1][0] = vec3(1., 1., 0.); m;', function (t) { t.end() }) +// #61 +test('foo=2.1;~~foo;', function (t) { + var compile = GLSL({ version: '300 es' }) + t.deepEqual(evaluate(t.name, { version: '300 es' }), 2) + t.end() +}) + // ` // vec2 pos; // float height; diff --git a/test/preprocessor.js b/test/preprocessor.js index 0ab7d07..0a33226 100644 --- a/test/preprocessor.js +++ b/test/preprocessor.js @@ -1,6 +1,6 @@ import test from 'tape' -import {compile} from '../index.js' -import clean from 'cln' +import { compile } from '../index.js' +import clean from './util/clean.js' test('Transform macro to commented', function (t) { t.equal(clean(compile(` diff --git a/test/primitives.js b/test/primitives.js index 73a1b69..3b25354 100644 --- a/test/primitives.js +++ b/test/primitives.js @@ -1,7 +1,7 @@ import GLSL from '../index.js' import test from 'tape' import evaluate from './util/eval.js' -import clean from 'cln' +import clean from './util/clean.js' var compile = GLSL({}) @@ -106,7 +106,7 @@ test('float(uint)', function (t) { // converts a Boolean value to a float test('float(bool)', function (t) { - t.equal(evaluate('+float(true);', {debug: false}), 1); + t.equal(evaluate('+float(true);', { debug: false }), 1); t.equal(evaluate('+float(false);'), 0); t.end() }) @@ -143,7 +143,7 @@ test('double(float)', function (t) { }) -test('should allow valid int initializations', function(t) { +test('should allow valid int initializations', function (t) { t.equal( clean(compile('void main() { int test = 1; }')), clean('function main () {\nvar test = 1;\n};')); @@ -162,7 +162,7 @@ test('should allow valid int initializations', function(t) { t.end() }) -test('should allow valid float initializations', function(t) { +test('should allow valid float initializations', function (t) { t.equal( clean(compile('void main() { float test = 1.0; }')), clean('function main () {\nvar test = 1.0;\n};')); @@ -193,7 +193,7 @@ test('should allow valid float initializations', function(t) { t.end() }) -test('should allow valid bool initializations', function(t) { +test('should allow valid bool initializations', function (t) { t.equal( clean(compile('void main() { bool test = true; }')), clean('function main () {\nvar test = true;\n};')); @@ -206,50 +206,50 @@ test('should allow valid bool initializations', function(t) { t.end() }) -test.skip('should throw on invalid int initializations', function(t) { - t.throws('void main() { int test = 1.0; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = .04; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = 0.50; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = 55.23; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = 5e3; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = 5.5e3; }', /Left and right arguments are of differing types/); +test.skip('should throw on invalid int initializations', function (t) { + t.throws('void main() { int test = 1.0; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = .04; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = 0.50; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = 55.23; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = 5e3; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = 5.5e3; }', /Left and right arguments are of differing types/); t.throws('void main() { int test = 5.5e-3; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = .5e3; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = true; }', /Left and right arguments are of differing types/); - t.throws('void main() { int test = false; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = .5e3; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = true; }', /Left and right arguments are of differing types/); + t.throws('void main() { int test = false; }', /Left and right arguments are of differing types/); t.end() }) -test.skip('should throw on invalid float initializations', function(t) { - t.throws('void main() { float test = 1; }', /Left and right arguments are of differing types/); - t.throws('void main() { float test = 55; }', /Left and right arguments are of differing types/); - t.throws('void main() { float test = 0x23; }', /Left and right arguments are of differing types/); - t.throws('void main() { float test = 023; }', /Left and right arguments are of differing types/); - t.throws('void main() { float test = true; }', /Left and right arguments are of differing types/); +test.skip('should throw on invalid float initializations', function (t) { + t.throws('void main() { float test = 1; }', /Left and right arguments are of differing types/); + t.throws('void main() { float test = 55; }', /Left and right arguments are of differing types/); + t.throws('void main() { float test = 0x23; }', /Left and right arguments are of differing types/); + t.throws('void main() { float test = 023; }', /Left and right arguments are of differing types/); + t.throws('void main() { float test = true; }', /Left and right arguments are of differing types/); t.throws('void main() { float test = false; }', /Left and right arguments are of differing types/); t.end() }) -test.skip('should throw on invalid bool initializations', function(t) { - t.throws('void main() { bool test = 1; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 55; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 0x23; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 023; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 1.0; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = .04; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 0.50; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 55.23; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 5e3; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = 5.5e3; }', /Left and right arguments are of differing types/); +test.skip('should throw on invalid bool initializations', function (t) { + t.throws('void main() { bool test = 1; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 55; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 0x23; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 023; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 1.0; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = .04; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 0.50; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 55.23; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 5e3; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = 5.5e3; }', /Left and right arguments are of differing types/); t.throws('void main() { bool test = 5.5e-3; }', /Left and right arguments are of differing types/); - t.throws('void main() { bool test = .5e3; }', /Left and right arguments are of differing types/); + t.throws('void main() { bool test = .5e3; }', /Left and right arguments are of differing types/); t.end() }) -test('should default ints to 0', function(t) { +test('should default ints to 0', function (t) { t.equal( clean(compile('void main() { int test; }')), clean('function main () {\nvar test = 0;\n};')); @@ -259,23 +259,22 @@ test('should default ints to 0', function(t) { t.end() }) -test('should default floats to 0.0', function(t) { +test('should default floats to 0.0', function (t) { t.equal( clean(compile('void main() { float test; }')), clean('function main () {\nvar test = 0;\n};')); t.equal( clean(compile('void main() { float test, foo; }')), - clean('function main () {\nvar test = 0, foo = 0;\n};')); + clean('function main () {\nvar test = 0, foo = 0;\n};')); t.end() }) -test('should default bools to 0 (false)', function(t) { +test('should default bools to 0 (false)', function (t) { t.equal( clean(compile('void main() { bool test; }')), clean('function main () {\nvar test = false;\n};')); t.equal( clean(compile('void main() { bool test, foo; }')), - clean('function main () {\nvar test = false, foo = false;\n};')); + clean('function main () {\nvar test = false, foo = false;\n};')); t.end() }) - diff --git a/test/structs.js b/test/structs.js index fddeb09..590ef18 100644 --- a/test/structs.js +++ b/test/structs.js @@ -1,7 +1,7 @@ import GLSL from '../index.js' import test from 'tape' import evaluate from './util/eval.js' -import clean from 'cln' +import clean from './util/clean.js' var compile = GLSL({}) diff --git a/test/util/clean.js b/test/util/clean.js new file mode 100644 index 0000000..620d36b --- /dev/null +++ b/test/util/clean.js @@ -0,0 +1,17 @@ +export default function clean(str) { + if (Array.isArray(str)) str = String.raw.apply(String, arguments) + + return str.trim() + + //remove empty lines + .replace(/^\s*\n/gm, '') + + //remove indentation/tabulation + .replace(/^\s*/gm, '') + + //transform all \r to \n + .replace(/[\n\r]+/g, '\n') + + //replace duble spaces/tabs to single ones + .replace(/(\s)\s+/g, '$1') +} diff --git a/test/vectors.js b/test/vectors.js index b294168..35e5161 100644 --- a/test/vectors.js +++ b/test/vectors.js @@ -3,7 +3,7 @@ import test from 'tape' import GLSL, { compile } from '../index.js' import almost from './util/almost.js' import evaluate from './util/eval.js' -import clean from 'cln' +import clean from './util/clean.js' // constructors