1
0

Compiler-AST.st 7.2 KB

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