Skip to content

Commit b44b68e

Browse files
committed
Handle unaries properly
1 parent 8554ae8 commit b44b68e

File tree

12 files changed

+77
-64
lines changed

12 files changed

+77
-64
lines changed

lib/operators.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ function processOperation(left, right, operator) {
5151
var rightType = right.type;
5252
var operatorName = operators[operator];
5353

54-
//0. unary: i++, -x
54+
//0. unary: i++, -x, !x, ~x
5555
if (!left) {
5656
// scalar
5757
if (this.types[rightType].length === 1) {
5858
var a = null, b = right;
59+
5960
var res = Descriptor(calculate(a, b, operator), {
6061
components: [calculate(a, b, operator)],
6162
type: leftType,
@@ -64,7 +65,6 @@ function processOperation(left, right, operator) {
6465
return res
6566
}
6667

67-
6868
// complex
6969
var outType = rightType;
7070
var vec = right;
@@ -325,6 +325,10 @@ function processOperation(left, right, operator) {
325325
//x + 0
326326
if (right == 0) opResult = left;
327327
}
328+
// !a, ~a
329+
else if (operator == '~' || operator == '~') {
330+
opResult = operator + right
331+
}
328332
else if (operator == '*') {
329333
//0 * x
330334
if (left == 0 || left.value === 0 || right == 0) opResult = 0;
@@ -343,7 +347,7 @@ function processOperation(left, right, operator) {
343347
if (opResult == null) {
344348
opResult = ''
345349

346-
// embrace complex operators
350+
// complex operators
347351
if (operator != '+' && operator != '-') {
348352
if (/\s/.test(left) && !(left[0] == '(' && left[left.length - 1] == ')')) opResult += '(' + left + ')'
349353
else opResult += left

package-lock.json

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
},
1515
"devDependencies": {
1616
"almost-equal": "^1.1.0",
17-
"cln": "^1.1.0",
1817
"gl-matrix": "^2.3.2",
1918
"glslify": "^7.1.1",
2019
"glslify-promise": "^1.0.2",

test/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import CompileStream from '../stream.js'
66
import test from 'tape'
77
import StringStream from 'stream-array'
88
import { Writable } from 'stream'
9-
import clean from 'cln'
9+
import clean from './util/clean.js'
1010

1111
var compile = GLSL({})
1212

test/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'tape'
22
import GLSL, { compile } from '../index.js'
33
import evaluate from './util/eval.js'
4-
import clean from 'cln'
4+
import clean from './util/clean.js'
55
import almost from './util/almost.js'
66

77

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script type="importmap">
77
{
88
"imports": {
9-
"tape" : "../node_modules/tape/tape.js",
9+
"tape" : "../node_modules/tape/tape.js"
1010
}
1111
}
1212
</script>

test/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import GLSL from '../index.js'
22
import test from 'tape'
33
import evaluate from './util/eval.js'
4-
import clean from 'cln'
4+
import clean from './util/clean.js'
55
import glsl from 'glslify'
66

77
import './functions.js'
@@ -608,6 +608,13 @@ test.skip('varying mat3 m[3]; m[1][0] = vec3(1., 1., 0.); m;', function (t) {
608608
t.end()
609609
})
610610

611+
// #61
612+
test('foo=2.1;~~foo;', function (t) {
613+
var compile = GLSL({ version: '300 es' })
614+
t.deepEqual(evaluate(t.name, { version: '300 es' }), 2)
615+
t.end()
616+
})
617+
611618
// `
612619
// vec2 pos;
613620
// float height;

test/preprocessor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'tape'
2-
import {compile} from '../index.js'
3-
import clean from 'cln'
2+
import { compile } from '../index.js'
3+
import clean from './util/clean.js'
44

55
test('Transform macro to commented', function (t) {
66
t.equal(clean(compile(`

test/primitives.js

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import GLSL from '../index.js'
22
import test from 'tape'
33
import evaluate from './util/eval.js'
4-
import clean from 'cln'
4+
import clean from './util/clean.js'
55

66

77
var compile = GLSL({})
@@ -106,7 +106,7 @@ test('float(uint)', function (t) {
106106

107107
// converts a Boolean value to a float
108108
test('float(bool)', function (t) {
109-
t.equal(evaluate('+float(true);', {debug: false}), 1);
109+
t.equal(evaluate('+float(true);', { debug: false }), 1);
110110
t.equal(evaluate('+float(false);'), 0);
111111
t.end()
112112
})
@@ -143,7 +143,7 @@ test('double(float)', function (t) {
143143
})
144144

145145

146-
test('should allow valid int initializations', function(t) {
146+
test('should allow valid int initializations', function (t) {
147147
t.equal(
148148
clean(compile('void main() { int test = 1; }')),
149149
clean('function main () {\nvar test = 1;\n};'));
@@ -162,7 +162,7 @@ test('should allow valid int initializations', function(t) {
162162
t.end()
163163
})
164164

165-
test('should allow valid float initializations', function(t) {
165+
test('should allow valid float initializations', function (t) {
166166
t.equal(
167167
clean(compile('void main() { float test = 1.0; }')),
168168
clean('function main () {\nvar test = 1.0;\n};'));
@@ -193,7 +193,7 @@ test('should allow valid float initializations', function(t) {
193193
t.end()
194194
})
195195

196-
test('should allow valid bool initializations', function(t) {
196+
test('should allow valid bool initializations', function (t) {
197197
t.equal(
198198
clean(compile('void main() { bool test = true; }')),
199199
clean('function main () {\nvar test = true;\n};'));
@@ -206,50 +206,50 @@ test('should allow valid bool initializations', function(t) {
206206
t.end()
207207
})
208208

209-
test.skip('should throw on invalid int initializations', function(t) {
210-
t.throws('void main() { int test = 1.0; }', /Left and right arguments are of differing types/);
211-
t.throws('void main() { int test = .04; }', /Left and right arguments are of differing types/);
212-
t.throws('void main() { int test = 0.50; }', /Left and right arguments are of differing types/);
213-
t.throws('void main() { int test = 55.23; }', /Left and right arguments are of differing types/);
214-
t.throws('void main() { int test = 5e3; }', /Left and right arguments are of differing types/);
215-
t.throws('void main() { int test = 5.5e3; }', /Left and right arguments are of differing types/);
209+
test.skip('should throw on invalid int initializations', function (t) {
210+
t.throws('void main() { int test = 1.0; }', /Left and right arguments are of differing types/);
211+
t.throws('void main() { int test = .04; }', /Left and right arguments are of differing types/);
212+
t.throws('void main() { int test = 0.50; }', /Left and right arguments are of differing types/);
213+
t.throws('void main() { int test = 55.23; }', /Left and right arguments are of differing types/);
214+
t.throws('void main() { int test = 5e3; }', /Left and right arguments are of differing types/);
215+
t.throws('void main() { int test = 5.5e3; }', /Left and right arguments are of differing types/);
216216
t.throws('void main() { int test = 5.5e-3; }', /Left and right arguments are of differing types/);
217-
t.throws('void main() { int test = .5e3; }', /Left and right arguments are of differing types/);
218-
t.throws('void main() { int test = true; }', /Left and right arguments are of differing types/);
219-
t.throws('void main() { int test = false; }', /Left and right arguments are of differing types/);
217+
t.throws('void main() { int test = .5e3; }', /Left and right arguments are of differing types/);
218+
t.throws('void main() { int test = true; }', /Left and right arguments are of differing types/);
219+
t.throws('void main() { int test = false; }', /Left and right arguments are of differing types/);
220220
t.end()
221221
})
222222

223223

224-
test.skip('should throw on invalid float initializations', function(t) {
225-
t.throws('void main() { float test = 1; }', /Left and right arguments are of differing types/);
226-
t.throws('void main() { float test = 55; }', /Left and right arguments are of differing types/);
227-
t.throws('void main() { float test = 0x23; }', /Left and right arguments are of differing types/);
228-
t.throws('void main() { float test = 023; }', /Left and right arguments are of differing types/);
229-
t.throws('void main() { float test = true; }', /Left and right arguments are of differing types/);
224+
test.skip('should throw on invalid float initializations', function (t) {
225+
t.throws('void main() { float test = 1; }', /Left and right arguments are of differing types/);
226+
t.throws('void main() { float test = 55; }', /Left and right arguments are of differing types/);
227+
t.throws('void main() { float test = 0x23; }', /Left and right arguments are of differing types/);
228+
t.throws('void main() { float test = 023; }', /Left and right arguments are of differing types/);
229+
t.throws('void main() { float test = true; }', /Left and right arguments are of differing types/);
230230
t.throws('void main() { float test = false; }', /Left and right arguments are of differing types/);
231231
t.end()
232232
})
233233

234-
test.skip('should throw on invalid bool initializations', function(t) {
235-
t.throws('void main() { bool test = 1; }', /Left and right arguments are of differing types/);
236-
t.throws('void main() { bool test = 55; }', /Left and right arguments are of differing types/);
237-
t.throws('void main() { bool test = 0x23; }', /Left and right arguments are of differing types/);
238-
t.throws('void main() { bool test = 023; }', /Left and right arguments are of differing types/);
239-
t.throws('void main() { bool test = 1.0; }', /Left and right arguments are of differing types/);
240-
t.throws('void main() { bool test = .04; }', /Left and right arguments are of differing types/);
241-
t.throws('void main() { bool test = 0.50; }', /Left and right arguments are of differing types/);
242-
t.throws('void main() { bool test = 55.23; }', /Left and right arguments are of differing types/);
243-
t.throws('void main() { bool test = 5e3; }', /Left and right arguments are of differing types/);
244-
t.throws('void main() { bool test = 5.5e3; }', /Left and right arguments are of differing types/);
234+
test.skip('should throw on invalid bool initializations', function (t) {
235+
t.throws('void main() { bool test = 1; }', /Left and right arguments are of differing types/);
236+
t.throws('void main() { bool test = 55; }', /Left and right arguments are of differing types/);
237+
t.throws('void main() { bool test = 0x23; }', /Left and right arguments are of differing types/);
238+
t.throws('void main() { bool test = 023; }', /Left and right arguments are of differing types/);
239+
t.throws('void main() { bool test = 1.0; }', /Left and right arguments are of differing types/);
240+
t.throws('void main() { bool test = .04; }', /Left and right arguments are of differing types/);
241+
t.throws('void main() { bool test = 0.50; }', /Left and right arguments are of differing types/);
242+
t.throws('void main() { bool test = 55.23; }', /Left and right arguments are of differing types/);
243+
t.throws('void main() { bool test = 5e3; }', /Left and right arguments are of differing types/);
244+
t.throws('void main() { bool test = 5.5e3; }', /Left and right arguments are of differing types/);
245245
t.throws('void main() { bool test = 5.5e-3; }', /Left and right arguments are of differing types/);
246-
t.throws('void main() { bool test = .5e3; }', /Left and right arguments are of differing types/);
246+
t.throws('void main() { bool test = .5e3; }', /Left and right arguments are of differing types/);
247247
t.end()
248248
})
249249

250250

251251

252-
test('should default ints to 0', function(t) {
252+
test('should default ints to 0', function (t) {
253253
t.equal(
254254
clean(compile('void main() { int test; }')),
255255
clean('function main () {\nvar test = 0;\n};'));
@@ -259,23 +259,22 @@ test('should default ints to 0', function(t) {
259259
t.end()
260260
})
261261

262-
test('should default floats to 0.0', function(t) {
262+
test('should default floats to 0.0', function (t) {
263263
t.equal(
264264
clean(compile('void main() { float test; }')),
265265
clean('function main () {\nvar test = 0;\n};'));
266266
t.equal(
267267
clean(compile('void main() { float test, foo; }')),
268-
clean('function main () {\nvar test = 0, foo = 0;\n};'));
268+
clean('function main () {\nvar test = 0, foo = 0;\n};'));
269269
t.end()
270270
})
271271

272-
test('should default bools to 0 (false)', function(t) {
272+
test('should default bools to 0 (false)', function (t) {
273273
t.equal(
274274
clean(compile('void main() { bool test; }')),
275275
clean('function main () {\nvar test = false;\n};'));
276276
t.equal(
277277
clean(compile('void main() { bool test, foo; }')),
278-
clean('function main () {\nvar test = false, foo = false;\n};'));
278+
clean('function main () {\nvar test = false, foo = false;\n};'));
279279
t.end()
280280
})
281-

test/structs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import GLSL from '../index.js'
22
import test from 'tape'
33
import evaluate from './util/eval.js'
4-
import clean from 'cln'
4+
import clean from './util/clean.js'
55

66
var compile = GLSL({})
77

test/util/clean.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function clean(str) {
2+
if (Array.isArray(str)) str = String.raw.apply(String, arguments)
3+
4+
return str.trim()
5+
6+
//remove empty lines
7+
.replace(/^\s*\n/gm, '')
8+
9+
//remove indentation/tabulation
10+
.replace(/^\s*/gm, '')
11+
12+
//transform all \r to \n
13+
.replace(/[\n\r]+/g, '\n')
14+
15+
//replace duble spaces/tabs to single ones
16+
.replace(/(\s)\s+/g, '$1')
17+
}

test/vectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import test from 'tape'
33
import GLSL, { compile } from '../index.js'
44
import almost from './util/almost.js'
55
import evaluate from './util/eval.js'
6-
import clean from 'cln'
6+
import clean from './util/clean.js'
77

88
// constructors
99

0 commit comments

Comments
 (0)