Skip to content

Commit a284825

Browse files
authored
Merge pull request #6 from axion014/minify-groups
Get rid of unnecessary grouping operators
2 parents 8d547f5 + df0c7a6 commit a284825

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

index.js

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var lang = require('cssauron-glsl')
77
, through = require('through')
88
, shortest = require('shortest')
99

10+
// recognize commutative + but not * as these are conditional
11+
var commutative_operators = ['+', '&&', '||']
12+
1013
function minifier(safe_words, mutate_storages) {
1114
safe_words = safe_words || ['main']
1215

@@ -23,6 +26,18 @@ function minifier(safe_words, mutate_storages) {
2326
return through(mutate)
2427

2528
function mutate(node) {
29+
// remove unnecessary grouping operators
30+
if(is_unnecessary_group(node)) return
31+
32+
if(node.parent) {
33+
for(var current = node.parent; current.parent; current = current.parent) {
34+
if(is_unnecessary_group(current)) {
35+
current.parent.children[current.parent.children.indexOf(current)] = current.children[0]
36+
current.children[0].parent = current.parent
37+
}
38+
}
39+
}
40+
2641
// vec2(1.0, 1.0) => vec2(1.0)
2742
if(node.parent && is_redundant_vector_literal(node.parent) && node.parent.children.indexOf(node) > 1) return
2843
if(is_redundant_vector_literal(node)) node.children = node.children.slice(0, 2)
@@ -56,6 +71,17 @@ function minifier(safe_words, mutate_storages) {
5671
!safe_words.hasOwnProperty(node.token.data)
5772
}
5873

74+
function is_unnecessary_group(node) {
75+
if(node.type !== 'group') return false
76+
if(node.children[0].lbp > node.parent.lbp) return true
77+
if(node.children[0].lbp === node.parent.lbp) {
78+
for(var i = 0; i < commutative_operators.length; i++) {
79+
if(node.parent.data === commutative_operators[i] && node.children[0].data === commutative_operators[i]) return true
80+
}
81+
}
82+
return false
83+
}
84+
5985
function is_redundant_vector_literal(node) {
6086
if(node.type === 'call' && /^[ib]?vec[234]$/.test(node.children[0].data) &&
6187
(node.children[1].type === 'literal' || node.children[1].type === 'ident')) {

tap-snapshots/test-basic.js-TAP.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,14 @@ float f = .1;
736736
float g = .01;
737737
`
738738

739+
exports[`test/basic.js TAP grouping removal test > output 1`] = `
740+
741+
float a = 2e10 + .2e2 + 1.e3 * (0xFaBc09 + 3);
742+
float b = 2e10 + (.2e2 - 1.e3 / (0xFaBc09 + 3));
743+
bool c = 1. && true && true;
744+
bool d = 0. || false || true;
745+
`
746+
739747
exports[`test/basic.js TAP vec shorthand > output 1`] = `
740748
741749
vec2 vec2Long = vec2(.0, 1.);

test/basic.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const path = require("path");
99
const through = require("through");
1010

1111
const zeroGLSL = path.resolve(__dirname, "./zero-decimals.glsl");
12+
const commutativeGLSL = path.resolve(__dirname, "./commutative-operators.glsl");
1213
const workingGLSL = path.resolve(__dirname, "./working.glsl");
1314
const vecGLSL = path.resolve(__dirname, "./vec-shorthand.glsl");
1415

@@ -85,7 +86,7 @@ tap.test("decimals starting or ending with 0", t => {
8586
fs.createReadStream(zeroGLSL)
8687
.pipe(tokenizer())
8788
.pipe(parser())
88-
.pipe(minify(["main"], true))
89+
.pipe(minify())
8990
.pipe(deparser())
9091
.pipe(endStream);
9192
});
@@ -137,3 +138,22 @@ tap.test("vec shorthand", t => {
137138
.pipe(deparser())
138139
.pipe(endStream);
139140
});
141+
142+
tap.test("grouping removal test", t => {
143+
let output = "";
144+
145+
const endStream = through((data) => {
146+
output += data;
147+
}, () => {
148+
t.matchSnapshot(output, "output");
149+
t.end();
150+
});
151+
152+
fs.createReadStream(commutativeGLSL)
153+
.pipe(tokenizer())
154+
.pipe(parser())
155+
.pipe(minify())
156+
.pipe(deparser())
157+
.pipe(endStream);
158+
});
159+

test/commutative-operators.glsl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
float x = 2e10 + (.2e2 + (1.e3 * (0xFaBc09 + 3)));
2+
float y = 2e10 + (.2e2 - (1.e3 / (0xFaBc09 + 3)));
3+
4+
bool a = 1. && (true && true);
5+
6+
bool b = 0. || (false || true);

test/test.glsl

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int first, second, third, fourth, fifth, sixth, seventh, eigth;
5656
vPosition = position;
5757
vec3 thing = vec2(1., 2.);
5858
int v_thing, garybusey;
59-
thing.rgba = 2e10 + .2e2 + 1.e3 * 0xFaBc09 + (3 * v_thing);
59+
thing.rgba = 2e10 + (.2e2 + (1.e3 * 0xFaBc09 + (3 * v_thing)));
6060

6161
for(xxx i = 0; i < 10; ++i) {
6262
discard;

0 commit comments

Comments
 (0)