Compiler-AST.st 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. Smalltalk current createPackage: 'Compiler-AST' properties: #{}!
  2. Object subclass: #Node
  3. instanceVariableNames: 'nodes shouldBeInlined shouldBeAliased'
  4. package: 'Compiler-AST'!
  5. !Node commentStamp!
  6. I am the abstract root class of the abstract syntax tree.!
  7. !Node methodsFor: 'accessing'!
  8. addNode: aNode
  9. self nodes add: aNode
  10. !
  11. nodes
  12. ^nodes ifNil: [nodes := Array new]
  13. !
  14. shouldBeAliased
  15. ^ shouldBeAliased ifNil: [ false ]
  16. !
  17. shouldBeAliased: aBoolean
  18. shouldBeAliased := aBoolean
  19. !
  20. shouldBeInlined
  21. ^ shouldBeInlined ifNil: [ false ]
  22. !
  23. shouldBeInlined: aBoolean
  24. shouldBeInlined := aBoolean
  25. ! !
  26. !Node methodsFor: 'building'!
  27. nodes: aCollection
  28. nodes := aCollection
  29. ! !
  30. !Node methodsFor: 'testing'!
  31. isAssignmentNode
  32. ^ false
  33. !
  34. isBlockNode
  35. ^false
  36. !
  37. isBlockSequenceNode
  38. ^false
  39. !
  40. isImmutable
  41. ^false
  42. !
  43. isReturnNode
  44. ^false
  45. !
  46. isSendNode
  47. ^false
  48. !
  49. isValueNode
  50. ^false
  51. !
  52. subtreeNeedsAliasing
  53. ^(self shouldBeAliased or: [ self shouldBeInlined ]) or: [
  54. (self nodes detect: [ :node | node subtreeNeedsAliasing ] ifNone: [ false ]) ~= false
  55. ]
  56. ! !
  57. !Node methodsFor: 'visiting'!
  58. accept: aVisitor
  59. ^ aVisitor visitNode: self
  60. ! !
  61. Node subclass: #AssignmentNode
  62. instanceVariableNames: 'left right'
  63. package: 'Compiler-AST'!
  64. !AssignmentNode methodsFor: 'accessing'!
  65. left
  66. ^left
  67. !
  68. left: aNode
  69. left := aNode
  70. !
  71. nodes
  72. ^ Array with: self left with: self right
  73. !
  74. right
  75. ^right
  76. !
  77. right: aNode
  78. right := aNode
  79. ! !
  80. !AssignmentNode methodsFor: 'testing'!
  81. isAssignmentNode
  82. ^ true
  83. ! !
  84. !AssignmentNode methodsFor: 'visiting'!
  85. accept: aVisitor
  86. ^ aVisitor visitAssignmentNode: self
  87. ! !
  88. Node subclass: #BlockNode
  89. instanceVariableNames: 'parameters scope'
  90. package: 'Compiler-AST'!
  91. !BlockNode methodsFor: 'accessing'!
  92. parameters
  93. ^parameters ifNil: [parameters := Array new]
  94. !
  95. parameters: aCollection
  96. parameters := aCollection
  97. !
  98. scope
  99. ^ scope
  100. !
  101. scope: aLexicalScope
  102. scope := aLexicalScope
  103. ! !
  104. !BlockNode methodsFor: 'testing'!
  105. isBlockNode
  106. ^true
  107. !
  108. subtreeNeedsAliasing
  109. ^self shouldBeAliased or: [ self shouldBeInlined ]
  110. ! !
  111. !BlockNode methodsFor: 'visiting'!
  112. accept: aVisitor
  113. ^ aVisitor visitBlockNode: self
  114. ! !
  115. Node subclass: #CascadeNode
  116. instanceVariableNames: 'receiver'
  117. package: 'Compiler-AST'!
  118. !CascadeNode methodsFor: 'accessing'!
  119. receiver
  120. ^receiver
  121. !
  122. receiver: aNode
  123. receiver := aNode
  124. ! !
  125. !CascadeNode methodsFor: 'visiting'!
  126. accept: aVisitor
  127. ^ aVisitor visitCascadeNode: self
  128. ! !
  129. Node subclass: #DynamicArrayNode
  130. instanceVariableNames: ''
  131. package: 'Compiler-AST'!
  132. !DynamicArrayNode methodsFor: 'visiting'!
  133. accept: aVisitor
  134. ^ aVisitor visitDynamicArrayNode: self
  135. ! !
  136. Node subclass: #DynamicDictionaryNode
  137. instanceVariableNames: ''
  138. package: 'Compiler-AST'!
  139. !DynamicDictionaryNode methodsFor: 'visiting'!
  140. accept: aVisitor
  141. ^ aVisitor visitDynamicDictionaryNode: self
  142. ! !
  143. Node subclass: #JSStatementNode
  144. instanceVariableNames: 'source'
  145. package: 'Compiler-AST'!
  146. !JSStatementNode methodsFor: 'accessing'!
  147. source
  148. ^source ifNil: ['']
  149. !
  150. source: aString
  151. source := aString
  152. ! !
  153. !JSStatementNode methodsFor: 'visiting'!
  154. accept: aVisitor
  155. ^ aVisitor visitJSStatementNode: self
  156. ! !
  157. Node subclass: #MethodNode
  158. instanceVariableNames: 'selector arguments source scope classReferences messageSends'
  159. package: 'Compiler-AST'!
  160. !MethodNode methodsFor: 'accessing'!
  161. arguments
  162. ^arguments ifNil: [#()]
  163. !
  164. arguments: aCollection
  165. arguments := aCollection
  166. !
  167. classReferences
  168. ^ classReferences
  169. !
  170. classReferences: aCollection
  171. classReferences := aCollection
  172. !
  173. messageSends
  174. ^ messageSends
  175. !
  176. messageSends: aCollection
  177. messageSends := aCollection
  178. !
  179. scope
  180. ^ scope
  181. !
  182. scope: aMethodScope
  183. scope := aMethodScope
  184. !
  185. selector
  186. ^selector
  187. !
  188. selector: aString
  189. selector := aString
  190. !
  191. source
  192. ^source
  193. !
  194. source: aString
  195. source := aString
  196. ! !
  197. !MethodNode methodsFor: 'visiting'!
  198. accept: aVisitor
  199. ^ aVisitor visitMethodNode: self
  200. ! !
  201. Node subclass: #ReturnNode
  202. instanceVariableNames: 'scope'
  203. package: 'Compiler-AST'!
  204. !ReturnNode methodsFor: 'accessing'!
  205. scope
  206. ^ scope
  207. !
  208. scope: aLexicalScope
  209. scope := aLexicalScope
  210. ! !
  211. !ReturnNode methodsFor: 'testing'!
  212. isReturnNode
  213. ^ true
  214. !
  215. nonLocalReturn
  216. ^ self scope isMethodScope not
  217. ! !
  218. !ReturnNode methodsFor: 'visiting'!
  219. accept: aVisitor
  220. ^ aVisitor visitReturnNode: self
  221. ! !
  222. Node subclass: #SendNode
  223. instanceVariableNames: 'selector arguments receiver superSend index'
  224. package: 'Compiler-AST'!
  225. !SendNode methodsFor: 'accessing'!
  226. arguments
  227. ^arguments ifNil: [arguments := #()]
  228. !
  229. arguments: aCollection
  230. arguments := aCollection
  231. !
  232. cascadeNodeWithMessages: aCollection
  233. | first |
  234. first := SendNode new
  235. selector: self selector;
  236. arguments: self arguments;
  237. yourself.
  238. ^CascadeNode new
  239. receiver: self receiver;
  240. nodes: (Array with: first), aCollection;
  241. yourself
  242. !
  243. index
  244. ^ index
  245. !
  246. index: anInteger
  247. index := anInteger
  248. !
  249. nodes
  250. ^ (Array withAll: self arguments)
  251. add: self receiver;
  252. yourself
  253. !
  254. receiver
  255. ^receiver
  256. !
  257. receiver: aNode
  258. receiver := aNode
  259. !
  260. selector
  261. ^selector
  262. !
  263. selector: aString
  264. selector := aString
  265. !
  266. superSend
  267. ^ superSend ifNil: [ false ]
  268. !
  269. superSend: aBoolean
  270. superSend := aBoolean
  271. !
  272. valueForReceiver: anObject
  273. ^SendNode new
  274. receiver: (self receiver
  275. ifNil: [anObject]
  276. ifNotNil: [self receiver valueForReceiver: anObject]);
  277. selector: self selector;
  278. arguments: self arguments;
  279. yourself
  280. ! !
  281. !SendNode methodsFor: 'testing'!
  282. isSendNode
  283. ^ true
  284. ! !
  285. !SendNode methodsFor: 'visiting'!
  286. accept: aVisitor
  287. ^ aVisitor visitSendNode: self
  288. ! !
  289. Node subclass: #SequenceNode
  290. instanceVariableNames: 'temps scope'
  291. package: 'Compiler-AST'!
  292. !SequenceNode methodsFor: 'accessing'!
  293. scope
  294. ^ scope
  295. !
  296. scope: aLexicalScope
  297. scope := aLexicalScope
  298. !
  299. temps
  300. ^temps ifNil: [#()]
  301. !
  302. temps: aCollection
  303. temps := aCollection
  304. ! !
  305. !SequenceNode methodsFor: 'testing'!
  306. asBlockSequenceNode
  307. ^BlockSequenceNode new
  308. nodes: self nodes;
  309. temps: self temps;
  310. yourself
  311. ! !
  312. !SequenceNode methodsFor: 'visiting'!
  313. accept: aVisitor
  314. ^ aVisitor visitSequenceNode: self
  315. ! !
  316. SequenceNode subclass: #BlockSequenceNode
  317. instanceVariableNames: ''
  318. package: 'Compiler-AST'!
  319. !BlockSequenceNode methodsFor: 'testing'!
  320. isBlockSequenceNode
  321. ^true
  322. ! !
  323. !BlockSequenceNode methodsFor: 'visiting'!
  324. accept: aVisitor
  325. ^ aVisitor visitBlockSequenceNode: self
  326. ! !
  327. Node subclass: #ValueNode
  328. instanceVariableNames: 'value'
  329. package: 'Compiler-AST'!
  330. !ValueNode methodsFor: 'accessing'!
  331. value
  332. ^value
  333. !
  334. value: anObject
  335. value := anObject
  336. ! !
  337. !ValueNode methodsFor: 'testing'!
  338. isImmutable
  339. ^true
  340. !
  341. isValueNode
  342. ^true
  343. ! !
  344. !ValueNode methodsFor: 'visiting'!
  345. accept: aVisitor
  346. ^ aVisitor visitValueNode: self
  347. ! !
  348. ValueNode subclass: #VariableNode
  349. instanceVariableNames: 'assigned binding'
  350. package: 'Compiler-AST'!
  351. !VariableNode methodsFor: 'accessing'!
  352. alias
  353. ^ self binding alias
  354. !
  355. assigned
  356. ^assigned ifNil: [false]
  357. !
  358. assigned: aBoolean
  359. assigned := aBoolean
  360. !
  361. beAssigned
  362. self binding validateAssignment.
  363. assigned := true
  364. !
  365. binding
  366. ^ binding
  367. !
  368. binding: aScopeVar
  369. binding := aScopeVar
  370. ! !
  371. !VariableNode methodsFor: 'testing'!
  372. isImmutable
  373. ^false
  374. ! !
  375. !VariableNode methodsFor: 'visiting'!
  376. accept: aVisitor
  377. ^ aVisitor visitVariableNode: self
  378. ! !
  379. VariableNode subclass: #ClassReferenceNode
  380. instanceVariableNames: ''
  381. package: 'Compiler-AST'!
  382. !ClassReferenceNode methodsFor: 'visiting'!
  383. accept: aVisitor
  384. ^ aVisitor visitClassReferenceNode: self
  385. ! !