Compiler-AST.st 6.8 KB

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