Compiler-AST.st 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. subtreeNeedsAliasing
  115. ^self shouldBeAliased or: [ self shouldBeInlined ]
  116. ! !
  117. !BlockNode methodsFor: 'visiting'!
  118. accept: aVisitor
  119. ^ aVisitor visitBlockNode: self
  120. ! !
  121. Node subclass: #CascadeNode
  122. instanceVariableNames: 'receiver'
  123. package: 'Compiler-AST'!
  124. !CascadeNode methodsFor: 'accessing'!
  125. receiver
  126. ^receiver
  127. !
  128. receiver: aNode
  129. receiver := aNode
  130. ! !
  131. !CascadeNode methodsFor: 'visiting'!
  132. accept: aVisitor
  133. ^ aVisitor visitCascadeNode: self
  134. ! !
  135. Node subclass: #DynamicArrayNode
  136. instanceVariableNames: ''
  137. package: 'Compiler-AST'!
  138. !DynamicArrayNode methodsFor: 'visiting'!
  139. accept: aVisitor
  140. ^ aVisitor visitDynamicArrayNode: self
  141. ! !
  142. Node subclass: #DynamicDictionaryNode
  143. instanceVariableNames: ''
  144. package: 'Compiler-AST'!
  145. !DynamicDictionaryNode methodsFor: 'visiting'!
  146. accept: aVisitor
  147. ^ aVisitor visitDynamicDictionaryNode: self
  148. ! !
  149. Node subclass: #JSStatementNode
  150. instanceVariableNames: 'source'
  151. package: 'Compiler-AST'!
  152. !JSStatementNode methodsFor: 'accessing'!
  153. source
  154. ^source ifNil: ['']
  155. !
  156. source: aString
  157. source := aString
  158. ! !
  159. !JSStatementNode methodsFor: 'visiting'!
  160. accept: aVisitor
  161. ^ aVisitor visitJSStatementNode: self
  162. ! !
  163. Node subclass: #MethodNode
  164. instanceVariableNames: 'selector arguments source scope classReferences messageSends superSends'
  165. package: 'Compiler-AST'!
  166. !MethodNode methodsFor: 'accessing'!
  167. arguments
  168. ^arguments ifNil: [#()]
  169. !
  170. arguments: aCollection
  171. arguments := aCollection
  172. !
  173. classReferences
  174. ^ classReferences
  175. !
  176. classReferences: aCollection
  177. classReferences := aCollection
  178. !
  179. messageSends
  180. ^ messageSends
  181. !
  182. messageSends: aCollection
  183. messageSends := aCollection
  184. !
  185. scope
  186. ^ scope
  187. !
  188. scope: aMethodScope
  189. scope := aMethodScope
  190. !
  191. selector
  192. ^selector
  193. !
  194. selector: aString
  195. selector := aString
  196. !
  197. source
  198. ^source
  199. !
  200. source: aString
  201. source := aString
  202. !
  203. superSends
  204. ^ superSends
  205. !
  206. superSends: aCollection
  207. superSends := aCollection
  208. ! !
  209. !MethodNode methodsFor: 'visiting'!
  210. accept: aVisitor
  211. ^ aVisitor visitMethodNode: self
  212. ! !
  213. Node subclass: #ReturnNode
  214. instanceVariableNames: 'scope'
  215. package: 'Compiler-AST'!
  216. !ReturnNode methodsFor: 'accessing'!
  217. scope
  218. ^ scope
  219. !
  220. scope: aLexicalScope
  221. scope := aLexicalScope
  222. ! !
  223. !ReturnNode methodsFor: 'testing'!
  224. isReturnNode
  225. ^ true
  226. !
  227. nonLocalReturn
  228. ^ self scope isMethodScope not
  229. ! !
  230. !ReturnNode methodsFor: 'visiting'!
  231. accept: aVisitor
  232. ^ aVisitor visitReturnNode: self
  233. ! !
  234. Node subclass: #SendNode
  235. instanceVariableNames: 'selector arguments receiver superSend index'
  236. package: 'Compiler-AST'!
  237. !SendNode methodsFor: 'accessing'!
  238. arguments
  239. ^arguments ifNil: [arguments := #()]
  240. !
  241. arguments: aCollection
  242. arguments := aCollection
  243. !
  244. cascadeNodeWithMessages: aCollection
  245. | first |
  246. first := SendNode new
  247. selector: self selector;
  248. arguments: self arguments;
  249. yourself.
  250. ^CascadeNode new
  251. receiver: self receiver;
  252. nodes: (Array with: first), aCollection;
  253. yourself
  254. !
  255. index
  256. ^ index
  257. !
  258. index: anInteger
  259. index := anInteger
  260. !
  261. nodes
  262. ^ (Array withAll: self arguments)
  263. add: self receiver;
  264. yourself
  265. !
  266. receiver
  267. ^receiver
  268. !
  269. receiver: aNode
  270. receiver := aNode
  271. !
  272. selector
  273. ^selector
  274. !
  275. selector: aString
  276. selector := aString
  277. !
  278. superSend
  279. ^ superSend ifNil: [ false ]
  280. !
  281. superSend: aBoolean
  282. superSend := aBoolean
  283. !
  284. valueForReceiver: anObject
  285. ^SendNode new
  286. receiver: (self receiver
  287. ifNil: [anObject]
  288. ifNotNil: [self receiver valueForReceiver: anObject]);
  289. selector: self selector;
  290. arguments: self arguments;
  291. yourself
  292. ! !
  293. !SendNode methodsFor: 'testing'!
  294. isSendNode
  295. ^ true
  296. ! !
  297. !SendNode methodsFor: 'visiting'!
  298. accept: aVisitor
  299. ^ aVisitor visitSendNode: self
  300. ! !
  301. Node subclass: #SequenceNode
  302. instanceVariableNames: 'temps scope'
  303. package: 'Compiler-AST'!
  304. !SequenceNode methodsFor: 'accessing'!
  305. scope
  306. ^ scope
  307. !
  308. scope: aLexicalScope
  309. scope := aLexicalScope
  310. !
  311. temps
  312. ^temps ifNil: [#()]
  313. !
  314. temps: aCollection
  315. temps := aCollection
  316. ! !
  317. !SequenceNode methodsFor: 'testing'!
  318. asBlockSequenceNode
  319. ^BlockSequenceNode new
  320. nodes: self nodes;
  321. temps: self temps;
  322. yourself
  323. ! !
  324. !SequenceNode methodsFor: 'visiting'!
  325. accept: aVisitor
  326. ^ aVisitor visitSequenceNode: self
  327. ! !
  328. SequenceNode subclass: #BlockSequenceNode
  329. instanceVariableNames: ''
  330. package: 'Compiler-AST'!
  331. !BlockSequenceNode methodsFor: 'testing'!
  332. isBlockSequenceNode
  333. ^true
  334. ! !
  335. !BlockSequenceNode methodsFor: 'visiting'!
  336. accept: aVisitor
  337. ^ aVisitor visitBlockSequenceNode: self
  338. ! !
  339. Node subclass: #ValueNode
  340. instanceVariableNames: 'value'
  341. package: 'Compiler-AST'!
  342. !ValueNode methodsFor: 'accessing'!
  343. value
  344. ^value
  345. !
  346. value: anObject
  347. value := anObject
  348. ! !
  349. !ValueNode methodsFor: 'testing'!
  350. isImmutable
  351. ^true
  352. !
  353. isValueNode
  354. ^true
  355. ! !
  356. !ValueNode methodsFor: 'visiting'!
  357. accept: aVisitor
  358. ^ aVisitor visitValueNode: self
  359. ! !
  360. ValueNode subclass: #VariableNode
  361. instanceVariableNames: 'assigned binding'
  362. package: 'Compiler-AST'!
  363. !VariableNode methodsFor: 'accessing'!
  364. alias
  365. ^ self binding alias
  366. !
  367. assigned
  368. ^assigned ifNil: [false]
  369. !
  370. assigned: aBoolean
  371. assigned := aBoolean
  372. !
  373. beAssigned
  374. self binding validateAssignment.
  375. assigned := true
  376. !
  377. binding
  378. ^ binding
  379. !
  380. binding: aScopeVar
  381. binding := aScopeVar
  382. ! !
  383. !VariableNode methodsFor: 'testing'!
  384. isImmutable
  385. ^false
  386. ! !
  387. !VariableNode methodsFor: 'visiting'!
  388. accept: aVisitor
  389. ^ aVisitor visitVariableNode: self
  390. ! !
  391. VariableNode subclass: #ClassReferenceNode
  392. instanceVariableNames: ''
  393. package: 'Compiler-AST'!
  394. !ClassReferenceNode methodsFor: 'visiting'!
  395. accept: aVisitor
  396. ^ aVisitor visitClassReferenceNode: self
  397. ! !