1
0

Compiler-AST.st 7.2 KB

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