Compiler-AST.st 7.5 KB

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