ConstNode.js 2.1 KB

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