Compiler-AST.st 7.2 KB

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