Compiler-AST.st 7.3 KB

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