@@ -197,22 +197,22 @@ public LuaSyntaxGenerator(IEnumerable<(string Text, string Path)> codes, IEnumer
197
197
DoPretreatment ( ) ;
198
198
}
199
199
200
- private LuaCompilationUnitSyntax CreateCompilationUnit ( SyntaxTree syntaxTree ) {
200
+ private LuaCompilationUnitSyntax CreateCompilationUnit ( SyntaxTree syntaxTree , bool isSingleFile ) {
201
201
var semanticModel = GetSemanticModel ( syntaxTree ) ;
202
202
var compilationUnitSyntax = ( CompilationUnitSyntax ) syntaxTree . GetRoot ( ) ;
203
203
var transfor = new LuaSyntaxNodeTransform ( this , semanticModel ) ;
204
- return compilationUnitSyntax . Accept < LuaCompilationUnitSyntax > ( transfor ) ;
204
+ return transfor . VisitCompilationUnit ( compilationUnitSyntax , isSingleFile ) ;
205
205
}
206
206
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 ) ;
209
209
}
210
210
211
- private IEnumerable < LuaCompilationUnitSyntax > Create ( ) {
211
+ private IEnumerable < LuaCompilationUnitSyntax > Create ( bool isSingleFile = false ) {
212
212
List < LuaCompilationUnitSyntax > luaCompilationUnits ;
213
213
if ( kIsConcurrent ) {
214
214
try {
215
- var tasks = compilation_ . SyntaxTrees . Select ( CreateCompilationUnitAsync ) ;
215
+ var tasks = compilation_ . SyntaxTrees . Select ( i => CreateCompilationUnitAsync ( i , isSingleFile ) ) ;
216
216
luaCompilationUnits = Task . WhenAll ( tasks ) . Result . ToList ( ) ;
217
217
} catch ( AggregateException e ) {
218
218
if ( e . InnerExceptions . Count > 0 ) {
@@ -222,7 +222,7 @@ private IEnumerable<LuaCompilationUnitSyntax> Create() {
222
222
}
223
223
}
224
224
} else {
225
- luaCompilationUnits = compilation_ . SyntaxTrees . Select ( CreateCompilationUnit ) . ToList ( ) ;
225
+ luaCompilationUnits = compilation_ . SyntaxTrees . Select ( i => CreateCompilationUnit ( i , isSingleFile ) ) . ToList ( ) ;
226
226
}
227
227
228
228
CheckExportEnums ( ) ;
@@ -254,21 +254,49 @@ public void Generate(string outFolder) {
254
254
public void GenerateSingleFile ( string outFile , string outFolder , IEnumerable < string > luaSystemLibs ) {
255
255
outFile = GetOutFileRelativePath ( outFile , outFolder , out _ ) ;
256
256
using var streamWriter = new StreamWriter ( outFile , false , Encoding ) ;
257
+ streamWriter . WriteLine ( "CSharpLuaSingleFile = true" ) ;
258
+ bool isFirst = true ;
257
259
foreach ( var luaSystemLib in luaSystemLibs ) {
258
- WriteLuaSystemLib ( luaSystemLib , streamWriter ) ;
260
+ WriteLuaSystemLib ( luaSystemLib , streamWriter , isFirst ) ;
261
+ isFirst = false ;
259
262
}
260
- foreach ( var luaCompilationUnit in Create ( ) ) {
263
+ streamWriter . WriteLine ( ) ;
264
+ streamWriter . WriteLine ( LuaSyntaxNode . Tokens . ShortComment + LuaCompilationUnitSyntax . GeneratedMarkString ) ;
265
+ foreach ( var luaCompilationUnit in Create ( true ) ) {
261
266
WriteCompilationUnit ( luaCompilationUnit , streamWriter ) ;
262
267
}
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
+ }
265
288
}
266
- WriteManifest ( streamWriter ) ;
267
289
}
268
290
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 ) ) ;
270
294
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 ) ;
272
300
writer . WriteLine ( LuaSyntaxNode . Keyword . End ) ;
273
301
}
274
302
@@ -279,25 +307,20 @@ private void WriteCompilationUnit(LuaCompilationUnitSyntax luaCompilationUnit, T
279
307
writer . WriteLine ( LuaSyntaxNode . Keyword . End ) ;
280
308
}
281
309
282
- private void WriteManifest ( TextWriter writer ) {
283
- const string kManifestFuncName = "InitCSharp" ;
310
+ private void WriteSingleFileManifest ( TextWriter writer ) {
284
311
var types = GetExportTypes ( ) ;
285
312
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
-
290
313
LuaTableExpression typeTable = new LuaTableExpression ( ) ;
291
314
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
+ }
301
324
Write ( luaCompilationUnit , writer ) ;
302
325
writer . WriteLine ( ) ;
303
326
}
0 commit comments