Compiler-AST.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 inlined'
  88. package: 'Compiler-AST'!
  89. !BlockNode methodsFor: 'accessing'!
  90. inlined
  91. ^inlined ifNil: [false]
  92. !
  93. inlined: aBoolean
  94. inlined := aBoolean
  95. !
  96. parameters
  97. ^parameters ifNil: [parameters := Array new]
  98. !
  99. parameters: aCollection
  100. parameters := aCollection
  101. !
  102. scope
  103. ^ scope
  104. !
  105. scope: aLexicalScope
  106. scope := aLexicalScope
  107. ! !
  108. !BlockNode methodsFor: 'testing'!
  109. isBlockNode
  110. ^true
  111. ! !
  112. !BlockNode methodsFor: 'visiting'!
  113. accept: aVisitor
  114. aVisitor visitBlockNode: self
  115. ! !
  116. Node subclass: #CascadeNode
  117. instanceVariableNames: 'receiver'
  118. package: 'Compiler-AST'!
  119. !CascadeNode methodsFor: 'accessing'!
  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. hasNonLocalReturn
  175. ^ self scope
  176. ifNil: [ false ]
  177. ifNotNil: [ self scope hasNonLocalReturn ]
  178. !
  179. messageSends
  180. ^ messageSends
  181. !
  182. messageSends: aCollection
  183. messageSends := aCollection
  184. !
  185. scope
  186. ^ scope
  187. !
  188. scope: aMethodScope
  189. scope := aMethodScope
  190. !
  191. selector
  192. ^selector
  193. !
  194. selector: aString
  195. selector := aString
  196. !
  197. source
  198. ^source
  199. !
  200. source: aString
  201. source := aString
  202. ! !
  203. !MethodNode methodsFor: 'testing'!
  204. canAliasChildren
  205. ^ false
  206. ! !
  207. !MethodNode methodsFor: 'visiting'!
  208. accept: aVisitor
  209. aVisitor visitMethodNode: self
  210. ! !
  211. Node subclass: #ReturnNode
  212. instanceVariableNames: 'nonLocalReturn'
  213. package: 'Compiler-AST'!
  214. !ReturnNode methodsFor: 'accessing'!
  215. nonLocalReturn
  216. ^ nonLocalReturn ifNil: [ false ]
  217. !
  218. nonLocalReturn: aBoolean
  219. nonLocalReturn := aBoolean
  220. ! !
  221. !ReturnNode methodsFor: 'testing'!
  222. shouldBeAliased
  223. ^ false
  224. ! !
  225. !ReturnNode methodsFor: 'visiting'!
  226. accept: aVisitor
  227. aVisitor visitReturnNode: self
  228. ! !
  229. Node subclass: #SendNode
  230. instanceVariableNames: 'selector arguments receiver'
  231. package: 'Compiler-AST'!
  232. !SendNode methodsFor: 'accessing'!
  233. arguments
  234. ^arguments ifNil: [arguments := #()]
  235. !
  236. arguments: aCollection
  237. arguments := aCollection
  238. !
  239. cascadeNodeWithMessages: aCollection
  240. | first |
  241. first := SendNode new
  242. selector: self selector;
  243. arguments: self arguments;
  244. yourself.
  245. ^CascadeNode new
  246. receiver: self receiver;
  247. nodes: (Array with: first), aCollection;
  248. yourself
  249. !
  250. nodes
  251. ^ (Array withAll: self arguments)
  252. add: self receiver;
  253. yourself
  254. !
  255. receiver
  256. ^receiver
  257. !
  258. receiver: aNode
  259. receiver := aNode
  260. !
  261. selector
  262. ^selector
  263. !
  264. selector: aString
  265. selector := aString
  266. !
  267. valueForReceiver: anObject
  268. ^SendNode new
  269. receiver: (self receiver
  270. ifNil: [anObject]
  271. ifNotNil: [self receiver valueForReceiver: anObject]);
  272. selector: self selector;
  273. arguments: self arguments;
  274. yourself
  275. ! !
  276. !SendNode methodsFor: 'testing'!
  277. isSendNode
  278. ^ true
  279. ! !
  280. !SendNode methodsFor: 'visiting'!
  281. accept: aVisitor
  282. aVisitor visitSendNode: self
  283. ! !
  284. Node subclass: #SequenceNode
  285. instanceVariableNames: 'temps scope'
  286. package: 'Compiler-AST'!
  287. !SequenceNode methodsFor: 'accessing'!
  288. scope
  289. ^ scope
  290. !
  291. scope: aLexicalScope
  292. scope := aLexicalScope
  293. !
  294. temps
  295. ^temps ifNil: [#()]
  296. !
  297. temps: aCollection
  298. temps := aCollection
  299. ! !
  300. !SequenceNode methodsFor: 'testing'!
  301. asBlockSequenceNode
  302. ^BlockSequenceNode new
  303. nodes: self nodes;
  304. temps: self temps;
  305. yourself
  306. !
  307. canAliasChildren
  308. ^ false
  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. isValueNode
  337. ^true
  338. !
  339. shouldBeAliased
  340. ^ false
  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. assigned := true
  361. !
  362. binding
  363. ^ binding
  364. !
  365. binding: aScopeVar
  366. (aScopeVar isKindOf: SemanticAnalyzer) ifTrue: [ self halt ].
  367. binding := aScopeVar
  368. ! !
  369. !VariableNode methodsFor: 'visiting'!
  370. accept: aVisitor
  371. aVisitor visitVariableNode: self
  372. ! !
  373. VariableNode subclass: #ClassReferenceNode
  374. instanceVariableNames: ''
  375. package: 'Compiler-AST'!
  376. !ClassReferenceNode methodsFor: 'visiting'!
  377. accept: aVisitor
  378. aVisitor visitClassReferenceNode: self
  379. ! !