1
0

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'
  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. receiver
  115. ^receiver
  116. !
  117. receiver: aNode
  118. receiver := aNode
  119. ! !
  120. !CascadeNode methodsFor: 'visiting'!
  121. accept: aVisitor
  122. aVisitor visitCascadeNode: self
  123. ! !
  124. Node subclass: #DynamicArrayNode
  125. instanceVariableNames: ''
  126. package: 'Compiler-AST'!
  127. !DynamicArrayNode methodsFor: 'visiting'!
  128. accept: aVisitor
  129. aVisitor visitDynamicArrayNode: self
  130. ! !
  131. Node subclass: #DynamicDictionaryNode
  132. instanceVariableNames: ''
  133. package: 'Compiler-AST'!
  134. !DynamicDictionaryNode methodsFor: 'visiting'!
  135. accept: aVisitor
  136. aVisitor visitDynamicDictionaryNode: self
  137. ! !
  138. Node subclass: #JSStatementNode
  139. instanceVariableNames: 'source'
  140. package: 'Compiler-AST'!
  141. !JSStatementNode methodsFor: 'accessing'!
  142. source
  143. ^source ifNil: ['']
  144. !
  145. source: aString
  146. source := aString
  147. ! !
  148. !JSStatementNode methodsFor: 'visiting'!
  149. accept: aVisitor
  150. aVisitor visitJSStatementNode: self
  151. ! !
  152. Node subclass: #MethodNode
  153. instanceVariableNames: 'selector arguments source scope classReferences messageSends'
  154. package: 'Compiler-AST'!
  155. !MethodNode methodsFor: 'accessing'!
  156. arguments
  157. ^arguments ifNil: [#()]
  158. !
  159. arguments: aCollection
  160. arguments := aCollection
  161. !
  162. classReferences
  163. ^ classReferences
  164. !
  165. classReferences: aCollection
  166. classReferences := aCollection
  167. !
  168. hasNonLocalReturn
  169. ^ self scope
  170. ifNil: [ false ]
  171. ifNotNil: [ self scope hasNonLocalReturn ]
  172. !
  173. messageSends
  174. ^ messageSends
  175. !
  176. messageSends: aCollection
  177. messageSends := aCollection
  178. !
  179. scope
  180. ^ scope
  181. !
  182. scope: aMethodScope
  183. scope := aMethodScope
  184. !
  185. selector
  186. ^selector
  187. !
  188. selector: aString
  189. selector := aString
  190. !
  191. source
  192. ^source
  193. !
  194. source: aString
  195. source := aString
  196. ! !
  197. !MethodNode methodsFor: 'testing'!
  198. canAliasChildren
  199. ^ false
  200. ! !
  201. !MethodNode methodsFor: 'visiting'!
  202. accept: aVisitor
  203. aVisitor visitMethodNode: self
  204. ! !
  205. Node subclass: #ReturnNode
  206. instanceVariableNames: 'nonLocalReturn'
  207. package: 'Compiler-AST'!
  208. !ReturnNode methodsFor: 'accessing'!
  209. nonLocalReturn
  210. ^ nonLocalReturn ifNil: [ false ]
  211. !
  212. nonLocalReturn: aBoolean
  213. nonLocalReturn := aBoolean
  214. ! !
  215. !ReturnNode methodsFor: 'testing'!
  216. shouldBeAliased
  217. ^ false
  218. ! !
  219. !ReturnNode methodsFor: 'visiting'!
  220. accept: aVisitor
  221. aVisitor visitReturnNode: self
  222. ! !
  223. Node subclass: #SendNode
  224. instanceVariableNames: 'selector arguments receiver superSend'
  225. package: 'Compiler-AST'!
  226. !SendNode methodsFor: 'accessing'!
  227. arguments
  228. ^arguments ifNil: [arguments := #()]
  229. !
  230. arguments: aCollection
  231. arguments := aCollection
  232. !
  233. cascadeNodeWithMessages: aCollection
  234. | first |
  235. first := SendNode new
  236. selector: self selector;
  237. arguments: self arguments;
  238. yourself.
  239. ^CascadeNode new
  240. receiver: self receiver;
  241. nodes: (Array with: first), aCollection;
  242. yourself
  243. !
  244. nodes
  245. ^ (Array withAll: self arguments)
  246. add: self receiver;
  247. yourself
  248. !
  249. receiver
  250. ^receiver
  251. !
  252. receiver: aNode
  253. receiver := aNode
  254. !
  255. selector
  256. ^selector
  257. !
  258. selector: aString
  259. selector := aString
  260. !
  261. superSend
  262. ^ superSend ifNil: [ false ]
  263. !
  264. superSend: aBoolean
  265. superSend := aBoolean
  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. ! !