Compiler-Core.st 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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: '7000-irLatePragmas' put: self lateIRPragmator;
  88. at: '8000-irToJs' put: self irTranslator;
  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. | sanitizedSource compilationResult result pragmas closureFactory |
  135. sanitizedSource := aString crlfSanitized.
  136. compilationResult := self
  137. start: sanitizedSource forClass: aClass protocol: anotherString;
  138. compileNode: (self parse: sanitizedSource).
  139. closureFactory := self
  140. eval: (self wrappedSourceOf: compilationResult)
  141. forPackage: self currentPackage.
  142. result := Smalltalk core method: #{
  143. #selector -> compilationResult selector.
  144. #protocol -> anotherString.
  145. #source -> sanitizedSource.
  146. #messageSends -> compilationResult messageSends asArray.
  147. #args -> compilationResult arguments.
  148. #referencedClasses -> compilationResult classReferences asArray.
  149. } withFactory: closureFactory.
  150. result pragmas: compilationResult pragmas.
  151. ^ result
  152. !
  153. compileNode: aNode
  154. | result |
  155. result := self codeGenerator compileNode: aNode.
  156. self cleanCodeGenerator.
  157. ^ result
  158. !
  159. eval: aString
  160. <inlineJS: 'return eval(aString)'>
  161. !
  162. eval: aString forPackage: aPackage
  163. ^ aPackage
  164. ifNil: [ self eval: aString ]
  165. ifNotNil: [ aPackage eval: aString ]
  166. !
  167. evaluateExpression: aString
  168. "Unlike #eval: evaluate a Smalltalk expression and answer the returned object"
  169. ^ self evaluateExpression: aString on: DoIt new
  170. !
  171. evaluateExpression: aString on: anObject
  172. "Unlike #eval: evaluate a Smalltalk expression with anObject as the receiver and answer the returned object"
  173. | result method |
  174. method := self
  175. install: (self sourceForExpression: aString)
  176. forClass: anObject class
  177. protocol: '**xxxDoIt'.
  178. result := anObject xxxDoIt.
  179. anObject class removeCompiledMethod: method.
  180. ^ result
  181. !
  182. install: aString forClass: aBehavior protocol: anotherString
  183. | compiledMethod |
  184. compiledMethod := self compile: aString forClass: aBehavior protocol: anotherString.
  185. aBehavior addCompiledMethod: compiledMethod.
  186. ^ compiledMethod
  187. !
  188. parse: aString
  189. | result |
  190. [ result := self basicParse: aString ]
  191. tryIfTrue: [ :err | (err basicAt: 'location') notNil ]
  192. catch: [ :ex | (self parseError: ex parsing: aString) signal ].
  193. ^ result
  194. !
  195. parseExpression: aString
  196. ^ self parse: (self sourceForExpression: aString)
  197. !
  198. recompile: aClass
  199. aClass includingPossibleMetaDo: [ :eachSide |
  200. eachSide methodDictionary values
  201. do: [ :each | each origin = eachSide ifTrue: [
  202. self
  203. install: each source
  204. forClass: eachSide
  205. protocol: each protocol ] ]
  206. displayingProgress: 'Recompiling ', eachSide name ]
  207. !
  208. recompileAll
  209. Smalltalk classes
  210. do: [ :each | self recompile: each ]
  211. displayingProgress: 'Compiling all classes...'
  212. !
  213. sourceForExpression: aString
  214. ^ 'xxxDoIt ^ [ ', aString, ' ] value'
  215. !
  216. start: aString forClass: aClass protocol: anotherString
  217. | package |
  218. package := aClass packageOfProtocol: anotherString.
  219. self
  220. currentPackage: package;
  221. codeGenerator: (self codeGeneratorClass new
  222. source: aString;
  223. currentClass: aClass;
  224. currentPackage: package;
  225. yourself)
  226. !
  227. transformerAt: aString put: anObject
  228. self codeGenerator transformersDictionary at: aString put: anObject
  229. ! !
  230. !Compiler methodsFor: 'error handling'!
  231. error: aString
  232. CompilerError signal: aString
  233. !
  234. parseError: anException parsing: aString
  235. | loc |
  236. loc := anException basicAt: 'location'.
  237. ^ ParseError messageText:
  238. 'Parse error on line ', loc start line asString,
  239. ' column ' , loc start column asString,
  240. ' : Unexpected character ', (anException basicAt: 'found')
  241. ! !
  242. !Compiler methodsFor: 'private'!
  243. basicParse: aString
  244. ^ smalltalkParser parse: aString
  245. !
  246. wrappedSourceOf: anIRMethod
  247. ^ anIRMethod attachments
  248. ifEmpty: [
  249. '(function ($methodClass){ return ',
  250. anIRMethod compiledSource,
  251. '; })' ]
  252. ifNotEmpty: [ :attachments |
  253. '(function ($methodClass){ return Object.defineProperty(',
  254. anIRMethod compiledSource,
  255. ',"a$atx",{enumerable:false,configurable:true,writable:true,value:',
  256. attachments asJavaScriptSource,
  257. '}); })' ]
  258. ! !
  259. !Compiler class methodsFor: 'compiling'!
  260. recompile: aClass
  261. self new recompile: aClass
  262. !
  263. recompileAll
  264. Smalltalk classes do: [ :each |
  265. self recompile: each ]
  266. ! !
  267. !Compiler class methodsFor: 'evaluating'!
  268. eval: aString
  269. ^ self new eval: aString
  270. ! !
  271. !Compiler class methodsFor: 'parsing'!
  272. parse: aString
  273. ^ self new parse: aString
  274. !
  275. pseudoVariableNames
  276. ^ PseudoVar dictionary keys asArray
  277. ! !
  278. Object subclass: #DoIt
  279. slots: {}
  280. package: 'Compiler-Core'!
  281. !DoIt commentStamp!
  282. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  283. Object subclass: #Evaluator
  284. slots: {}
  285. package: 'Compiler-Core'!
  286. !Evaluator commentStamp!
  287. I evaluate code against a receiver, dispatching #evaluate:on: to the receiver.!
  288. !Evaluator methodsFor: 'evaluating'!
  289. evaluate: aString for: anObject
  290. ^ anObject evaluate: aString on: self
  291. !
  292. evaluate: aString receiver: anObject
  293. | compiler |
  294. compiler := Compiler new.
  295. [ compiler parseExpression: aString ]
  296. on: Error
  297. do: [ :ex | ^ Terminal alert: ex messageText ].
  298. ^ compiler evaluateExpression: aString on: anObject
  299. ! !
  300. !Evaluator class methodsFor: 'instance creation'!
  301. evaluate: aString for: anObject
  302. ^ self new evaluate: aString for: anObject
  303. ! !
  304. Error subclass: #ParseError
  305. slots: {}
  306. package: 'Compiler-Core'!
  307. !ParseError commentStamp!
  308. Instance of ParseError are signaled on any parsing error.
  309. See `Compiler >> #parse:`!
  310. Trait named: #TPragmator
  311. package: 'Compiler-Core'!
  312. !TPragmator methodsFor: 'pragma processing'!
  313. canProcessPragma: aMessage
  314. ^ self class includesSelector: aMessage selector
  315. !
  316. processPragma: aMessage
  317. (self canProcessPragma: aMessage) ifTrue: [
  318. ^ aMessage sendTo: self ]
  319. !
  320. processPragmas: aCollection
  321. aCollection do: [ :each | self processPragma: each ]
  322. ! !
  323. !Package methodsFor: '*Compiler-Core'!
  324. eval: aString
  325. ^ self context
  326. ifEmpty: [ Compiler eval: aString ]
  327. ifNotEmpty: [ :context |
  328. | wrapperSource |
  329. wrapperSource :=
  330. '(function(', (',' join: context keys), '){return(', aString, ');})'.
  331. (Compiler eval: wrapperSource)
  332. valueWithPossibleArguments: context values ]
  333. ! !
  334. !String methodsFor: '*Compiler-Core'!
  335. asVariableName
  336. ^ (Smalltalk reservedWords includes: self)
  337. ifTrue: [ self, '_' ]
  338. ifFalse: [ self ]
  339. ! !