AST.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. import { toFloatType } from './TranspilerUtils.js';
  2. export class ASTNode {
  3. constructor() {
  4. this.isASTNode = true;
  5. this.linker = {
  6. reference: null,
  7. accesses: [],
  8. assignments: []
  9. };
  10. this.parent = null;
  11. }
  12. get isNumericExpression() {
  13. return false;
  14. }
  15. get hasAssignment() {
  16. if ( this.isAssignment === true ) {
  17. return true;
  18. }
  19. if ( this.parent === null ) {
  20. return false;
  21. }
  22. return this.parent.hasAssignment;
  23. }
  24. getType() {
  25. return this.type || null;
  26. }
  27. getParent( parents = [] ) {
  28. if ( this.parent === null ) {
  29. return parents;
  30. }
  31. parents.push( this.parent );
  32. return this.parent.getParent( parents );
  33. }
  34. initialize() {
  35. for ( const key in this ) {
  36. if ( this[ key ] && this[ key ].isASTNode ) {
  37. this[ key ].parent = this;
  38. } else if ( Array.isArray( this[ key ] ) ) {
  39. const array = this[ key ];
  40. for ( const item of array ) {
  41. if ( item && item.isASTNode ) {
  42. item.parent = this;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. export class Comment extends ASTNode {
  50. constructor( comment ) {
  51. super();
  52. this.comment = comment;
  53. this.isComment = true;
  54. this.initialize();
  55. }
  56. }
  57. export class Program extends ASTNode {
  58. constructor( body = [] ) {
  59. super();
  60. this.body = body;
  61. this.isProgram = true;
  62. this.initialize();
  63. }
  64. }
  65. export class VariableDeclaration extends ASTNode {
  66. constructor( type, name, value = null, next = null, immutable = false ) {
  67. super();
  68. this.type = type;
  69. this.name = name;
  70. this.value = value;
  71. this.next = next;
  72. this.immutable = immutable;
  73. this.isVariableDeclaration = true;
  74. this.initialize();
  75. }
  76. get isAssignment() {
  77. return this.value !== null;
  78. }
  79. }
  80. export class Uniform extends ASTNode {
  81. constructor( type, name ) {
  82. super();
  83. this.type = type;
  84. this.name = name;
  85. this.isUniform = true;
  86. this.initialize();
  87. }
  88. }
  89. export class Varying extends ASTNode {
  90. constructor( type, name ) {
  91. super();
  92. this.type = type;
  93. this.name = name;
  94. this.isVarying = true;
  95. this.initialize();
  96. }
  97. }
  98. export class FunctionParameter extends ASTNode {
  99. constructor( type, name, qualifier = null, immutable = true ) {
  100. super();
  101. this.type = type;
  102. this.name = name;
  103. this.qualifier = qualifier;
  104. this.immutable = immutable;
  105. this.isFunctionParameter = true;
  106. this.initialize();
  107. }
  108. }
  109. export class FunctionDeclaration extends ASTNode {
  110. constructor( type, name, params = [], body = [] ) {
  111. super();
  112. this.type = type;
  113. this.name = name;
  114. this.params = params;
  115. this.body = body;
  116. this.isFunctionDeclaration = true;
  117. this.initialize();
  118. }
  119. }
  120. export class Expression extends ASTNode {
  121. constructor( expression ) {
  122. super();
  123. this.expression = expression;
  124. this.isExpression = true;
  125. this.initialize();
  126. }
  127. }
  128. export class Ternary extends ASTNode {
  129. constructor( cond, left, right ) {
  130. super();
  131. this.cond = cond;
  132. this.left = left;
  133. this.right = right;
  134. this.isTernary = true;
  135. this.initialize();
  136. }
  137. }
  138. export class Operator extends ASTNode {
  139. constructor( type, left, right ) {
  140. super();
  141. this.type = type;
  142. this.left = left;
  143. this.right = right;
  144. this.isOperator = true;
  145. this.initialize();
  146. }
  147. get isAssignment() {
  148. return /^(=|\+=|-=|\*=|\/=|%=|<<=|>>=|>>>=|&=|\^=|\|=)$/.test( this.type );
  149. }
  150. get isNumericExpression() {
  151. if ( this.left.isNumericExpression && this.right.isNumericExpression ) {
  152. return true;
  153. }
  154. return false;
  155. }
  156. getType() {
  157. const leftType = this.left.getType();
  158. const rightType = this.right.getType();
  159. if ( leftType === rightType ) {
  160. return leftType;
  161. } else if ( toFloatType( leftType ) === toFloatType( rightType ) ) {
  162. return toFloatType( leftType );
  163. }
  164. return null;
  165. }
  166. }
  167. export class Unary extends ASTNode {
  168. constructor( type, expression, after = false ) {
  169. super();
  170. this.type = type;
  171. this.expression = expression;
  172. this.after = after;
  173. this.isUnary = true;
  174. this.initialize();
  175. }
  176. get isAssignment() {
  177. return /^(\+\+|--)$/.test( this.type );
  178. }
  179. get isNumericExpression() {
  180. if ( this.expression.isNumber ) {
  181. return true;
  182. }
  183. return false;
  184. }
  185. }
  186. export class Number extends ASTNode {
  187. constructor( value, type = 'float' ) {
  188. super();
  189. this.type = type;
  190. this.value = value;
  191. this.isNumber = true;
  192. this.initialize();
  193. }
  194. get isNumericExpression() {
  195. return true;
  196. }
  197. }
  198. export class String extends ASTNode {
  199. constructor( value ) {
  200. super();
  201. this.value = value;
  202. this.isString = true;
  203. this.initialize();
  204. }
  205. }
  206. export class Conditional extends ASTNode {
  207. constructor( cond = null, body = [] ) {
  208. super();
  209. this.cond = cond;
  210. this.body = body;
  211. this.elseConditional = null;
  212. this.isConditional = true;
  213. this.initialize();
  214. }
  215. }
  216. export class FunctionCall extends ASTNode {
  217. constructor( name, params = [] ) {
  218. super();
  219. this.name = name;
  220. this.params = params;
  221. this.isFunctionCall = true;
  222. this.initialize();
  223. }
  224. }
  225. export class Return extends ASTNode {
  226. constructor( value ) {
  227. super();
  228. this.value = value;
  229. this.isReturn = true;
  230. this.initialize();
  231. }
  232. }
  233. export class Discard extends ASTNode {
  234. constructor() {
  235. super();
  236. this.isDiscard = true;
  237. this.initialize();
  238. }
  239. }
  240. export class Continue extends ASTNode {
  241. constructor() {
  242. super();
  243. this.isContinue = true;
  244. this.initialize();
  245. }
  246. }
  247. export class Break extends ASTNode {
  248. constructor() {
  249. super();
  250. this.isBreak = true;
  251. this.initialize();
  252. }
  253. }
  254. export class Accessor extends ASTNode {
  255. constructor( property ) {
  256. super();
  257. this.property = property;
  258. this.isAccessor = true;
  259. this.initialize();
  260. }
  261. getType() {
  262. if ( this.linker.reference ) {
  263. return this.linker.reference.getType();
  264. }
  265. return super.getType();
  266. }
  267. }
  268. export class StaticElement extends ASTNode {
  269. constructor( value ) {
  270. super();
  271. this.value = value;
  272. this.isStaticElement = true;
  273. this.initialize();
  274. }
  275. }
  276. export class DynamicElement extends ASTNode {
  277. constructor( value ) {
  278. super();
  279. this.value = value;
  280. this.isDynamicElement = true;
  281. this.initialize();
  282. }
  283. }
  284. export class AccessorElements extends ASTNode {
  285. constructor( object, elements = [] ) {
  286. super();
  287. this.object = object;
  288. this.elements = elements;
  289. this.isAccessorElements = true;
  290. this.initialize();
  291. }
  292. }
  293. export class For extends ASTNode {
  294. constructor( initialization, condition, afterthought, body = [] ) {
  295. super();
  296. this.initialization = initialization;
  297. this.condition = condition;
  298. this.afterthought = afterthought;
  299. this.body = body;
  300. this.isFor = true;
  301. this.initialize();
  302. }
  303. }
  304. export class While extends ASTNode {
  305. constructor( condition, body = [] ) {
  306. super();
  307. this.condition = condition;
  308. this.body = body;
  309. this.isWhile = true;
  310. this.initialize();
  311. }
  312. }
  313. export class Switch extends ASTNode {
  314. constructor( discriminant, cases ) {
  315. super();
  316. this.discriminant = discriminant;
  317. this.cases = cases;
  318. this.isSwitch = true;
  319. this.initialize();
  320. }
  321. }
  322. export class SwitchCase extends ASTNode {
  323. constructor( body, conditions = null ) {
  324. super();
  325. this.body = body;
  326. this.conditions = conditions;
  327. this.isDefault = conditions === null ? true : false;
  328. this.isSwitchCase = true;
  329. this.initialize();
  330. }
  331. }
粤ICP备19079148号