AST.js 7.7 KB

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