Skip to content

Commit 919a75a

Browse files
authored
Merge pull request #37 from Pessimistress/matrix-multiplication-fix
Chained multiplication bug fix
2 parents ea49924 + 458fcd9 commit 919a75a

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/operators.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ var opComplexity = {
4444

4545
var opsRE = /\*|\+|\-|\/|\%|\<|\=|\>|\&|\||\!|\^|\~/;
4646

47+
/**
48+
* Check if expression is protected by parentheses
49+
*/
50+
function isProtected (exp) {
51+
if (/\s/.test(exp)) {
52+
// a + b
53+
if (exp[0] !== '(') return false;
54+
55+
var level = 1;
56+
var len = exp.length;
57+
for (let i = 1; i < len - 1; i++) {
58+
if (exp[i] === '(') level++;
59+
else if (exp[i] === ')') level--;
60+
// (a + b) * c + d
61+
if (level === 0) return false;
62+
}
63+
}
64+
return true;
65+
}
4766

4867
/**
4968
* Return rendered operation
@@ -287,15 +306,15 @@ function processOperation (left, right, operator) {
287306

288307
// embrace complex operators
289308
if (operator != '+' && operator != '-') {
290-
if (/\s/.test(left) && left[0] != '(' && left[left.length - 1] != ')') opResult += '(' + left + ')'
309+
if (!isProtected(left)) opResult += '(' + left + ')'
291310
else opResult += left
292311
}
293312
else opResult += left
294313

295314
opResult += ' ' + operator + ' '
296315

297316
if (operator != '+' && operator != '-') {
298-
if (/\s/.test(right) && right[0] != '(' && right[right.length - 1] != ')') opResult += '(' + right + ')'
317+
if (!isProtected(right)) opResult += '(' + right + ')'
299318
else opResult += right
300319
}
301320
else opResult += right

test/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,21 @@ test('Vec/matrix operators', function () {
927927
var a = [1, 0, 0, 1], b = [1, 0, 0, 1], c = [1, 0, 0, 1];
928928
gl_Position = [(a[0] * b[0] + a[2] * b[1]) * c[0] + (a[0] * b[2] + a[2] * b[3]) * c[1], (a[1] * b[0] + a[3] * b[1]) * c[0] + (a[1] * b[2] + a[3] * b[3]) * c[1], (a[0] * b[0] + a[2] * b[1]) * c[2] + (a[0] * b[2] + a[2] * b[3]) * c[3], (a[1] * b[0] + a[3] * b[1]) * c[2] + (a[1] * b[2] + a[3] * b[3]) * c[3]];
929929
`))
930-
})
930+
});
931+
932+
test(`mat * mat * mat * vec`, function () {
933+
var compile = GLSL({includes: false});
934+
935+
assert.equal(clean(compile(`
936+
mat2 a, b, c;
937+
vec2 d;
938+
gl_Position = a * b * c * d;
939+
`)), clean(`
940+
var a = [1, 0, 0, 1], b = [1, 0, 0, 1], c = [1, 0, 0, 1];
941+
var d = [0, 0];
942+
gl_Position = [((a[0] * b[0] + a[2] * b[1]) * c[0] + (a[0] * b[2] + a[2] * b[3]) * c[1]) * d[0] + ((a[0] * b[0] + a[2] * b[1]) * c[2] + (a[0] * b[2] + a[2] * b[3]) * c[3]) * d[1], ((a[1] * b[0] + a[3] * b[1]) * c[0] + (a[1] * b[2] + a[3] * b[3]) * c[1]) * d[0] + ((a[1] * b[0] + a[3] * b[1]) * c[2] + (a[1] * b[2] + a[3] * b[3]) * c[3]) * d[1]];
943+
`))
944+
});
931945
});
932946

933947

0 commit comments

Comments
 (0)