1
0

Compiler-AST.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 superSends'
  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. superSends
  187. ^ superSends
  188. !
  189. superSends: aCollection
  190. superSends := aCollection
  191. ! !
  192. !MethodNode methodsFor: 'visiting'!
  193. accept: aVisitor
  194. ^ aVisitor visitMethodNode: self
  195. ! !
  196. Node subclass: #ReturnNode
  197. instanceVariableNames: 'scope'
  198. package: 'Compiler-AST'!
  199. !ReturnNode methodsFor: 'accessing'!
  200. scope
  201. ^ scope
  202. !
  203. scope: aLexicalScope
  204. scope := aLexicalScope
  205. ! !
  206. !ReturnNode methodsFor: 'testing'!
  207. isReturnNode
  208. ^ true
  209. !
  210. nonLocalReturn
  211. ^ self scope isMethodScope not
  212. ! !
  213. !ReturnNode methodsFor: 'visiting'!
  214. accept: aVisitor
  215. ^ aVisitor visitReturnNode: self
  216. ! !
  217. Node subclass: #SendNode
  218. instanceVariableNames: 'selector arguments receiver superSend index'
  219. package: 'Compiler-AST'!
  220. !SendNode methodsFor: 'accessing'!
  221. arguments
  222. ^arguments ifNil: [arguments := #()]
  223. !
  224. arguments: aCollection
  225. arguments := aCollection
  226. !
  227. cascadeNodeWithMessages: aCollection
  228. | first |
  229. first := SendNode new
  230. selector: self selector;
  231. arguments: self arguments;
  232. yourself.
  233. ^CascadeNode new
  234. receiver: self receiver;
  235. nodes: (Array with: first), aCollection;
  236. yourself
  237. !
  238. index
  239. ^ index
  240. !
  241. index: anInteger
  242. index := anInteger
  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. !SequenceNode methodsFor: 'visiting'!
  308. accept: aVisitor
  309. ^ aVisitor visitSequenceNode: self
  310. ! !
  311. SequenceNode subclass: #BlockSequenceNode
  312. instanceVariableNames: ''
  313. package: 'Compiler-AST'!
  314. !BlockSequenceNode methodsFor: 'testing'!
  315. isBlockSequenceNode
  316. ^true
  317. ! !
  318. !BlockSequenceNode methodsFor: 'visiting'!
  319. accept: aVisitor
  320. ^ aVisitor visitBlockSequenceNode: self
  321. ! !
  322. Node subclass: #ValueNode
  323. instanceVariableNames: 'value'
  324. package: 'Compiler-AST'!
  325. !ValueNode methodsFor: 'accessing'!
  326. value
  327. ^value
  328. !
  329. value: anObject
  330. value := anObject
  331. ! !
  332. !ValueNode methodsFor: 'testing'!
  333. isValueNode
  334. ^true
  335. ! !
  336. !ValueNode methodsFor: 'visiting'!
  337. accept: aVisitor
  338. ^ aVisitor visitValueNode: self
  339. ! !
  340. ValueNode subclass: #VariableNode
  341. instanceVariableNames: 'assigned binding'
  342. package: 'Compiler-AST'!
  343. !VariableNode methodsFor: 'accessing'!
  344. alias
  345. ^ self binding alias
  346. !
  347. assigned
  348. ^assigned ifNil: [false]
  349. !
  350. assigned: aBoolean
  351. assigned := aBoolean
  352. !
  353. beAssigned
  354. self binding validateAssignment.
  355. assigned := true
  356. !
  357. binding
  358. ^ binding
  359. !
  360. binding: aScopeVar
  361. binding := aScopeVar
  362. ! !
  363. !VariableNode methodsFor: 'visiting'!
  364. accept: aVisitor
  365. ^ aVisitor visitVariableNode: self
  366. ! !
  367. VariableNode subclass: #ClassReferenceNode
  368. instanceVariableNames: ''
  369. package: 'Compiler-AST'!
  370. !ClassReferenceNode methodsFor: 'visiting'!
  371. accept: aVisitor
  372. ^ aVisitor visitClassReferenceNode: self
  373. ! !