1
0

Compiler-Core.st 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 protocol: 'xxxDoIt'.
  141. anObject class addCompiledMethod: method.
  142. result := anObject xxxDoIt.
  143. anObject class removeCompiledMethod: method.
  144. ^ result
  145. !
  146. install: aString forClass: aBehavior protocol: anotherString
  147. ^ ClassBuilder new
  148. installMethod: (self eval: (self compile: aString forClass: aBehavior))
  149. forClass: aBehavior
  150. protocol: 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 |
  161. self
  162. install: each source
  163. forClass: aClass
  164. protocol: each protocol ]
  165. displayingProgress: 'Recompiling ', aClass name.
  166. aClass isMetaclass ifFalse: [ self recompile: aClass class ]
  167. !
  168. recompileAll
  169. Smalltalk current classes
  170. do: [ :each | self recompile: each ]
  171. displayingProgress: 'Compiling all classes...'
  172. ! !
  173. !Compiler class methodsFor: 'compiling'!
  174. recompile: aClass
  175. self new recompile: aClass
  176. !
  177. recompileAll
  178. Smalltalk current classes do: [ :each |
  179. self recompile: each ]
  180. ! !
  181. Object subclass: #DoIt
  182. instanceVariableNames: ''
  183. package: 'Compiler-Core'!
  184. !DoIt commentStamp!
  185. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  186. Object subclass: #NodeVisitor
  187. instanceVariableNames: ''
  188. package: 'Compiler-Core'!
  189. !NodeVisitor commentStamp!
  190. I am the abstract super class of all AST node visitors.!
  191. !NodeVisitor methodsFor: 'visiting'!
  192. visit: aNode
  193. ^ aNode accept: self
  194. !
  195. visitAll: aCollection
  196. ^ aCollection collect: [ :each | self visit: each ]
  197. !
  198. visitAssignmentNode: aNode
  199. ^ self visitNode: aNode
  200. !
  201. visitBlockNode: aNode
  202. ^ self visitNode: aNode
  203. !
  204. visitBlockSequenceNode: aNode
  205. ^ self visitSequenceNode: aNode
  206. !
  207. visitCascadeNode: aNode
  208. ^ self visitNode: aNode
  209. !
  210. visitDynamicArrayNode: aNode
  211. ^ self visitNode: aNode
  212. !
  213. visitDynamicDictionaryNode: aNode
  214. ^ self visitNode: aNode
  215. !
  216. visitJSStatementNode: aNode
  217. ^ self visitNode: aNode
  218. !
  219. visitMethodNode: aNode
  220. ^ self visitNode: aNode
  221. !
  222. visitNode: aNode
  223. ^ self visitAll: aNode nodes
  224. !
  225. visitReturnNode: aNode
  226. ^ self visitNode: aNode
  227. !
  228. visitSendNode: aNode
  229. ^ self visitNode: aNode
  230. !
  231. visitSequenceNode: aNode
  232. ^ self visitNode: aNode
  233. !
  234. visitValueNode: aNode
  235. ^ self visitNode: aNode
  236. !
  237. visitVariableNode: aNode
  238. ^ self visitNode: aNode
  239. ! !