1
0

Compiler-AST.st 7.5 KB

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