Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft. flo-bit.dev/shapecraft
Something went wrong. Try again.
3.2 kB · 156 lines
JavaScript
at commit 049ef598
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157import Node from '../core/Node.js';import { property } from '../core/PropertyNode.js';import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
class ConditionalNode extends Node {
static get type() {
return 'ConditionalNode';
}
constructor( condNode, ifNode, elseNode = null ) {
super();
this.condNode = condNode;
this.ifNode = ifNode; this.elseNode = elseNode;
}
getNodeType( builder ) {
const ifType = this.ifNode.getNodeType( builder );
if ( this.elseNode !== null ) {
const elseType = this.elseNode.getNodeType( builder );
if ( builder.getTypeLength( elseType ) > builder.getTypeLength( ifType ) ) {
return elseType;
}
}
return ifType;
}
setup( builder ) {
const condNode = this.condNode.cache(); const ifNode = this.ifNode.cache(); const elseNode = this.elseNode ? this.elseNode.cache() : null;
//
const currentNodeBlock = builder.context.nodeBlock;
builder.getDataFromNode( ifNode ).parentNodeBlock = currentNodeBlock; if ( elseNode !== null ) builder.getDataFromNode( elseNode ).parentNodeBlock = currentNodeBlock;
//
const properties = builder.getNodeProperties( this ); properties.condNode = condNode; properties.ifNode = ifNode.context( { nodeBlock: ifNode } ); properties.elseNode = elseNode ? elseNode.context( { nodeBlock: elseNode } ) : null;
}
generate( builder, output ) {
const type = this.getNodeType( builder );
const nodeData = builder.getDataFromNode( this );
if ( nodeData.nodeProperty !== undefined ) {
return nodeData.nodeProperty;
}
const { condNode, ifNode, elseNode } = builder.getNodeProperties( this );
const needsOutput = output !== 'void'; const nodeProperty = needsOutput ? property( type ).build( builder ) : '';
nodeData.nodeProperty = nodeProperty;
const nodeSnippet = condNode.build( builder, 'bool' );
builder.addFlowCode( `\n${ builder.tab }if ( ${ nodeSnippet } ) {\n\n` ).addFlowTab();
let ifSnippet = ifNode.build( builder, type );
if ( ifSnippet ) {
if ( needsOutput ) {
ifSnippet = nodeProperty + ' = ' + ifSnippet + ';';
} else {
ifSnippet = 'return ' + ifSnippet + ';';
}
}
builder.removeFlowTab().addFlowCode( builder.tab + '\t' + ifSnippet + '\n\n' + builder.tab + '}' );
if ( elseNode !== null ) {
builder.addFlowCode( ' else {\n\n' ).addFlowTab();
let elseSnippet = elseNode.build( builder, type );
if ( elseSnippet ) {
if ( needsOutput ) {
elseSnippet = nodeProperty + ' = ' + elseSnippet + ';';
} else {
elseSnippet = 'return ' + elseSnippet + ';';
}
}
builder.removeFlowTab().addFlowCode( builder.tab + '\t' + elseSnippet + '\n\n' + builder.tab + '}\n\n' );
} else {
builder.addFlowCode( '\n\n' );
}
return builder.format( nodeProperty, type, output );
}
}
export default ConditionalNode;
export const select = /*@__PURE__*/ nodeProxy( ConditionalNode );
addMethodChaining( 'select', select );
//
export const cond = ( ...params ) => { // @deprecated, r168
console.warn( 'TSL.ConditionalNode: cond() has been renamed to select().' ); return select( ...params );
};
addMethodChaining( 'cond', cond );