Compiler-AST.st 7.2 KB

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