Compiler-Core.st 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. Smalltalk createPackage: 'Compiler-Core'!
  2. (Smalltalk packageAt: 'Compiler-Core' ifAbsent: [ self error: 'Package not created: Compiler-Core' ]) imports: {'smalltalkParser' -> 'amber/parser'}!
  3. Object subclass: #AbstractCodeGenerator
  4. slots: {#currentClass. #currentPackage. #source}
  5. package: 'Compiler-Core'!
  6. !AbstractCodeGenerator commentStamp!
  7. I am the abstract super class of all code generators and provide their common API.!
  8. !AbstractCodeGenerator methodsFor: 'accessing'!
  9. currentClass
  10. ^ currentClass
  11. !
  12. currentClass: aClass
  13. currentClass := aClass
  14. !
  15. currentPackage
  16. ^ currentPackage
  17. !
  18. currentPackage: anObject
  19. currentPackage := anObject
  20. !
  21. pseudoVariables
  22. ^ Smalltalk pseudoVariableNames
  23. !
  24. source
  25. ^ source ifNil: [ '' ]
  26. !
  27. source: aString
  28. source := aString
  29. ! !
  30. !AbstractCodeGenerator methodsFor: 'compiling'!
  31. compileNode: aNode
  32. ^ self transformers
  33. inject: aNode
  34. into: [ :input :transformer | transformer value: input ]
  35. !
  36. transformers
  37. | dict |
  38. dict := self transformersDictionary.
  39. ^ dict keys asArray sort collect: [ :each | dict at: each ]
  40. !
  41. transformersDictionary
  42. self subclassResponsibility
  43. ! !
  44. AbstractCodeGenerator subclass: #AstGenerator
  45. slots: {#transformersDictionary}
  46. package: 'Compiler-Core'!
  47. !AstGenerator commentStamp!
  48. I am a very basic code generator.
  49. I generate semantically augmented abstract syntax tree,
  50. Some initial pragmas (eg. #inlineJS:) are applied to transform the tree.!
  51. !AstGenerator methodsFor: 'compiling'!
  52. semanticAnalyzer
  53. ^ (SemanticAnalyzer on: self currentClass)
  54. thePackage: self currentPackage;
  55. yourself
  56. !
  57. semanticAstPragmator
  58. ^ AstSemanticPragmator new
  59. !
  60. transformersDictionary
  61. ^ transformersDictionary ifNil: [ transformersDictionary := Dictionary new
  62. at: '2000-semantic' put: self semanticAnalyzer;
  63. at: '2500-semanticPragmas' put: self semanticAstPragmator;
  64. yourself ]
  65. ! !
  66. AstGenerator subclass: #CodeGenerator
  67. slots: {}
  68. package: 'Compiler-Core'!
  69. !CodeGenerator commentStamp!
  70. I am a basic code generator. I generate a valid JavaScript output, but do not perform any inlining.
  71. See `InliningCodeGenerator` for an optimized JavaScript code generation.!
  72. !CodeGenerator methodsFor: 'compiling'!
  73. irTranslator
  74. ^ self irTranslatorClass new
  75. currentClass: self currentClass;
  76. yourself
  77. !
  78. irTranslatorClass
  79. ^ IRJSTranslator
  80. !
  81. lateIRPragmator
  82. ^ IRLatePragmator new
  83. !
  84. transformersDictionary
  85. ^ transformersDictionary ifNil: [ transformersDictionary := super transformersDictionary
  86. at: '5000-astToIr' put: self translator;
  87. at: '8000-irToJs' put: self irTranslator;
  88. at: '9000-latePragmas' put: self lateIRPragmator;
  89. yourself ]
  90. !
  91. translator
  92. ^ IRASTTranslator new
  93. source: self source;
  94. theClass: self currentClass;
  95. yourself
  96. ! !
  97. Object subclass: #Compiler
  98. slots: {#currentPackage. #codeGeneratorClass. #codeGenerator}
  99. package: 'Compiler-Core'!
  100. !Compiler commentStamp!
  101. I provide the public interface for compiling Amber source code into JavaScript.
  102. The code generator used to produce JavaScript can be plugged with `#codeGeneratorClass`.
  103. The default code generator is an instance of `InlinedCodeGenerator`!
  104. !Compiler methodsFor: 'accessing'!
  105. cleanCodeGenerator
  106. codeGenerator := nil
  107. !
  108. codeGenerator
  109. ^ codeGenerator
  110. !
  111. codeGenerator: anObject
  112. codeGenerator := anObject
  113. !
  114. codeGeneratorClass
  115. ^ codeGeneratorClass ifNil: [ InliningCodeGenerator ]
  116. !
  117. codeGeneratorClass: aClass
  118. codeGeneratorClass := aClass
  119. !
  120. currentPackage
  121. ^ currentPackage
  122. !
  123. currentPackage: anObject
  124. currentPackage := anObject
  125. ! !
  126. !Compiler methodsFor: 'compiling'!
  127. ast: aString forClass: aClass protocol: anotherString
  128. ^ self
  129. codeGeneratorClass: AstGenerator;
  130. start: aString forClass: aClass protocol: anotherString;
  131. compileNode: (self parse: aString)
  132. !
  133. compile: aString forClass: aClass protocol: anotherString
  134. | compilationResult result pragmas closureFactory |
  135. compilationResult := self
  136. start: aString forClass: aClass protocol: anotherString;
  137. compileNode: (self parse: aString).
  138. closureFactory := self
  139. eval: (self wrappedSourceOf: compilationResult)
  140. forPackage: self currentPackage.
  141. result := Smalltalk core method: #{
  142. #selector -> compilationResult selector.
  143. #protocol -> anotherString.
  144. #source -> aString.
  145. #messageSends -> compilationResult messageSends asArray.
  146. #args -> compilationResult arguments.
  147. #referencedClasses -> compilationResult classReferences asArray.
  148. } withFactory: closureFactory.
  149. result pragmas: compilationResult pragmas.
  150. ^ result
  151. !
  152. compileNode: aNode
  153. | result |
  154. result := self codeGenerator compileNode: aNode.
  155. self cleanCodeGenerator.
  156. ^ result
  157. !
  158. eval: aString
  159. <inlineJS: 'return eval(aString)'>
  160. !
  161. eval: aString forPackage: aPackage
  162. ^ aPackage
  163. ifNil: [ self eval: aString ]
  164. ifNotNil: [ aPackage eval: aString ]
  165. !
  166. evaluateExpression: aString
  167. "Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
  168. ^ self evaluateExpression: aString on: DoIt new
  169. !
  170. evaluateExpression: aString on: anObject
  171. "Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object"
  172. | result method |
  173. method := self
  174. install: (self sourceForExpression: aString)
  175. forClass: anObject class
  176. protocol: '**xxxDoIt'.
  177. result := anObject xxxDoIt.
  178. anObject class removeCompiledMethod: method.
  179. ^ result
  180. !
  181. install: aString forClass: aBehavior protocol: anotherString
  182. | compiledMethod |
  183. compiledMethod := self compile: aString forClass: aBehavior protocol: anotherString.
  184. aBehavior addCompiledMethod: compiledMethod.
  185. ^ compiledMethod
  186. !
  187. parse: aString
  188. | result |
  189. [ result := self basicParse: aString ]
  190. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  191. ^ result
  192. !
  193. parseExpression: aString
  194. ^ self parse: (self sourceForExpression: aString)
  195. !
  196. recompile: aClass
  197. aClass includingPossibleMetaDo: [ :eachSide |
  198. eachSide methodDictionary values
  199. do: [ :each | each origin = eachSide ifTrue: [
  200. self
  201. install: each source
  202. forClass: eachSide
  203. protocol: each protocol ] ]
  204. displayingProgress: 'Recompiling ', eachSide name ]
  205. !
  206. recompileAll
  207. Smalltalk classes
  208. do: [ :each | self recompile: each ]
  209. displayingProgress: 'Compiling all classes...'
  210. !
  211. sourceForExpression: aString
  212. ^ 'xxxDoIt ^ [ ', aString, ' ] value'
  213. !
  214. start: aString forClass: aClass protocol: anotherString
  215. | package |
  216. package := aClass packageOfProtocol: anotherString.
  217. self
  218. currentPackage: package;
  219. codeGenerator: (self codeGeneratorClass new
  220. source: aString;
  221. currentClass: aClass;
  222. currentPackage: package;
  223. yourself)
  224. !
  225. transformerAt: aString put: anObject
  226. self codeGenerator transformersDictionary at: aString put: anObject
  227. ! !
  228. !Compiler methodsFor: 'error handling'!
  229. error: aString
  230. CompilerError signal: aString
  231. !
  232. parseError: anException parsing: aString
  233. (anException basicAt: 'location')
  234. ifNil: [ ^ anException pass ]
  235. ifNotNil: [ :loc |
  236. ^ ParseError new
  237. messageText:
  238. 'Parse error on line ', loc start line ,
  239. ' column ' , loc start column ,
  240. ' : Unexpected character ', (anException basicAt: 'found');
  241. yourself ]
  242. ! !
  243. !Compiler methodsFor: 'private'!
  244. basicParse: aString
  245. ^ smalltalkParser parse: aString
  246. !
  247. wrappedSourceOf: anIRMethod
  248. ^ anIRMethod attachments
  249. ifEmpty: [
  250. '(function ($methodClass){ return ',
  251. anIRMethod compiledSource,
  252. '; })' ]
  253. ifNotEmpty: [ :attachments |
  254. '(function ($methodClass){ return Object.defineProperty(',
  255. anIRMethod compiledSource,
  256. ',"a$atx",{enumerable:false,configurable:true,writable:true,value:',
  257. attachments asJavaScriptSource,
  258. '}); })' ]
  259. ! !
  260. !Compiler class methodsFor: 'compiling'!
  261. recompile: aClass
  262. self new recompile: aClass
  263. !
  264. recompileAll
  265. Smalltalk classes do: [ :each |
  266. self recompile: each ]
  267. ! !
  268. !Compiler class methodsFor: 'evaluating'!
  269. eval: aString
  270. ^ self new eval: aString
  271. ! !
  272. !Compiler class methodsFor: 'initialization'!
  273. initialize
  274. "TODO remove, backward compat"
  275. Smalltalk globals at: #SmalltalkParser put: smalltalkParser
  276. ! !
  277. !Compiler class methodsFor: 'parsing'!
  278. parse: aString
  279. ^ self new parse: aString
  280. !
  281. pseudoVariableNames
  282. ^ PseudoVar dictionary keys asArray
  283. ! !
  284. Object subclass: #DoIt
  285. slots: {}
  286. package: 'Compiler-Core'!
  287. !DoIt commentStamp!
  288. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  289. Object subclass: #Evaluator
  290. slots: {}
  291. package: 'Compiler-Core'!
  292. !Evaluator commentStamp!
  293. I evaluate code against a receiver, dispatching #evaluate:on: to the receiver.!
  294. !Evaluator methodsFor: 'evaluating'!
  295. evaluate: aString for: anObject
  296. ^ anObject evaluate: aString on: self
  297. !
  298. evaluate: aString receiver: anObject
  299. | compiler |
  300. compiler := Compiler new.
  301. [ compiler parseExpression: aString ]
  302. on: Error
  303. do: [ :ex | ^ Terminal alert: ex messageText ].
  304. ^ compiler evaluateExpression: aString on: anObject
  305. ! !
  306. !Evaluator class methodsFor: 'instance creation'!
  307. evaluate: aString for: anObject
  308. ^ self new evaluate: aString for: anObject
  309. ! !
  310. Error subclass: #ParseError
  311. slots: {}
  312. package: 'Compiler-Core'!
  313. !ParseError commentStamp!
  314. Instance of ParseError are signaled on any parsing error.
  315. See `Compiler >> #parse:`!
  316. !String methodsFor: '*Compiler-Core'!
  317. asVariableName
  318. ^ (Smalltalk reservedWords includes: self)
  319. ifTrue: [ self, '_' ]
  320. ifFalse: [ self ]
  321. ! !