Skip to content

Commit

Permalink
Handle unaries properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Jul 8, 2024
1 parent 8554ae8 commit b44b68e
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 64 deletions.
10 changes: 7 additions & 3 deletions lib/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -64,7 +65,6 @@ function processOperation(left, right, operator) {
return res
}


// complex
var outType = rightType;
var vec = right;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({})

Expand Down
2 changes: 1 addition & 1 deletion test/functions.js
Original file line number Diff line number Diff line change
@@ -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'


Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script type="importmap">
{
"imports": {
"tape" : "../node_modules/tape/tape.js",
"tape" : "../node_modules/tape/tape.js"
}
}
</script>
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/preprocessor.js
Original file line number Diff line number Diff line change
@@ -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(`
Expand Down
77 changes: 38 additions & 39 deletions test/primitives.js
Original file line number Diff line number Diff line change
@@ -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({})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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};'));
Expand All @@ -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};'));
Expand Down Expand Up @@ -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};'));
Expand All @@ -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};'));
Expand All @@ -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()
})

2 changes: 1 addition & 1 deletion test/structs.js
Original file line number Diff line number Diff line change
@@ -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({})

Expand Down
17 changes: 17 additions & 0 deletions test/util/clean.js
Original file line number Diff line number Diff line change
@@ -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')
}
2 changes: 1 addition & 1 deletion test/vectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b44b68e

Please sign in to comment.