1
0

Compiler-AST.st 6.9 KB

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