TSLEncoder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. import { REVISION } from 'three';
  2. import { VariableDeclaration, Accessor } from './AST.js';
  3. import * as Nodes from '../nodes/Nodes.js';
  4. const opLib = {
  5. '=': 'assign',
  6. '+': 'add',
  7. '-': 'sub',
  8. '*': 'mul',
  9. '/': 'div',
  10. '%': 'remainder',
  11. '<': 'lessThan',
  12. '>': 'greaterThan',
  13. '<=': 'lessThanEqual',
  14. '>=': 'greaterThanEqual',
  15. '==': 'equal',
  16. '&&': 'and',
  17. '||': 'or',
  18. '^^': 'xor',
  19. '&': 'bitAnd',
  20. '|': 'bitOr',
  21. '^': 'bitXor',
  22. '<<': 'shiftLeft',
  23. '>>': 'shiftRight',
  24. '+=': 'addAssign',
  25. '-=': 'subAssign',
  26. '*=': 'mulAssign',
  27. '/=': 'divAssign',
  28. '%=': 'remainderAssign',
  29. '^=': 'bitXorAssign',
  30. '&=': 'bitAndAssign',
  31. '|=': 'bitOrAssign',
  32. '<<=': 'shiftLeftAssign',
  33. '>>=': 'shiftRightAssign'
  34. };
  35. const unaryLib = {
  36. '+': '', // positive
  37. '-': 'negate',
  38. '~': 'bitNot',
  39. '!': 'not',
  40. '++': 'increment', // incrementBefore
  41. '--': 'decrement' // decrementBefore
  42. };
  43. const isPrimitive = ( value ) => /^(true|false|-?\d)/.test( value );
  44. class TSLEncoder {
  45. constructor() {
  46. this.tab = '';
  47. this.imports = new Set();
  48. this.global = new Set();
  49. this.overloadings = new Map();
  50. this.iife = false;
  51. this.uniqueNames = false;
  52. this.reference = false;
  53. this._currentProperties = {};
  54. this._lastStatement = null;
  55. }
  56. addImport( name ) {
  57. // import only if it's a node
  58. name = name.split( '.' )[ 0 ];
  59. if ( Nodes[ name ] !== undefined && this.global.has( name ) === false && this._currentProperties[ name ] === undefined ) {
  60. this.imports.add( name );
  61. }
  62. }
  63. emitUniform( node ) {
  64. let code = `const ${ node.name } = `;
  65. if ( this.reference === true ) {
  66. this.addImport( 'reference' );
  67. this.global.add( node.name );
  68. //code += `reference( '${ node.name }', '${ node.type }', uniforms )`;
  69. // legacy
  70. code += `reference( 'value', '${ node.type }', uniforms[ '${ node.name }' ] )`;
  71. } else {
  72. this.addImport( 'uniform' );
  73. this.global.add( node.name );
  74. code += `uniform( '${ node.type }' )`;
  75. }
  76. return code;
  77. }
  78. emitExpression( node ) {
  79. let code;
  80. /*@TODO: else if ( node.isVarying ) {
  81. code = this.emitVarying( node );
  82. }*/
  83. if ( node.isAccessor ) {
  84. this.addImport( node.property );
  85. code = node.property;
  86. } else if ( node.isNumber ) {
  87. if ( node.type === 'int' || node.type === 'uint' ) {
  88. code = node.type + '( ' + node.value + ' )';
  89. this.addImport( node.type );
  90. } else {
  91. code = node.value;
  92. }
  93. } else if ( node.isString ) {
  94. code = '\'' + node.value + '\'';
  95. } else if ( node.isOperator ) {
  96. const opFn = opLib[ node.type ] || node.type;
  97. const left = this.emitExpression( node.left );
  98. const right = this.emitExpression( node.right );
  99. if ( isPrimitive( left ) && isPrimitive( right ) ) {
  100. return left + ' ' + node.type + ' ' + right;
  101. }
  102. if ( isPrimitive( left ) ) {
  103. code = opFn + '( ' + left + ', ' + right + ' )';
  104. this.addImport( opFn );
  105. } else {
  106. code = left + '.' + opFn + '( ' + right + ' )';
  107. }
  108. } else if ( node.isFunctionCall ) {
  109. const params = [];
  110. for ( const parameter of node.params ) {
  111. params.push( this.emitExpression( parameter ) );
  112. }
  113. this.addImport( node.name );
  114. const paramsStr = params.length > 0 ? ' ' + params.join( ', ' ) + ' ' : '';
  115. code = `${ node.name }(${ paramsStr })`;
  116. } else if ( node.isReturn ) {
  117. code = 'return';
  118. if ( node.value ) {
  119. code += ' ' + this.emitExpression( node.value );
  120. }
  121. } else if ( node.isAccessorElements ) {
  122. code = node.property;
  123. for ( const element of node.elements ) {
  124. if ( element.isStaticElement ) {
  125. code += '.' + this.emitExpression( element.value );
  126. } else if ( element.isDynamicElement ) {
  127. const value = this.emitExpression( element.value );
  128. if ( isPrimitive( value ) ) {
  129. code += `[ ${ value } ]`;
  130. } else {
  131. code += `.element( ${ value } )`;
  132. }
  133. }
  134. }
  135. } else if ( node.isDynamicElement ) {
  136. code = this.emitExpression( node.value );
  137. } else if ( node.isStaticElement ) {
  138. code = this.emitExpression( node.value );
  139. } else if ( node.isFor ) {
  140. code = this.emitFor( node );
  141. } else if ( node.isVariableDeclaration ) {
  142. code = this.emitVariables( node );
  143. } else if ( node.isUniform ) {
  144. code = this.emitUniform( node );
  145. } else if ( node.isTernary ) {
  146. code = this.emitTernary( node );
  147. } else if ( node.isConditional ) {
  148. code = this.emitConditional( node );
  149. } else if ( node.isUnary && node.expression.isNumber ) {
  150. code = node.type + ' ' + node.expression.value;
  151. } else if ( node.isUnary ) {
  152. let type = unaryLib[ node.type ];
  153. if ( node.after === false && ( node.type === '++' || node.type === '--' ) ) {
  154. type += 'Before';
  155. }
  156. const exp = this.emitExpression( node.expression );
  157. if ( isPrimitive( exp ) ) {
  158. this.addImport( type );
  159. code = type + '( ' + exp + ' )';
  160. } else {
  161. code = exp + '.' + type + '()';
  162. }
  163. } else {
  164. console.warn( 'Unknown node type', node );
  165. }
  166. if ( ! code ) code = '/* unknown statement */';
  167. return code;
  168. }
  169. emitBody( body ) {
  170. this.setLastStatement( null );
  171. let code = '';
  172. this.tab += '\t';
  173. for ( const statement of body ) {
  174. code += this.emitExtraLine( statement );
  175. code += this.tab + this.emitExpression( statement );
  176. if ( code.slice( - 1 ) !== '}' ) code += ';';
  177. code += '\n';
  178. this.setLastStatement( statement );
  179. }
  180. code = code.slice( 0, - 1 ); // remove the last extra line
  181. this.tab = this.tab.slice( 0, - 1 );
  182. return code;
  183. }
  184. emitTernary( node ) {
  185. const condStr = this.emitExpression( node.cond );
  186. const leftStr = this.emitExpression( node.left );
  187. const rightStr = this.emitExpression( node.right );
  188. this.addImport( 'cond' );
  189. return `cond( ${ condStr }, ${ leftStr }, ${ rightStr } )`;
  190. }
  191. emitConditional( node ) {
  192. const condStr = this.emitExpression( node.cond );
  193. const bodyStr = this.emitBody( node.body );
  194. let ifStr = `If( ${ condStr }, () => {
  195. ${ bodyStr }
  196. ${ this.tab }} )`;
  197. let current = node;
  198. while ( current.elseConditional ) {
  199. const elseBodyStr = this.emitBody( current.elseConditional.body );
  200. if ( current.elseConditional.cond ) {
  201. const elseCondStr = this.emitExpression( current.elseConditional.cond );
  202. ifStr += `.elseif( ${ elseCondStr }, () => {
  203. ${ elseBodyStr }
  204. ${ this.tab }} )`;
  205. } else {
  206. ifStr += `.else( () => {
  207. ${ elseBodyStr }
  208. ${ this.tab }} )`;
  209. }
  210. current = current.elseConditional;
  211. }
  212. this.imports.add( 'If' );
  213. return ifStr;
  214. }
  215. emitLoop( node ) {
  216. const start = this.emitExpression( node.initialization.value );
  217. const end = this.emitExpression( node.condition.right );
  218. const name = node.initialization.name;
  219. const type = node.initialization.type;
  220. const condition = node.condition.type;
  221. const update = node.afterthought.type;
  222. const nameParam = name !== 'i' ? `, name: '${ name }'` : '';
  223. const typeParam = type !== 'int' ? `, type: '${ type }'` : '';
  224. const conditionParam = condition !== '<' ? `, condition: '${ condition }'` : '';
  225. const updateParam = update !== '++' ? `, update: '${ update }'` : '';
  226. let loopStr = `loop( { start: ${ start }, end: ${ end + nameParam + typeParam + conditionParam + updateParam } }, ( { ${ name } } ) => {\n\n`;
  227. loopStr += this.emitBody( node.body ) + '\n\n';
  228. loopStr += this.tab + '} )';
  229. this.imports.add( 'loop' );
  230. return loopStr;
  231. }
  232. emitFor( node ) {
  233. const { initialization, condition, afterthought } = node;
  234. if ( ( initialization && initialization.isVariableDeclaration && initialization.next === null ) &&
  235. ( condition && condition.left.isAccessor && condition.left.property === initialization.name ) &&
  236. ( afterthought && afterthought.isUnary ) &&
  237. ( initialization.name === afterthought.expression.property )
  238. ) {
  239. return this.emitLoop( node );
  240. }
  241. return this.emitForWhile( node );
  242. }
  243. emitForWhile( node ) {
  244. const initialization = this.emitExpression( node.initialization );
  245. const condition = this.emitExpression( node.condition );
  246. const afterthought = this.emitExpression( node.afterthought );
  247. this.tab += '\t';
  248. let forStr = '{\n\n' + this.tab + initialization + ';\n\n';
  249. forStr += `${ this.tab }While( ${ condition }, () => {\n\n`;
  250. forStr += this.emitBody( node.body ) + '\n\n';
  251. forStr += this.tab + '\t' + afterthought + ';\n\n';
  252. forStr += this.tab + '} )\n\n';
  253. this.tab = this.tab.slice( 0, - 1 );
  254. forStr += this.tab + '}';
  255. this.imports.add( 'While' );
  256. return forStr;
  257. }
  258. emitVariables( node, isRoot = true ) {
  259. const { name, type, value, next } = node;
  260. const valueStr = value ? this.emitExpression( value ) : '';
  261. let varStr = isRoot ? 'const ' : '';
  262. varStr += name;
  263. if ( value ) {
  264. if ( value.isFunctionCall && value.name === type ) {
  265. varStr += ' = ' + valueStr;
  266. } else {
  267. varStr += ` = ${ type }( ${ valueStr } )`;
  268. }
  269. } else {
  270. varStr += ` = ${ type }()`;
  271. }
  272. if ( node.immutable === false ) {
  273. varStr += '.toVar()';
  274. }
  275. if ( next ) {
  276. varStr += ', ' + this.emitVariables( next, false );
  277. }
  278. this.addImport( type );
  279. return varStr;
  280. }
  281. /*emitVarying( node ) { }*/
  282. emitOverloadingFunction( nodes ) {
  283. const { name } = nodes[ 0 ];
  284. this.addImport( 'overloadingFn' );
  285. return `export const ${ name } = /*#__PURE__*/ overloadingFn( [ ${ nodes.map( node => node.name + '_' + nodes.indexOf( node ) ).join( ', ' ) } ] );\n`;
  286. }
  287. emitFunction( node ) {
  288. const { name, type } = node;
  289. this._currentProperties = { name: node };
  290. const params = [];
  291. const inputs = [];
  292. const mutableParams = [];
  293. let hasPointer = false;
  294. for ( const param of node.params ) {
  295. let str = `{ name: '${ param.name }', type: '${ param.type }'`;
  296. let name = param.name;
  297. if ( param.immutable === false && ( param.qualifier !== 'inout' && param.qualifier !== 'out' ) ) {
  298. name = name + '_immutable';
  299. mutableParams.push( param );
  300. }
  301. if ( param.qualifier ) {
  302. if ( param.qualifier === 'inout' || param.qualifier === 'out' ) {
  303. hasPointer = true;
  304. }
  305. str += ', qualifier: \'' + param.qualifier + '\'';
  306. }
  307. inputs.push( str + ' }' );
  308. params.push( name );
  309. this._currentProperties[ name ] = param;
  310. }
  311. for ( const param of mutableParams ) {
  312. node.body.unshift( new VariableDeclaration( param.type, param.name, new Accessor( param.name + '_immutable' ) ) );
  313. }
  314. const paramsStr = params.length > 0 ? ' [ ' + params.join( ', ' ) + ' ] ' : '';
  315. const bodyStr = this.emitBody( node.body );
  316. let fnName = name;
  317. let overloadingNodes = null;
  318. if ( this.overloadings.has( name ) ) {
  319. const overloadings = this.overloadings.get( name );
  320. if ( overloadings.length > 1 ) {
  321. const index = overloadings.indexOf( node );
  322. fnName += '_' + index;
  323. if ( index === overloadings.length - 1 ) {
  324. overloadingNodes = overloadings;
  325. }
  326. }
  327. }
  328. let funcStr = `export const ${ fnName } = /*#__PURE__*/ tslFn( (${ paramsStr }) => {
  329. ${ bodyStr }
  330. ${ this.tab }} )`;
  331. const layoutInput = inputs.length > 0 ? '\n\t\t' + this.tab + inputs.join( ',\n\t\t' + this.tab ) + '\n\t' + this.tab : '';
  332. if ( node.layout !== false && hasPointer === false ) {
  333. const uniqueName = this.uniqueNames ? fnName + '_' + Math.random().toString( 36 ).slice( 2 ) : fnName;
  334. funcStr += `.setLayout( {
  335. ${ this.tab }\tname: '${ uniqueName }',
  336. ${ this.tab }\ttype: '${ type }',
  337. ${ this.tab }\tinputs: [${ layoutInput }]
  338. ${ this.tab }} )`;
  339. }
  340. funcStr += ';\n';
  341. this.imports.add( 'tslFn' );
  342. this.global.add( node.name );
  343. if ( overloadingNodes !== null ) {
  344. funcStr += '\n' + this.emitOverloadingFunction( overloadingNodes );
  345. }
  346. return funcStr;
  347. }
  348. setLastStatement( statement ) {
  349. this._lastStatement = statement;
  350. }
  351. emitExtraLine( statement ) {
  352. const last = this._lastStatement;
  353. if ( last === null ) return '';
  354. if ( statement.isReturn ) return '\n';
  355. const isExpression = ( st ) => st.isFunctionDeclaration !== true && st.isFor !== true && st.isConditional !== true;
  356. const lastExp = isExpression( last );
  357. const currExp = isExpression( statement );
  358. if ( lastExp !== currExp || ( ! lastExp && ! currExp ) ) return '\n';
  359. return '';
  360. }
  361. emit( ast ) {
  362. let code = '\n';
  363. if ( this.iife ) this.tab += '\t';
  364. const overloadings = this.overloadings;
  365. for ( const statement of ast.body ) {
  366. if ( statement.isFunctionDeclaration ) {
  367. if ( overloadings.has( statement.name ) === false ) {
  368. overloadings.set( statement.name, [] );
  369. }
  370. overloadings.get( statement.name ).push( statement );
  371. }
  372. }
  373. for ( const statement of ast.body ) {
  374. code += this.emitExtraLine( statement );
  375. if ( statement.isFunctionDeclaration ) {
  376. code += this.tab + this.emitFunction( statement );
  377. } else {
  378. code += this.tab + this.emitExpression( statement ) + ';\n';
  379. }
  380. this.setLastStatement( statement );
  381. }
  382. const imports = [ ...this.imports ];
  383. let header = '// Three.js Transpiler r' + REVISION + '\n\n';
  384. let footer = '';
  385. if ( this.iife ) {
  386. header += '( function ( TSL, uniforms ) {\n\n';
  387. header += imports.length > 0 ? '\tconst { ' + imports.join( ', ' ) + ' } = TSL;\n' : '';
  388. footer += '\n} );';
  389. } else {
  390. header += imports.length > 0 ? 'import { ' + imports.join( ', ' ) + ' } from \'three/nodes\';\n' : '';
  391. }
  392. return header + code + footer;
  393. }
  394. }
  395. export default TSLEncoder;