Compiler-Core.st 5.6 KB

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