Compiler-AST.st 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. Smalltalk current createPackage: 'Compiler-AST'!
  2. Object subclass: #Node
  3. instanceVariableNames: 'parent position nodes shouldBeInlined shouldBeAliased'
  4. package: 'Compiler-AST'!
  5. !Node commentStamp!
  6. I am the abstract root class of the abstract syntax tree.
  7. Concrete classes should implement `#accept:` to allow visiting.
  8. `position` holds a point containing line and column number of the symbol location in the original source file.!
  9. !Node methodsFor: 'accessing'!
  10. addNode: aNode
  11. self nodes add: aNode.
  12. aNode parent: self
  13. !
  14. nextChild
  15. "Answer the next node after aNode.
  16. Recurse into the possible children of the receiver to answer the next node to be evaluated"
  17. ^ self nodes isEmpty
  18. ifTrue: [ self ]
  19. ifFalse: [ self nodes first nextChild ]
  20. !
  21. nextNode
  22. ^ self parent ifNotNil: [ :node |
  23. node nextNode: self ]
  24. !
  25. nextNode: aNode
  26. "Answer the next node after aNode.
  27. Recurse into the possible children of the next node to answer the next node to be evaluated"
  28. | next |
  29. next := self nodes
  30. at: (self nodes indexOf: aNode) + 1
  31. ifAbsent: [ ^ self ].
  32. ^ next nextChild
  33. !
  34. nodes
  35. ^nodes ifNil: [nodes := Array new]
  36. !
  37. parent
  38. ^ parent
  39. !
  40. parent: aNode
  41. parent := aNode
  42. !
  43. position
  44. "answer the line and column of the receiver in the source code"
  45. ^ position ifNil: [
  46. self parent ifNotNil: [ :node | node position ] ]
  47. !
  48. shouldBeAliased
  49. ^ shouldBeAliased ifNil: [ false ]
  50. !
  51. shouldBeAliased: aBoolean
  52. shouldBeAliased := aBoolean
  53. !
  54. shouldBeInlined
  55. ^ shouldBeInlined ifNil: [ false ]
  56. !
  57. shouldBeInlined: aBoolean
  58. shouldBeInlined := aBoolean
  59. ! !
  60. !Node methodsFor: 'building'!
  61. nodes: aCollection
  62. nodes := aCollection.
  63. aCollection do: [ :each | each parent: self ]
  64. !
  65. position: aPosition
  66. position := aPosition
  67. ! !
  68. !Node methodsFor: 'copying'!
  69. postCopy
  70. super postCopy.
  71. self nodes do: [ :each | each parent: self ]
  72. ! !
  73. !Node methodsFor: 'testing'!
  74. isAssignmentNode
  75. ^ false
  76. !
  77. isBlockNode
  78. ^false
  79. !
  80. isBlockSequenceNode
  81. ^false
  82. !
  83. isCascadeNode
  84. ^ false
  85. !
  86. isImmutable
  87. ^false
  88. !
  89. isJSStatementNode
  90. ^ false
  91. !
  92. isLastChild
  93. ^ self parent nodes last = self
  94. !
  95. isNode
  96. ^ true
  97. !
  98. isReturnNode
  99. ^false
  100. !
  101. isSendNode
  102. ^false
  103. !
  104. isValueNode
  105. ^false
  106. !
  107. stopOnStepping
  108. ^ false
  109. !
  110. subtreeNeedsAliasing
  111. ^(self shouldBeAliased or: [ self shouldBeInlined ]) or: [
  112. (self nodes detect: [ :each | each subtreeNeedsAliasing ] ifNone: [ false ]) ~= false ]
  113. ! !
  114. !Node methodsFor: 'visiting'!
  115. accept: aVisitor
  116. ^ aVisitor visitNode: self
  117. ! !
  118. Node subclass: #AssignmentNode
  119. instanceVariableNames: 'left right'
  120. package: 'Compiler-AST'!
  121. !AssignmentNode commentStamp!
  122. I represent an assignment node.!
  123. !AssignmentNode methodsFor: 'accessing'!
  124. left
  125. ^left
  126. !
  127. left: aNode
  128. left := aNode.
  129. aNode parent: self
  130. !
  131. nodes
  132. ^ Array with: self left with: self right
  133. !
  134. right
  135. ^right
  136. !
  137. right: aNode
  138. right := aNode.
  139. aNode parent: self
  140. ! !
  141. !AssignmentNode methodsFor: 'testing'!
  142. isAssignmentNode
  143. ^ true
  144. ! !
  145. !AssignmentNode methodsFor: 'visiting'!
  146. accept: aVisitor
  147. ^ aVisitor visitAssignmentNode: self
  148. ! !
  149. Node subclass: #BlockNode
  150. instanceVariableNames: 'parameters scope'
  151. package: 'Compiler-AST'!
  152. !BlockNode commentStamp!
  153. I represent an block closure node.!
  154. !BlockNode methodsFor: 'accessing'!
  155. nextChild
  156. "Answer the receiver as we want to avoid eager evaluation"
  157. ^ self
  158. !
  159. nextNode: aNode
  160. "Answer the receiver as we want to avoid eager evaluation"
  161. ^ self
  162. !
  163. parameters
  164. ^parameters ifNil: [parameters := Array new]
  165. !
  166. parameters: aCollection
  167. parameters := aCollection
  168. !
  169. scope
  170. ^ scope
  171. !
  172. scope: aLexicalScope
  173. scope := aLexicalScope
  174. ! !
  175. !BlockNode methodsFor: 'testing'!
  176. isBlockNode
  177. ^true
  178. !
  179. subtreeNeedsAliasing
  180. ^ self shouldBeAliased or: [ self shouldBeInlined ]
  181. ! !
  182. !BlockNode methodsFor: 'visiting'!
  183. accept: aVisitor
  184. ^ aVisitor visitBlockNode: self
  185. ! !
  186. Node subclass: #CascadeNode
  187. instanceVariableNames: 'receiver'
  188. package: 'Compiler-AST'!
  189. !CascadeNode commentStamp!
  190. I represent an cascade node.!
  191. !CascadeNode methodsFor: 'accessing'!
  192. receiver
  193. ^receiver
  194. !
  195. receiver: aNode
  196. receiver := aNode
  197. ! !
  198. !CascadeNode methodsFor: 'testing'!
  199. isCascadeNode
  200. ^ true
  201. ! !
  202. !CascadeNode methodsFor: 'visiting'!
  203. accept: aVisitor
  204. ^ aVisitor visitCascadeNode: self
  205. ! !
  206. Node subclass: #DynamicArrayNode
  207. instanceVariableNames: ''
  208. package: 'Compiler-AST'!
  209. !DynamicArrayNode commentStamp!
  210. I represent an dynamic array node.!
  211. !DynamicArrayNode methodsFor: 'visiting'!
  212. accept: aVisitor
  213. ^ aVisitor visitDynamicArrayNode: self
  214. ! !
  215. Node subclass: #DynamicDictionaryNode
  216. instanceVariableNames: ''
  217. package: 'Compiler-AST'!
  218. !DynamicDictionaryNode commentStamp!
  219. I represent an dynamic dictionary node.!
  220. !DynamicDictionaryNode methodsFor: 'visiting'!
  221. accept: aVisitor
  222. ^ aVisitor visitDynamicDictionaryNode: self
  223. ! !
  224. Node subclass: #JSStatementNode
  225. instanceVariableNames: 'source'
  226. package: 'Compiler-AST'!
  227. !JSStatementNode commentStamp!
  228. I represent an JavaScript statement node.!
  229. !JSStatementNode methodsFor: 'accessing'!
  230. source
  231. ^source ifNil: ['']
  232. !
  233. source: aString
  234. source := aString
  235. ! !
  236. !JSStatementNode methodsFor: 'testing'!
  237. isJSStatementNode
  238. ^ true
  239. ! !
  240. !JSStatementNode methodsFor: 'visiting'!
  241. accept: aVisitor
  242. ^ aVisitor visitJSStatementNode: self
  243. ! !
  244. Node subclass: #MethodNode
  245. instanceVariableNames: 'selector arguments source scope classReferences messageSends superSends'
  246. package: 'Compiler-AST'!
  247. !MethodNode commentStamp!
  248. I represent an method node.
  249. A method node must be the root and only method node of a valid AST.!
  250. !MethodNode methodsFor: 'accessing'!
  251. arguments
  252. ^arguments ifNil: [#()]
  253. !
  254. arguments: aCollection
  255. arguments := aCollection
  256. !
  257. classReferences
  258. ^ classReferences
  259. !
  260. classReferences: aCollection
  261. classReferences := aCollection
  262. !
  263. extent
  264. ^ self source lines size @ (self source lines last size + 1)
  265. !
  266. messageSends
  267. ^ messageSends
  268. !
  269. messageSends: aCollection
  270. messageSends := aCollection
  271. !
  272. scope
  273. ^ scope
  274. !
  275. scope: aMethodScope
  276. scope := aMethodScope
  277. !
  278. selector
  279. ^selector
  280. !
  281. selector: aString
  282. selector := aString
  283. !
  284. source
  285. ^source
  286. !
  287. source: aString
  288. source := aString
  289. !
  290. superSends
  291. ^ superSends
  292. !
  293. superSends: aCollection
  294. superSends := aCollection
  295. ! !
  296. !MethodNode methodsFor: 'visiting'!
  297. accept: aVisitor
  298. ^ aVisitor visitMethodNode: self
  299. ! !
  300. Node subclass: #ReturnNode
  301. instanceVariableNames: 'scope'
  302. package: 'Compiler-AST'!
  303. !ReturnNode commentStamp!
  304. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  305. !ReturnNode methodsFor: 'accessing'!
  306. scope
  307. ^ scope
  308. !
  309. scope: aLexicalScope
  310. scope := aLexicalScope
  311. ! !
  312. !ReturnNode methodsFor: 'testing'!
  313. isReturnNode
  314. ^ true
  315. !
  316. nonLocalReturn
  317. ^ self scope isMethodScope not
  318. ! !
  319. !ReturnNode methodsFor: 'visiting'!
  320. accept: aVisitor
  321. ^ aVisitor visitReturnNode: self
  322. ! !
  323. Node subclass: #SendNode
  324. instanceVariableNames: 'selector arguments receiver superSend index'
  325. package: 'Compiler-AST'!
  326. !SendNode commentStamp!
  327. I represent an message send node.!
  328. !SendNode methodsFor: 'accessing'!
  329. arguments
  330. ^arguments ifNil: [arguments := #()]
  331. !
  332. arguments: aCollection
  333. arguments := aCollection.
  334. aCollection do: [ :each | each parent: self ]
  335. !
  336. cascadeNodeWithMessages: aCollection
  337. | first |
  338. first := SendNode new
  339. selector: self selector;
  340. arguments: self arguments;
  341. yourself.
  342. ^CascadeNode new
  343. receiver: self receiver;
  344. nodes: (Array with: first), aCollection;
  345. yourself
  346. !
  347. index
  348. ^ index
  349. !
  350. index: anInteger
  351. index := anInteger
  352. !
  353. nodes
  354. self receiver ifNil: [ ^ self arguments copy ].
  355. ^ (Array with: self receiver)
  356. addAll: self arguments;
  357. yourself
  358. !
  359. receiver
  360. ^receiver
  361. !
  362. receiver: aNode
  363. receiver := aNode.
  364. aNode isNode ifTrue: [
  365. aNode parent: self ]
  366. !
  367. selector
  368. ^selector
  369. !
  370. selector: aString
  371. selector := aString
  372. !
  373. superSend
  374. ^ superSend ifNil: [ false ]
  375. !
  376. superSend: aBoolean
  377. superSend := aBoolean
  378. !
  379. valueForReceiver: anObject
  380. ^SendNode new
  381. position: self position;
  382. receiver: (self receiver
  383. ifNil: [anObject]
  384. ifNotNil: [self receiver valueForReceiver: anObject]);
  385. selector: self selector;
  386. arguments: self arguments;
  387. yourself
  388. ! !
  389. !SendNode methodsFor: 'testing'!
  390. isCascadeSendNode
  391. ^ self parent isCascadeNode
  392. !
  393. isSendNode
  394. ^ true
  395. !
  396. stopOnStepping
  397. ^ true
  398. ! !
  399. !SendNode methodsFor: 'visiting'!
  400. accept: aVisitor
  401. ^ aVisitor visitSendNode: self
  402. ! !
  403. Node subclass: #SequenceNode
  404. instanceVariableNames: 'temps scope'
  405. package: 'Compiler-AST'!
  406. !SequenceNode commentStamp!
  407. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  408. !SequenceNode methodsFor: 'accessing'!
  409. scope
  410. ^ scope
  411. !
  412. scope: aLexicalScope
  413. scope := aLexicalScope
  414. !
  415. temps
  416. ^temps ifNil: [#()]
  417. !
  418. temps: aCollection
  419. temps := aCollection
  420. ! !
  421. !SequenceNode methodsFor: 'testing'!
  422. asBlockSequenceNode
  423. ^BlockSequenceNode new
  424. position: self position;
  425. nodes: self nodes;
  426. temps: self temps;
  427. yourself
  428. ! !
  429. !SequenceNode methodsFor: 'visiting'!
  430. accept: aVisitor
  431. ^ aVisitor visitSequenceNode: self
  432. ! !
  433. SequenceNode subclass: #BlockSequenceNode
  434. instanceVariableNames: ''
  435. package: 'Compiler-AST'!
  436. !BlockSequenceNode commentStamp!
  437. I represent an special sequence node for block scopes.!
  438. !BlockSequenceNode methodsFor: 'testing'!
  439. isBlockSequenceNode
  440. ^true
  441. ! !
  442. !BlockSequenceNode methodsFor: 'visiting'!
  443. accept: aVisitor
  444. ^ aVisitor visitBlockSequenceNode: self
  445. ! !
  446. Node subclass: #ValueNode
  447. instanceVariableNames: 'value'
  448. package: 'Compiler-AST'!
  449. !ValueNode commentStamp!
  450. I represent a value node.!
  451. !ValueNode methodsFor: 'accessing'!
  452. value
  453. ^value
  454. !
  455. value: anObject
  456. value := anObject
  457. ! !
  458. !ValueNode methodsFor: 'testing'!
  459. isImmutable
  460. ^ self value isImmutable
  461. !
  462. isValueNode
  463. ^true
  464. ! !
  465. !ValueNode methodsFor: 'visiting'!
  466. accept: aVisitor
  467. ^ aVisitor visitValueNode: self
  468. ! !
  469. !ValueNode methodsFor: 'xxxDoIt'!
  470. xxxDoIt ^[self stack] value
  471. ! !
  472. ValueNode subclass: #VariableNode
  473. instanceVariableNames: 'assigned binding'
  474. package: 'Compiler-AST'!
  475. !VariableNode commentStamp!
  476. I represent an variable node.!
  477. !VariableNode methodsFor: 'accessing'!
  478. alias
  479. ^ self binding alias
  480. !
  481. assigned
  482. ^assigned ifNil: [false]
  483. !
  484. assigned: aBoolean
  485. assigned := aBoolean
  486. !
  487. beAssigned
  488. self binding validateAssignment.
  489. assigned := true
  490. !
  491. binding
  492. ^ binding
  493. !
  494. binding: aScopeVar
  495. binding := aScopeVar
  496. ! !
  497. !VariableNode methodsFor: 'testing'!
  498. isImmutable
  499. ^false
  500. ! !
  501. !VariableNode methodsFor: 'visiting'!
  502. accept: aVisitor
  503. ^ aVisitor visitVariableNode: self
  504. ! !
  505. !Object methodsFor: '*Compiler-AST'!
  506. isNode
  507. ^ false
  508. ! !
  509. !CompiledMethod methodsFor: '*Compiler-AST'!
  510. ast
  511. self source ifEmpty: [ self error: 'Method source is empty' ].
  512. ^ Smalltalk current parse: self source
  513. ! !