1
0

Compiler-AST.st 7.6 KB

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