123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import TempNode from '../core/TempNode.js';
- class OperatorNode extends TempNode {
- constructor( op, aNode, bNode, ...params ) {
- super();
- this.op = op;
- if ( params.length > 0 ) {
- let finalBNode = bNode;
- for ( let i = 0; i < params.length; i ++ ) {
- finalBNode = new OperatorNode( op, finalBNode, params[ i ] );
- }
- bNode = finalBNode;
- }
- this.aNode = aNode;
- this.bNode = bNode;
- }
- getNodeType( builder, output ) {
- const op = this.op;
- const aNode = this.aNode;
- const bNode = this.bNode;
- const typeA = aNode.getNodeType( builder );
- const typeB = bNode.getNodeType( builder );
- if ( typeA === 'void' || typeB === 'void' ) {
- return 'void';
- } else if ( op === '=' ) {
- return typeA;
- } else if ( op === '==' || op === '&&' ) {
- return 'bool';
- } else if ( op === '<=' || op === '>' ) {
- const length = builder.getTypeLength( output );
- return length > 1 ? `bvec${ length }` : 'bool';
- } else {
- if ( typeA === 'float' && builder.isMatrix( typeB ) ) {
- return typeB;
- } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
- // matrix x vector
- return builder.getVectorFromMatrix( typeA );
- } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
- // vector x matrix
- return builder.getVectorFromMatrix( typeB );
- } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
- // anytype x anytype: use the greater length vector
- return typeB;
- }
- return typeA;
- }
- }
- generate( builder, output ) {
- const op = this.op;
- const aNode = this.aNode;
- const bNode = this.bNode;
- const type = this.getNodeType( builder, output );
- let typeA = null;
- let typeB = null;
- if ( type !== 'void' ) {
- typeA = aNode.getNodeType( builder );
- typeB = bNode.getNodeType( builder );
- if ( op === '=' ) {
- typeB = typeA;
- } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
- // matrix x vector
- typeB = builder.getVectorFromMatrix( typeA );
- } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
- // vector x matrix
- typeA = builder.getVectorFromMatrix( typeB );
- } else {
- // anytype x anytype
- typeA = typeB = type;
- }
- } else {
- typeA = typeB = type;
- }
- const a = aNode.build( builder, typeA );
- const b = bNode.build( builder, typeB );
- const outputLength = builder.getTypeLength( output );
- if ( output !== 'void' ) {
- if ( op === '=' ) {
- builder.addFlowCode( `${a} ${this.op} ${b}` );
- return a;
- } else if ( op === '>' && outputLength > 1 ) {
- return `greaterThan( ${a}, ${b} )`;
- } else if ( op === '<=' && outputLength > 1 ) {
- return `lessThanEqual( ${a}, ${b} )`;
- } else {
- return `( ${a} ${this.op} ${b} )`;
- }
- } else if ( typeA !== 'void' ) {
- return `${a} ${this.op} ${b}`;
- }
- }
- }
- export default OperatorNode;
|