Compiler-Core.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Smalltalk createPackage: 'Compiler-Core'!
  2. Object subclass: #AbstractCodeGenerator
  3. instanceVariableNames: 'currentClass currentPackage 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. currentClass
  9. ^ currentClass
  10. !
  11. currentClass: aClass
  12. currentClass := aClass
  13. !
  14. currentPackage
  15. ^ currentPackage
  16. !
  17. currentPackage: anObject
  18. currentPackage := anObject
  19. !
  20. pseudoVariables
  21. ^ Smalltalk pseudoVariableNames
  22. !
  23. source
  24. ^ source ifNil: [ '' ]
  25. !
  26. source: aString
  27. source := aString
  28. ! !
  29. !AbstractCodeGenerator methodsFor: 'compiling'!
  30. compileNode: aNode
  31. ^ self transformers
  32. inject: aNode
  33. into: [ :input :transformer | transformer value: input ]
  34. !
  35. transformers
  36. | dict |
  37. dict := self transformersDictionary.
  38. ^ dict keys asArray sort collect: [ :each | dict at: each ]
  39. !
  40. transformersDictionary
  41. self subclassResponsibility
  42. ! !
  43. AbstractCodeGenerator subclass: #CodeGenerator
  44. instanceVariableNames: 'transformersDictionary'
  45. package: 'Compiler-Core'!
  46. !CodeGenerator commentStamp!
  47. I am a basic code generator. I generate a valid JavaScript output, but no not perform any inlining.
  48. See `InliningCodeGenerator` for an optimized JavaScript code generation.!
  49. !CodeGenerator methodsFor: 'compiling'!
  50. irTranslator
  51. ^ self irTranslatorClass new
  52. currentClass: self currentClass;
  53. yourself
  54. !
  55. irTranslatorClass
  56. ^ IRJSTranslator
  57. !
  58. semanticAnalyzer
  59. ^ (SemanticAnalyzer on: self currentClass)
  60. thePackage: self currentPackage;
  61. yourself
  62. !
  63. transformersDictionary
  64. ^ transformersDictionary ifNil: [ transformersDictionary := Dictionary new
  65. at: '2000-semantic' put: self semanticAnalyzer;
  66. at: '5000-astToIr' put: self translator;
  67. at: '8000-irToJs' put: self irTranslator;
  68. yourself ]
  69. !
  70. translator
  71. ^ IRASTTranslator new
  72. source: self source;
  73. theClass: self currentClass;
  74. yourself
  75. ! !
  76. Object subclass: #Compiler
  77. instanceVariableNames: 'currentClass currentPackage source codeGeneratorClass'
  78. package: 'Compiler-Core'!
  79. !Compiler commentStamp!
  80. I provide the public interface for compiling Amber source code into JavaScript.
  81. The code generator used to produce JavaScript can be plugged with `#codeGeneratorClass`.
  82. The default code generator is an instance of `InlinedCodeGenerator`!
  83. !Compiler methodsFor: 'accessing'!
  84. codeGeneratorClass
  85. ^ codeGeneratorClass ifNil: [ InliningCodeGenerator ]
  86. !
  87. codeGeneratorClass: aClass
  88. codeGeneratorClass := aClass
  89. !
  90. currentClass
  91. ^ currentClass
  92. !
  93. currentClass: aClass
  94. currentClass := aClass
  95. !
  96. currentPackage
  97. ^ currentPackage
  98. !
  99. currentPackage: anObject
  100. currentPackage := anObject
  101. !
  102. source
  103. ^ source ifNil: [ '' ]
  104. !
  105. source: aString
  106. source := aString
  107. ! !
  108. !Compiler methodsFor: 'compiling'!
  109. compile: aString forClass: aClass protocol: anotherString
  110. ^ self
  111. source: aString;
  112. compileNode: (self parse: aString)
  113. forClass: aClass
  114. package: (aClass packageOfProtocol: anotherString)
  115. !
  116. compileExpression: aString on: anObject
  117. ^ self
  118. compile: 'xxxDoIt ^ [ ', aString, ' ] value'
  119. forClass: anObject class
  120. protocol: '**xxxDoIt'
  121. !
  122. compileNode: aNode
  123. | generator result |
  124. generator := self codeGeneratorClass new.
  125. generator
  126. source: self source;
  127. currentClass: self currentClass;
  128. currentPackage: self currentPackage.
  129. result := generator compileNode: aNode.
  130. ^ result
  131. !
  132. compileNode: aNode forClass: aClass package: aPackage
  133. ^ self
  134. currentClass: aClass;
  135. currentPackage: aPackage;
  136. compileNode: aNode
  137. !
  138. eval: aString
  139. <inlineJS: 'return eval(aString)'>
  140. !
  141. eval: aString forPackage: aPackage
  142. <inlineJS: 'return aPackage && aPackage.innerEval
  143. ? aPackage.innerEval(aString)
  144. : eval(aString)'>
  145. !
  146. evaluateExpression: aString
  147. "Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
  148. ^ self evaluateExpression: aString on: DoIt new
  149. !
  150. evaluateExpression: aString on: anObject
  151. "Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object"
  152. | result method |
  153. method := self eval: (self compileExpression: aString on: anObject).
  154. method protocol: '**xxxDoIt'.
  155. anObject class addCompiledMethod: method.
  156. result := anObject xxxDoIt.
  157. anObject class removeCompiledMethod: method.
  158. ^ result
  159. !
  160. install: aString forClass: aBehavior protocol: anotherString
  161. | compiledMethod |
  162. compiledMethod := self
  163. eval: (self compile: aString forClass: aBehavior protocol: anotherString)
  164. forPackage: (aBehavior packageOfProtocol: anotherString).
  165. ^ ClassBuilder new
  166. installMethod: compiledMethod
  167. forClass: aBehavior
  168. protocol: anotherString
  169. !
  170. parse: aString
  171. ^ Smalltalk parse: aString
  172. !
  173. parseExpression: aString
  174. ^ self parse: 'doIt ^ [ ', aString, ' ] value'
  175. !
  176. recompile: aClass
  177. aClass methodDictionary values
  178. do: [ :each | each methodClass = aClass ifTrue: [
  179. self
  180. install: each source
  181. forClass: aClass
  182. protocol: each protocol ] ]
  183. displayingProgress: 'Recompiling ', aClass name.
  184. aClass theMetaClass ifNotNil: [ :meta |
  185. meta = aClass ifFalse: [ self recompile: meta ] ]
  186. !
  187. recompileAll
  188. Smalltalk classes
  189. do: [ :each | self recompile: each ]
  190. displayingProgress: 'Compiling all classes...'
  191. ! !
  192. !Compiler class methodsFor: 'compiling'!
  193. recompile: aClass
  194. self new recompile: aClass
  195. !
  196. recompileAll
  197. Smalltalk classes do: [ :each |
  198. self recompile: each ]
  199. ! !
  200. Error subclass: #CompilerError
  201. instanceVariableNames: ''
  202. package: 'Compiler-Core'!
  203. !CompilerError commentStamp!
  204. I am the common superclass of all compiling errors.!
  205. Object subclass: #DoIt
  206. instanceVariableNames: ''
  207. package: 'Compiler-Core'!
  208. !DoIt commentStamp!
  209. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  210. Object subclass: #Evaluator
  211. instanceVariableNames: ''
  212. package: 'Compiler-Core'!
  213. !Evaluator commentStamp!
  214. I evaluate code against a receiver, dispatching #evaluate:on: to the receiver.!
  215. !Evaluator methodsFor: 'evaluating'!
  216. evaluate: aString context: aContext
  217. "Similar to #evaluate:for:, with the following differences:
  218. - instead of compiling and running `aString`, `aString` is interpreted using an `ASTInterpreter`
  219. - instead of evaluating against a receiver, evaluate in the context of `aContext`"
  220. | compiler ast |
  221. compiler := Compiler new.
  222. [ ast := compiler parseExpression: aString ]
  223. on: Error
  224. do: [ :ex | ^ Terminal alert: ex messageText ].
  225. (AISemanticAnalyzer on: aContext receiver class)
  226. context: aContext;
  227. visit: ast.
  228. ^ aContext evaluateNode: ast
  229. !
  230. evaluate: aString for: anObject
  231. ^ anObject evaluate: aString on: self
  232. !
  233. evaluate: aString receiver: anObject
  234. | compiler |
  235. compiler := Compiler new.
  236. [ compiler parseExpression: aString ]
  237. on: Error
  238. do: [ :ex | ^ Terminal alert: ex messageText ].
  239. ^ compiler evaluateExpression: aString on: anObject
  240. ! !
  241. !Evaluator class methodsFor: 'instance creation'!
  242. evaluate: aString for: anObject
  243. ^ self new evaluate: aString for: anObject
  244. ! !
  245. !String methodsFor: '*Compiler-Core'!
  246. asVariableName
  247. ^ (Smalltalk reservedWords includes: self)
  248. ifTrue: [ self, '_' ]
  249. ifFalse: [ self ]
  250. ! !