Compiler-AST.st 7.6 KB

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