compiler.st 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. Object subclass: #Node
  2. instanceVariableNames: 'nodes'
  3. category: 'Compiler'!
  4. !Node methodsFor: 'accessing'!
  5. addNode: aNode
  6. self nodes add: aNode
  7. !
  8. nodes
  9. ^nodes ifNil: [nodes := Array new]
  10. ! !
  11. !Node methodsFor: 'building'!
  12. nodes: aCollection
  13. nodes := aCollection
  14. ! !
  15. !Node methodsFor: 'visiting'!
  16. accept: aVisitor
  17. aVisitor visitNode: self
  18. ! !
  19. Node subclass: #MethodNode
  20. instanceVariableNames: 'selector arguments source'
  21. category: 'Compiler'!
  22. !MethodNode methodsFor: 'accessing'!
  23. arguments
  24. ^arguments ifNil: [#()]
  25. !
  26. arguments: aCollection
  27. arguments := aCollection
  28. !
  29. selector
  30. ^selector
  31. !
  32. selector: aString
  33. selector := aString
  34. !
  35. source
  36. ^source
  37. !
  38. source: aString
  39. source := aString
  40. ! !
  41. !MethodNode methodsFor: 'visiting'!
  42. accept: aVisitor
  43. aVisitor visitMethodNode: self
  44. ! !
  45. Node subclass: #SendNode
  46. instanceVariableNames: 'selector arguments receiver'
  47. category: 'Compiler'!
  48. !SendNode methodsFor: 'accessing'!
  49. arguments
  50. ^arguments ifNil: [arguments := #()]
  51. !
  52. arguments: aCollection
  53. arguments := aCollection
  54. !
  55. cascadeNodeWithMessages: aCollection
  56. | first |
  57. first := SendNode new
  58. selector: self selector;
  59. arguments: self arguments;
  60. yourself.
  61. ^CascadeNode new
  62. receiver: self receiver;
  63. nodes: (Array with: first), aCollection;
  64. yourself
  65. !
  66. receiver
  67. ^receiver
  68. !
  69. receiver: aNode
  70. receiver := aNode
  71. !
  72. selector
  73. ^selector
  74. !
  75. selector: aString
  76. selector := aString
  77. !
  78. valueForReceiver: anObject
  79. ^SendNode new
  80. receiver: (self receiver
  81. ifNil: [anObject]
  82. ifNotNil: [self receiver valueForReceiver: anObject]);
  83. selector: self selector;
  84. arguments: self arguments;
  85. yourself
  86. ! !
  87. !SendNode methodsFor: 'visiting'!
  88. accept: aVisitor
  89. aVisitor visitSendNode: self
  90. ! !
  91. Node subclass: #CascadeNode
  92. instanceVariableNames: 'receiver'
  93. category: 'Compiler'!
  94. !CascadeNode methodsFor: 'accessing'!
  95. receiver
  96. ^receiver
  97. !
  98. receiver: aNode
  99. receiver := aNode
  100. ! !
  101. !CascadeNode methodsFor: 'visiting'!
  102. accept: aVisitor
  103. aVisitor visitCascadeNode: self
  104. ! !
  105. Node subclass: #AssignmentNode
  106. instanceVariableNames: 'left right'
  107. category: 'Compiler'!
  108. !AssignmentNode methodsFor: 'accessing'!
  109. left
  110. ^left
  111. !
  112. left: aNode
  113. left := aNode
  114. !
  115. right
  116. ^right
  117. !
  118. right: aNode
  119. right := aNode
  120. ! !
  121. !AssignmentNode methodsFor: 'visiting'!
  122. accept: aVisitor
  123. aVisitor visitAssignmentNode: self
  124. ! !
  125. Node subclass: #BlockNode
  126. instanceVariableNames: 'parameters'
  127. category: 'Compiler'!
  128. !BlockNode methodsFor: 'accessing'!
  129. parameters
  130. ^parameters ifNil: [parameters := Array new]
  131. !
  132. parameters: aCollection
  133. parameters := aCollection
  134. ! !
  135. !BlockNode methodsFor: 'visiting'!
  136. accept: aVisitor
  137. aVisitor visitBlockNode: self
  138. ! !
  139. Node subclass: #SequenceNode
  140. instanceVariableNames: 'temps'
  141. category: 'Compiler'!
  142. !SequenceNode methodsFor: 'accessing'!
  143. temps
  144. ^temps ifNil: [#()]
  145. !
  146. temps: aCollection
  147. temps := aCollection
  148. ! !
  149. !SequenceNode methodsFor: 'testing'!
  150. asBlockSequenceNode
  151. ^BlockSequenceNode new
  152. nodes: self nodes;
  153. temps: self temps;
  154. yourself
  155. ! !
  156. !SequenceNode methodsFor: 'visiting'!
  157. accept: aVisitor
  158. aVisitor visitSequenceNode: self
  159. ! !
  160. SequenceNode subclass: #BlockSequenceNode
  161. instanceVariableNames: ''
  162. category: 'Compiler'!
  163. !BlockSequenceNode methodsFor: 'visiting'!
  164. accept: aVisitor
  165. aVisitor visitBlockSequenceNode: self
  166. ! !
  167. Node subclass: #ReturnNode
  168. instanceVariableNames: ''
  169. category: 'Compiler'!
  170. !ReturnNode methodsFor: 'visiting'!
  171. accept: aVisitor
  172. aVisitor visitReturnNode: self
  173. ! !
  174. Node subclass: #ValueNode
  175. instanceVariableNames: 'value'
  176. category: 'Compiler'!
  177. !ValueNode methodsFor: 'accessing'!
  178. value
  179. ^value
  180. !
  181. value: anObject
  182. value := anObject
  183. ! !
  184. !ValueNode methodsFor: 'visiting'!
  185. accept: aVisitor
  186. aVisitor visitValueNode: self
  187. ! !
  188. ValueNode subclass: #VariableNode
  189. instanceVariableNames: ''
  190. category: 'Compiler'!
  191. !VariableNode methodsFor: 'visiting'!
  192. accept: aVisitor
  193. aVisitor visitVariableNode: self
  194. ! !
  195. VariableNode subclass: #ClassReferenceNode
  196. instanceVariableNames: ''
  197. category: 'Compiler'!
  198. !ClassReferenceNode methodsFor: 'visiting'!
  199. accept: aVisitor
  200. aVisitor visitClassReferenceNode: self
  201. ! !
  202. Node subclass: #JSStatementNode
  203. instanceVariableNames: 'source'
  204. category: 'Compiler'!
  205. !JSStatementNode methodsFor: 'accessing'!
  206. source
  207. ^source ifNil: ['']
  208. !
  209. source: aString
  210. source := aString
  211. ! !
  212. !JSStatementNode methodsFor: 'visiting'!
  213. accept: aVisitor
  214. aVisitor visitJSStatementNode: self
  215. ! !
  216. Object subclass: #NodeVisitor
  217. instanceVariableNames: ''
  218. category: 'Compiler'!
  219. !NodeVisitor methodsFor: 'visiting'!
  220. visit: aNode
  221. aNode accept: self
  222. !
  223. visitAssignmentNode: aNode
  224. self visitNode: aNode
  225. !
  226. visitBlockNode: aNode
  227. self visitNode: aNode
  228. !
  229. visitBlockSequenceNode: aNode
  230. self visitSequenceNode: aNode
  231. !
  232. visitCascadeNode: aNode
  233. self visitNode: aNode
  234. !
  235. visitClassReferenceNode: aNode
  236. self
  237. nextPutAll: 'smalltalk.';
  238. nextPutAll: aNode value
  239. !
  240. visitJSStatementNode: aNode
  241. self
  242. nextPutAll: 'function(){';
  243. nextPutAll: aNode source;
  244. nextPutAll: '})()'
  245. !
  246. visitMethodNode: aNode
  247. self visitNode: aNode
  248. !
  249. visitNode: aNode
  250. !
  251. visitReturnNode: aNode
  252. self visitNode: aNode
  253. !
  254. visitSendNode: aNode
  255. self visitNode: aNode
  256. !
  257. visitSequenceNode: aNode
  258. self visitNode: aNode
  259. !
  260. visitValueNode: aNode
  261. self visitNode: aNode
  262. !
  263. visitVariableNode: aNode
  264. ! !
  265. NodeVisitor subclass: #Compiler
  266. instanceVariableNames: 'stream nestedBlocks earlyReturn currentClass currentSelector'
  267. category: 'Compiler'!
  268. !Compiler methodsFor: 'accessing'!
  269. currentClass
  270. ^currentClass
  271. !
  272. currentClass: aClass
  273. currentClass := aClass
  274. !
  275. parser
  276. ^SmalltalkParser new
  277. ! !
  278. !Compiler methodsFor: 'compiling'!
  279. compile: aString
  280. ^self compileNode: (self parser parse: aString readStream)
  281. !
  282. compile: aString forClass: aClass
  283. self currentClass: aClass.
  284. ^self compile: aString
  285. !
  286. compileExpression: aString
  287. self currentClass: DoIt.
  288. ^self compile: 'doIt ^[', aString, '] value'
  289. !
  290. compileNode: aNode
  291. stream := '' writeStream.
  292. self visit: aNode.
  293. ^stream contents
  294. !
  295. eval: aString
  296. ^{'return eval(aString);'}
  297. !
  298. load: aString forClass: aClass
  299. ^self eval: (self compile: aString forClass: aClass)
  300. !
  301. loadExpression: aString
  302. DoIt addCompiledMethod: (self eval: (self compileExpression: aString)).
  303. ^DoIt new doIt
  304. ! !
  305. !Compiler methodsFor: 'initialization'!
  306. initialize
  307. super initialize.
  308. stream := '' writeStream
  309. ! !
  310. !Compiler methodsFor: 'visiting'!
  311. visit: aNode
  312. aNode accept: self
  313. !
  314. visitAssignmentNode: aNode
  315. self visit: aNode left.
  316. stream nextPutAll: '='.
  317. self visit: aNode right
  318. !
  319. visitBlockNode: aNode
  320. stream nextPutAll: '(function('.
  321. aNode parameters
  322. do: [:each |
  323. stream nextPutAll: each]
  324. separatedBy: [stream nextPutAll: ', '].
  325. stream nextPutAll: '){'.
  326. aNode nodes do: [:each | self visit: each].
  327. stream nextPutAll: '})'
  328. !
  329. visitBlockSequenceNode: aNode
  330. | index |
  331. nestedBlocks := nestedBlocks + 1.
  332. aNode nodes isEmpty
  333. ifTrue: [
  334. stream nextPutAll: 'return nil;']
  335. ifFalse: [
  336. aNode temps do: [:each |
  337. stream nextPutAll: 'var ', each, '=nil;'.
  338. stream nextPutAll: String cr].
  339. index := 0.
  340. aNode nodes do: [:each |
  341. index := index + 1.
  342. index = aNode nodes size ifTrue: [
  343. stream nextPutAll: 'return '].
  344. self visit: each.
  345. stream nextPutAll: ';']].
  346. nestedBlocks := nestedBlocks - 1
  347. !
  348. visitCascadeNode: aNode
  349. | index |
  350. index := 0.
  351. stream nextPutAll: '(function($rec){'.
  352. aNode nodes do: [:each |
  353. index := index + 1.
  354. index = aNode nodes size ifTrue: [
  355. stream nextPutAll: 'return '].
  356. each receiver: (VariableNode new value: '$rec').
  357. self visit: each.
  358. stream nextPutAll: ';'].
  359. stream nextPutAll: '})('.
  360. self visit: aNode receiver.
  361. stream nextPutAll: ')'
  362. !
  363. visitClassReferenceNode: aNode
  364. stream
  365. nextPutAll: 'smalltalk.';
  366. nextPutAll: aNode value
  367. !
  368. visitJSStatementNode: aNode
  369. stream nextPutAll: '(function(){'.
  370. stream nextPutAll: (aNode source value replace: '''''' with: '''').
  371. stream nextPutAll: '})()'
  372. !
  373. visitMethodNode: aNode
  374. | str currentSelector |
  375. currentSelector := aNode selector asSelector.
  376. nestedBlocks := 0.
  377. earlyReturn := false.
  378. stream
  379. nextPutAll: 'smalltalk.method({', String cr;
  380. nextPutAll: 'selector: "', aNode selector, '",', String cr;
  381. nextPutAll: 'source: unescape("', aNode source escaped, '"),', String cr;
  382. nextPutAll: 'fn: function('.
  383. aNode arguments
  384. do: [:each | stream nextPutAll: each]
  385. separatedBy: [stream nextPutAll: ', '].
  386. stream
  387. nextPutAll: '){', String cr;
  388. nextPutAll: 'var self=this;', String cr.
  389. str := stream.
  390. stream := '' writeStream.
  391. aNode nodes do: [:each |
  392. self visit: each].
  393. earlyReturn ifTrue: [
  394. str nextPutAll: 'try{'].
  395. str nextPutAll: stream contents.
  396. stream := str.
  397. stream
  398. nextPutAll: String cr;
  399. nextPutAll: 'return self;'.
  400. earlyReturn ifTrue: [
  401. stream nextPutAll: String cr, '} catch(e) {if(e.name === ''stReturn'' && e.selector === ', currentSelector printString, '){return e.fn()} throw(e)}'].
  402. stream
  403. nextPutAll: '}', String cr;
  404. nextPutAll: '})'
  405. !
  406. visitReturnNode: aNode
  407. nestedBlocks > 0 ifTrue: [
  408. earlyReturn := true].
  409. earlyReturn
  410. ifTrue: [
  411. stream
  412. nextPutAll: '(function(){throw(';
  413. nextPutAll: '{name: ''stReturn'', selector: ';
  414. nextPutAll: currentSelector printString;
  415. nextPutAll: ', fn: function(){return ']
  416. ifFalse: [stream nextPutAll: 'return '].
  417. aNode nodes do: [:each |
  418. self visit: each].
  419. earlyReturn ifTrue: [
  420. stream nextPutAll: '}})})()']
  421. !
  422. visitSendNode: aNode
  423. | str |
  424. str := stream.
  425. stream := '' writeStream.
  426. self visit: aNode receiver.
  427. stream contents = 'super'
  428. ifTrue: [
  429. stream := str.
  430. stream
  431. nextPutAll: 'self.klass.superclass.fn.prototype[''';
  432. nextPutAll: aNode selector asSelector;
  433. nextPutAll: '''].apply(self, ['.
  434. aNode arguments
  435. do: [:each | self visit: each]
  436. separatedBy: [stream nextPutAll: ','].
  437. stream nextPutAll: '])']
  438. ifFalse: [
  439. str nextPutAll: stream contents.
  440. stream := str.
  441. stream nextPutAll: '.', aNode selector asSelector, '('.
  442. aNode arguments
  443. do: [:each | self visit: each]
  444. separatedBy: [stream nextPutAll: ','].
  445. stream nextPutAll: ')']
  446. !
  447. visitSequenceNode: aNode
  448. aNode temps do: [:each |
  449. stream nextPutAll: 'var ', each, '=nil;'.
  450. stream nextPutAll: String cr].
  451. aNode nodes do: [:each |
  452. self visit: each.
  453. stream nextPutAll: ';']
  454. separatedBy: [stream nextPutAll: String cr]
  455. !
  456. visitValueNode: aNode
  457. stream nextPutAll: aNode value asJavascript
  458. !
  459. visitVariableNode: aNode
  460. (self currentClass instVarNames includes: aNode value)
  461. ifTrue: [stream nextPutAll: 'self[''@', aNode value, ''']']
  462. ifFalse: [stream nextPutAll: aNode value]
  463. ! !
  464. Object subclass: #DoIt
  465. instanceVariableNames: ''
  466. category: 'Compiler'!