ConstNode.js 1.9 KB

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