Compiler-Core.st 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. Smalltalk current createPackage: 'Compiler-Core'!
  2. Object subclass: #AbstractCodeGenerator
  3. instanceVariableNames: 'currentClass source'
  4. package: 'Compiler-Core'!
  5. !AbstractCodeGenerator commentStamp!
  6. I am the abstract super class of all code generators and provide their common API.!
  7. !AbstractCodeGenerator methodsFor: 'accessing'!
  8. classNameFor: aClass
  9. ^ aClass isMetaclass
  10. ifTrue: [ aClass instanceClass name, '.klass' ]
  11. ifFalse: [
  12. aClass isNil
  13. ifTrue: [ 'nil' ]
  14. ifFalse: [ aClass name ]]
  15. !
  16. currentClass
  17. ^ currentClass
  18. !
  19. currentClass: aClass
  20. currentClass := aClass
  21. !
  22. pseudoVariables
  23. ^ Smalltalk current pseudoVariableNames
  24. !
  25. safeVariableNameFor: aString
  26. ^ (Smalltalk current reservedWords includes: aString)
  27. ifTrue: [ aString, '_' ]
  28. ifFalse: [ aString ]
  29. !
  30. source
  31. ^ source ifNil: [ '' ]
  32. !
  33. source: aString
  34. source := aString
  35. ! !
  36. !AbstractCodeGenerator methodsFor: 'compiling'!
  37. compileNode: aNode
  38. self subclassResponsibility
  39. ! !
  40. AbstractCodeGenerator subclass: #CodeGenerator
  41. instanceVariableNames: ''
  42. package: 'Compiler-Core'!
  43. !CodeGenerator commentStamp!
  44. I am a basic code generator. I generate a valid JavaScript output, but no not perform any inlining.
  45. See `InliningCodeGenerator` for an optimized JavaScript code generation.!
  46. !CodeGenerator methodsFor: 'compiling'!
  47. compileNode: aNode
  48. | ir stream |
  49. self semanticAnalyzer visit: aNode.
  50. ir := self translator visit: aNode.
  51. ^ self irTranslator
  52. currentClass: self currentClass;
  53. visit: ir;
  54. contents
  55. !
  56. irTranslator
  57. ^ IRJSTranslator new
  58. !
  59. semanticAnalyzer
  60. ^ SemanticAnalyzer on: self currentClass
  61. !
  62. translator
  63. ^ IRASTTranslator new
  64. source: self source;
  65. theClass: self currentClass;
  66. yourself
  67. ! !
  68. Object subclass: #Compiler
  69. instanceVariableNames: 'currentClass source unknownVariables codeGeneratorClass'
  70. package: 'Compiler-Core'!
  71. !Compiler commentStamp!
  72. I provide the public interface for compiling Amber source code into JavaScript.
  73. The code generator used to produce JavaScript can be plugged with `#codeGeneratorClass`.
  74. The default code generator is an instance of `InlinedCodeGenerator`!
  75. !Compiler methodsFor: 'accessing'!
  76. codeGeneratorClass
  77. ^ codeGeneratorClass ifNil: [ InliningCodeGenerator ]
  78. !
  79. codeGeneratorClass: aClass
  80. codeGeneratorClass := aClass
  81. !
  82. currentClass
  83. ^ currentClass
  84. !
  85. currentClass: aClass
  86. currentClass := aClass
  87. !
  88. source
  89. ^ source ifNil: [ '' ]
  90. !
  91. source: aString
  92. source := aString
  93. !
  94. unknownVariables
  95. ^ unknownVariables
  96. !
  97. unknownVariables: aCollection
  98. unknownVariables := aCollection
  99. ! !
  100. !Compiler methodsFor: 'compiling'!
  101. compile: aString
  102. ^ self compileNode: (self parse: aString)
  103. !
  104. compile: aString forClass: aClass
  105. self currentClass: aClass.
  106. self source: aString.
  107. ^ self compile: aString
  108. !
  109. compileExpression: aString
  110. self currentClass: DoIt.
  111. self source: 'doIt ^ [ ', aString, ' ] value'.
  112. ^ self compileNode: (self parse: self source)
  113. !
  114. compileExpression: aString on: anObject
  115. self currentClass: anObject class.
  116. self source: 'xxxDoIt ^ [ ', aString, ' ] value'.
  117. ^ self compileNode: (self parse: self source)
  118. !
  119. compileNode: aNode
  120. | generator result |
  121. generator := self codeGeneratorClass new.
  122. generator
  123. source: self source;
  124. currentClass: self currentClass.
  125. result := generator compileNode: aNode.
  126. self unknownVariables: #().
  127. ^ result
  128. !
  129. eval: aString
  130. <return eval(aString)>
  131. !
  132. evaluateExpression: aString
  133. "Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
  134. ^ self evaluateExpression: aString on: DoIt new
  135. !
  136. evaluateExpression: aString on: anObject
  137. "Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object"
  138. | result method |
  139. method := self eval: (self compileExpression: aString on: anObject).
  140. method category: 'xxxDoIt'.
  141. anObject class addCompiledMethod: method.
  142. result := anObject xxxDoIt.
  143. anObject class removeCompiledMethod: method.
  144. ^ result
  145. !
  146. install: aString forClass: aBehavior category: anotherString
  147. ^ ClassBuilder new
  148. installMethod: (self eval: (self compile: aString forClass: aBehavior))
  149. forClass: aBehavior
  150. category: anotherString
  151. !
  152. parse: aString
  153. ^ Smalltalk current parse: aString
  154. !
  155. parseExpression: aString
  156. ^ self parse: 'doIt ^ [ ', aString, ' ] value'
  157. !
  158. recompile: aClass
  159. aClass methodDictionary values
  160. do: [ :each | self install: each source forClass: aClass category: each category ]
  161. displayingProgress: 'Recompiling ', aClass name.
  162. "self setupClass: aClass."
  163. aClass isMetaclass ifFalse: [ self recompile: aClass class ]
  164. !
  165. recompileAll
  166. Smalltalk current classes
  167. do: [ :each | self recompile: each ]
  168. displayingProgress: 'Compiling all classes...'
  169. ! !
  170. !Compiler class methodsFor: 'compiling'!
  171. recompile: aClass
  172. self new recompile: aClass
  173. !
  174. recompileAll
  175. Smalltalk current classes do: [ :each |
  176. self recompile: each ]
  177. ! !
  178. Object subclass: #DoIt
  179. instanceVariableNames: ''
  180. package: 'Compiler-Core'!
  181. !DoIt commentStamp!
  182. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  183. Object subclass: #NodeVisitor
  184. instanceVariableNames: ''
  185. package: 'Compiler-Core'!
  186. !NodeVisitor commentStamp!
  187. I am the abstract super class of all AST node visitors.!
  188. !NodeVisitor methodsFor: 'visiting'!
  189. visit: aNode
  190. ^ aNode accept: self
  191. !
  192. visitAll: aCollection
  193. ^ aCollection collect: [ :each | self visit: each ]
  194. !
  195. visitAssignmentNode: aNode
  196. ^ self visitNode: aNode
  197. !
  198. visitBlockNode: aNode
  199. ^ self visitNode: aNode
  200. !
  201. visitBlockSequenceNode: aNode
  202. ^ self visitSequenceNode: aNode
  203. !
  204. visitCascadeNode: aNode
  205. ^ self visitNode: aNode
  206. !
  207. visitDynamicArrayNode: aNode
  208. ^ self visitNode: aNode
  209. !
  210. visitDynamicDictionaryNode: aNode
  211. ^ self visitNode: aNode
  212. !
  213. visitJSStatementNode: aNode
  214. ^ self visitNode: aNode
  215. !
  216. visitMethodNode: aNode
  217. ^ self visitNode: aNode
  218. !
  219. visitNode: aNode
  220. ^ self visitAll: aNode nodes
  221. !
  222. visitReturnNode: aNode
  223. ^ self visitNode: aNode
  224. !
  225. visitSendNode: aNode
  226. ^ self visitNode: aNode
  227. !
  228. visitSequenceNode: aNode
  229. ^ self visitNode: aNode
  230. !
  231. visitValueNode: aNode
  232. ^ self visitNode: aNode
  233. !
  234. visitVariableNode: aNode
  235. ^ self visitNode: aNode
  236. ! !