ConstNode.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { TempNode } from './TempNode.js';
  2. var declarationRegexp = /^([a-z_0-9]+)\s([a-z_0-9]+)\s?\=?\s?(.*?)(\;|$)/i;
  3. function ConstNode( src, useDefine ) {
  4. TempNode.call( this );
  5. this.parse( src || ConstNode.PI, useDefine );
  6. }
  7. ConstNode.PI = 'PI';
  8. ConstNode.PI2 = 'PI2';
  9. ConstNode.RECIPROCAL_PI = 'RECIPROCAL_PI';
  10. ConstNode.RECIPROCAL_PI2 = 'RECIPROCAL_PI2';
  11. ConstNode.LOG2 = 'LOG2';
  12. ConstNode.EPSILON = 'EPSILON';
  13. ConstNode.prototype = Object.create( TempNode.prototype );
  14. ConstNode.prototype.constructor = ConstNode;
  15. ConstNode.prototype.nodeType = 'Const';
  16. ConstNode.prototype.getType = function ( builder ) {
  17. return builder.getTypeByFormat( this.type );
  18. };
  19. ConstNode.prototype.parse = function ( src, useDefine ) {
  20. this.src = src || '';
  21. var name, type, value = '';
  22. var match = this.src.match( declarationRegexp );
  23. this.useDefine = useDefine || this.src.charAt( 0 ) === '#';
  24. if ( match && match.length > 1 ) {
  25. type = match[ 1 ];
  26. name = match[ 2 ];
  27. value = match[ 3 ];
  28. } else {
  29. name = this.src;
  30. type = 'f';
  31. }
  32. this.name = name;
  33. this.type = type;
  34. this.value = value;
  35. };
  36. ConstNode.prototype.build = function ( builder, output ) {
  37. if ( output === 'source' ) {
  38. if ( this.value ) {
  39. if ( this.useDefine ) {
  40. return '#define ' + this.name + ' ' + this.value;
  41. }
  42. return 'const ' + this.type + ' ' + this.name + ' = ' + this.value + ';';
  43. } else if ( this.useDefine ) {
  44. return this.src;
  45. }
  46. } else {
  47. builder.include( this );
  48. return builder.format( this.name, this.getType( builder ), output );
  49. }
  50. };
  51. ConstNode.prototype.generate = function ( builder, output ) {
  52. return builder.format( this.name, this.getType( builder ), output );
  53. };
  54. ConstNode.prototype.copy = function ( source ) {
  55. TempNode.prototype.copy.call( this, source );
  56. this.parse( source.src, source.useDefine );
  57. return this;
  58. };
  59. ConstNode.prototype.toJSON = function ( meta ) {
  60. var data = this.getJSONNode( meta );
  61. if ( ! data ) {
  62. data = this.createJSONNode( meta );
  63. data.src = this.src;
  64. if ( data.useDefine === true ) data.useDefine = true;
  65. }
  66. return data;
  67. };
  68. export { ConstNode };