Compiler-Core.st 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  192. ^ result
  193. !
  194. parseExpression: aString
  195. ^ self parse: (self sourceForExpression: aString)
  196. !
  197. recompile: aClass
  198. aClass includingPossibleMetaDo: [ :eachSide |
  199. eachSide methodDictionary values
  200. do: [ :each | each origin = eachSide ifTrue: [
  201. self
  202. install: each source
  203. forClass: eachSide
  204. protocol: each protocol ] ]
  205. displayingProgress: 'Recompiling ', eachSide name ]
  206. !
  207. recompileAll
  208. Smalltalk classes
  209. do: [ :each | self recompile: each ]
  210. displayingProgress: 'Compiling all classes...'
  211. !
  212. sourceForExpression: aString
  213. ^ 'xxxDoIt ^ [ ', aString, ' ] value'
  214. !
  215. start: aString forClass: aClass protocol: anotherString
  216. | package |
  217. package := aClass packageOfProtocol: anotherString.
  218. self
  219. currentPackage: package;
  220. codeGenerator: (self codeGeneratorClass new
  221. source: aString;
  222. currentClass: aClass;
  223. currentPackage: package;
  224. yourself)
  225. !
  226. transformerAt: aString put: anObject
  227. self codeGenerator transformersDictionary at: aString put: anObject
  228. ! !
  229. !Compiler methodsFor: 'error handling'!
  230. error: aString
  231. CompilerError signal: aString
  232. !
  233. parseError: anException parsing: aString
  234. (anException basicAt: 'location')
  235. ifNil: [ ^ anException pass ]
  236. ifNotNil: [ :loc |
  237. ^ ParseError new
  238. messageText:
  239. 'Parse error on line ', loc start line asString,
  240. ' column ' , loc start column asString,
  241. ' : Unexpected character ', (anException basicAt: 'found');
  242. yourself ]
  243. ! !
  244. !Compiler methodsFor: 'private'!
  245. basicParse: aString
  246. ^ smalltalkParser parse: aString
  247. !
  248. wrappedSourceOf: anIRMethod
  249. ^ anIRMethod attachments
  250. ifEmpty: [
  251. '(function ($methodClass){ return ',
  252. anIRMethod compiledSource,
  253. '; })' ]
  254. ifNotEmpty: [ :attachments |
  255. '(function ($methodClass){ return Object.defineProperty(',
  256. anIRMethod compiledSource,
  257. ',"a$atx",{enumerable:false,configurable:true,writable:true,value:',
  258. attachments asJavaScriptSource,
  259. '}); })' ]
  260. ! !
  261. !Compiler class methodsFor: 'compiling'!
  262. recompile: aClass
  263. self new recompile: aClass
  264. !
  265. recompileAll
  266. Smalltalk classes do: [ :each |
  267. self recompile: each ]
  268. ! !
  269. !Compiler class methodsFor: 'evaluating'!
  270. eval: aString
  271. ^ self new eval: aString
  272. ! !
  273. !Compiler class methodsFor: 'initialization'!
  274. initialize
  275. "TODO remove, backward compat"
  276. Smalltalk globals at: #SmalltalkParser put: smalltalkParser
  277. ! !
  278. !Compiler class methodsFor: 'parsing'!
  279. parse: aString
  280. ^ self new parse: aString
  281. !
  282. pseudoVariableNames
  283. ^ PseudoVar dictionary keys asArray
  284. ! !
  285. Object subclass: #DoIt
  286. slots: {}
  287. package: 'Compiler-Core'!
  288. !DoIt commentStamp!
  289. `DoIt` is the class used to compile and evaluate expressions. See `Compiler >> evaluateExpression:`.!
  290. Object subclass: #Evaluator
  291. slots: {}
  292. package: 'Compiler-Core'!
  293. !Evaluator commentStamp!
  294. I evaluate code against a receiver, dispatching #evaluate:on: to the receiver.!
  295. !Evaluator methodsFor: 'evaluating'!
  296. evaluate: aString for: anObject
  297. ^ anObject evaluate: aString on: self
  298. !
  299. evaluate: aString receiver: anObject
  300. | compiler |
  301. compiler := Compiler new.
  302. [ compiler parseExpression: aString ]
  303. on: Error
  304. do: [ :ex | ^ Terminal alert: ex messageText ].
  305. ^ compiler evaluateExpression: aString on: anObject
  306. ! !
  307. !Evaluator class methodsFor: 'instance creation'!
  308. evaluate: aString for: anObject
  309. ^ self new evaluate: aString for: anObject
  310. ! !
  311. Error subclass: #ParseError
  312. slots: {}
  313. package: 'Compiler-Core'!
  314. !ParseError commentStamp!
  315. Instance of ParseError are signaled on any parsing error.
  316. See `Compiler >> #parse:`!
  317. Trait named: #TPragmator
  318. package: 'Compiler-Core'!
  319. !TPragmator methodsFor: 'pragma processing'!
  320. canProcessPragma: aMessage
  321. ^ self class includesSelector: aMessage selector
  322. !
  323. processPragma: aMessage
  324. (self canProcessPragma: aMessage) ifTrue: [
  325. ^ aMessage sendTo: self ]
  326. !
  327. processPragmas: aCollection
  328. aCollection do: [ :each | self processPragma: each ]
  329. ! !
  330. !Package methodsFor: '*Compiler-Core'!
  331. eval: aString
  332. ^ self context
  333. ifEmpty: [ Compiler eval: aString ]
  334. ifNotEmpty: [ :context |
  335. | wrapperSource |
  336. wrapperSource :=
  337. '(function(', (',' join: context keys), '){return(', aString, ');})'.
  338. (Compiler eval: wrapperSource)
  339. valueWithPossibleArguments: context values ]
  340. ! !
  341. !String methodsFor: '*Compiler-Core'!
  342. asVariableName
  343. ^ (Smalltalk reservedWords includes: self)
  344. ifTrue: [ self, '_' ]
  345. ifFalse: [ self ]
  346. ! !