@@ -197,22 +197,22 @@ public LuaSyntaxGenerator(IEnumerable<(string Text, string Path)> codes, IEnumer
197197 DoPretreatment ( ) ;
198198 }
199199
200- private LuaCompilationUnitSyntax CreateCompilationUnit ( SyntaxTree syntaxTree ) {
200+ private LuaCompilationUnitSyntax CreateCompilationUnit ( SyntaxTree syntaxTree , bool isSingleFile ) {
201201 var semanticModel = GetSemanticModel ( syntaxTree ) ;
202202 var compilationUnitSyntax = ( CompilationUnitSyntax ) syntaxTree . GetRoot ( ) ;
203203 var transfor = new LuaSyntaxNodeTransform ( this , semanticModel ) ;
204- return compilationUnitSyntax . Accept < LuaCompilationUnitSyntax > ( transfor ) ;
204+ return transfor . VisitCompilationUnit ( compilationUnitSyntax , isSingleFile ) ;
205205 }
206206
207- private Task < LuaCompilationUnitSyntax > CreateCompilationUnitAsync ( SyntaxTree syntaxTree ) {
208- return Task . Factory . StartNew ( o => CreateCompilationUnit ( syntaxTree ) , null ) ;
207+ private Task < LuaCompilationUnitSyntax > CreateCompilationUnitAsync ( SyntaxTree syntaxTree , bool isSingleFile ) {
208+ return Task . Factory . StartNew ( o => CreateCompilationUnit ( syntaxTree , isSingleFile ) , null ) ;
209209 }
210210
211- private IEnumerable < LuaCompilationUnitSyntax > Create ( ) {
211+ private IEnumerable < LuaCompilationUnitSyntax > Create ( bool isSingleFile = false ) {
212212 List < LuaCompilationUnitSyntax > luaCompilationUnits ;
213213 if ( kIsConcurrent ) {
214214 try {
215- var tasks = compilation_ . SyntaxTrees . Select ( CreateCompilationUnitAsync ) ;
215+ var tasks = compilation_ . SyntaxTrees . Select ( i => CreateCompilationUnitAsync ( i , isSingleFile ) ) ;
216216 luaCompilationUnits = Task . WhenAll ( tasks ) . Result . ToList ( ) ;
217217 } catch ( AggregateException e ) {
218218 if ( e . InnerExceptions . Count > 0 ) {
@@ -222,7 +222,7 @@ private IEnumerable<LuaCompilationUnitSyntax> Create() {
222222 }
223223 }
224224 } else {
225- luaCompilationUnits = compilation_ . SyntaxTrees . Select ( CreateCompilationUnit ) . ToList ( ) ;
225+ luaCompilationUnits = compilation_ . SyntaxTrees . Select ( i => CreateCompilationUnit ( i , isSingleFile ) ) . ToList ( ) ;
226226 }
227227
228228 CheckExportEnums ( ) ;
@@ -254,21 +254,49 @@ public void Generate(string outFolder) {
254254 public void GenerateSingleFile ( string outFile , string outFolder , IEnumerable < string > luaSystemLibs ) {
255255 outFile = GetOutFileRelativePath ( outFile , outFolder , out _ ) ;
256256 using var streamWriter = new StreamWriter ( outFile , false , Encoding ) ;
257+ streamWriter . WriteLine ( "CSharpLuaSingleFile = true" ) ;
258+ bool isFirst = true ;
257259 foreach ( var luaSystemLib in luaSystemLibs ) {
258- WriteLuaSystemLib ( luaSystemLib , streamWriter ) ;
260+ WriteLuaSystemLib ( luaSystemLib , streamWriter , isFirst ) ;
261+ isFirst = false ;
259262 }
260- foreach ( var luaCompilationUnit in Create ( ) ) {
263+ streamWriter . WriteLine ( ) ;
264+ streamWriter . WriteLine ( LuaSyntaxNode . Tokens . ShortComment + LuaCompilationUnitSyntax . GeneratedMarkString ) ;
265+ foreach ( var luaCompilationUnit in Create ( true ) ) {
261266 WriteCompilationUnit ( luaCompilationUnit , streamWriter ) ;
262267 }
263- if ( mainEntryPoint_ is null ) {
264- throw new CompilationErrorException ( "Program has no main entry point." ) ;
268+ WriteSingleFileManifest ( streamWriter ) ;
269+ }
270+
271+ private static string GetSystemLibName ( string path ) {
272+ const string begin = "CoreSystem" ;
273+ int index = path . LastIndexOf ( begin ) ;
274+ return path . Substring ( index + begin . Length + 1 ) ;
275+ }
276+
277+ private static void RemoveLicenseComments ( ref string code ) {
278+ const string kBegin = "--[[" ;
279+ const string kEnd = "--]]" ;
280+ int i = code . IndexOf ( kBegin ) ;
281+ if ( i != - 1 ) {
282+ bool isSpace = code . Take ( i ) . All ( char . IsWhiteSpace ) ;
283+ if ( isSpace ) {
284+ int j = code . IndexOf ( kEnd , i + kBegin . Length ) ;
285+ Contract . Assert ( j != - 1 ) ;
286+ code = code . Substring ( j + kEnd . Length ) . Trim ( ) ;
287+ }
265288 }
266- WriteManifest ( streamWriter ) ;
267289 }
268290
269- private void WriteLuaSystemLib ( string filePath , TextWriter writer ) {
291+ private void WriteLuaSystemLib ( string filePath , TextWriter writer , bool isFirst ) {
292+ writer . WriteLine ( ) ;
293+ writer . WriteLine ( "-- CoreSystemLib: {0}" , GetSystemLibName ( filePath ) ) ;
270294 writer . WriteLine ( LuaSyntaxNode . Keyword . Do ) ;
271- writer . WriteLine ( File . ReadAllText ( filePath ) ) ;
295+ string code = File . ReadAllText ( filePath ) ;
296+ if ( ! isFirst ) {
297+ RemoveLicenseComments ( ref code ) ;
298+ }
299+ writer . WriteLine ( code ) ;
272300 writer . WriteLine ( LuaSyntaxNode . Keyword . End ) ;
273301 }
274302
@@ -279,25 +307,20 @@ private void WriteCompilationUnit(LuaCompilationUnitSyntax luaCompilationUnit, T
279307 writer . WriteLine ( LuaSyntaxNode . Keyword . End ) ;
280308 }
281309
282- private void WriteManifest ( TextWriter writer ) {
283- const string kManifestFuncName = "InitCSharp" ;
310+ private void WriteSingleFileManifest ( TextWriter writer ) {
284311 var types = GetExportTypes ( ) ;
285312 if ( types . Count > 0 ) {
286- var functionExpression = new LuaFunctionExpressionSyntax ( ) ;
287- var initCSharpFunctionDeclarationStatement = new LuaLocalVariablesSyntax ( ) { Initializer = new LuaEqualsValueClauseListSyntax ( functionExpression . ArrayOf ( ) ) } ;
288- initCSharpFunctionDeclarationStatement . Variables . Add ( new LuaSymbolNameSyntax ( new LuaIdentifierLiteralExpressionSyntax ( kManifestFuncName ) ) ) ;
289-
290313 LuaTableExpression typeTable = new LuaTableExpression ( ) ;
291314 typeTable . Add ( "types" , new LuaTableExpression ( types . Select ( i => new LuaStringLiteralExpressionSyntax ( GetTypeShortName ( i ) ) ) ) ) ;
292- var methodName = mainEntryPoint_ . Name ;
293- var methodTypeName = GetTypeName ( mainEntryPoint_ . ContainingType ) ;
294- var entryPointInvocation = new LuaInvocationExpressionSyntax ( methodTypeName . MemberAccess ( methodName ) ) ;
295- functionExpression . AddStatement ( LuaIdentifierNameSyntax . SystemInit . Invocation ( typeTable ) ) ;
296- functionExpression . AddStatement ( entryPointInvocation ) ;
297-
298- LuaCompilationUnitSyntax luaCompilationUnit = new LuaCompilationUnitSyntax ( ) ;
299- luaCompilationUnit . AddStatement ( new LuaLocalDeclarationStatementSyntax ( initCSharpFunctionDeclarationStatement ) ) ;
300-
315+ LuaCompilationUnitSyntax luaCompilationUnit = new LuaCompilationUnitSyntax ( hasGeneratedMark : false ) ;
316+ luaCompilationUnit . AddStatement ( LuaIdentifierNameSyntax . SystemInit . Invocation ( typeTable ) ) ;
317+ if ( mainEntryPoint_ != null ) {
318+ luaCompilationUnit . AddStatement ( LuaBlankLinesStatement . One ) ;
319+ var methodName = mainEntryPoint_ . Name ;
320+ var methodTypeName = GetTypeName ( mainEntryPoint_ . ContainingType ) ;
321+ var entryPointInvocation = new LuaInvocationExpressionSyntax ( methodTypeName . MemberAccess ( methodName ) ) ;
322+ luaCompilationUnit . AddStatement ( entryPointInvocation ) ;
323+ }
301324 Write ( luaCompilationUnit , writer ) ;
302325 writer . WriteLine ( ) ;
303326 }
0 commit comments