private void CheckStaticCtorFunction(LuaBlockSyntax body) { List <LuaStatementSyntax> statements = new List <LuaStatementSyntax>(); statements.AddRange(staticInitStatements_); statements.AddRange(staticcCtorStatements_); if (statements.Count > 0) { LuaFunctionExpressionSyntax staticCtor = new LuaFunctionExpressionSyntax(); staticCtor.AddParameter(LuaIdentifierNameSyntax.This); staticCtor.Body.Statements.AddRange(statements); AddStaticAssignmentNames(staticCtor.Body); AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor); } }
internal void AddBaseTypes(IEnumerable <LuaExpressionSyntax> baseTypes, LuaSpeaicalGenericType genericArgument = null) { bool hasLazyGenericArgument = false; if (genericArgument != null) { if (genericArgument.IsLazy) { hasLazyGenericArgument = true; } else { AddResultTable(genericArgument.Name, genericArgument.Value); } } var global = LuaIdentifierNameSyntax.Global; LuaTableInitializerExpression table = new LuaTableInitializerExpression(); foreach (var baseType in baseTypes) { LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(global, baseType); table.Items.Add(new LuaSingleTableItemSyntax(memberAccess)); } LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(global); if (hasLazyGenericArgument) { functionExpression.AddParameter(LuaIdentifierNameSyntax.This); var assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, genericArgument.Name), genericArgument.Value); functionExpression.AddStatement(assignment); } functionExpression.AddStatement(new LuaReturnStatementSyntax(table)); AddResultTable(LuaIdentifierNameSyntax.Inherits, functionExpression); }
internal void AddBaseTypes(IEnumerable <LuaExpressionSyntax> baseTypes) { var global = LuaIdentifierNameSyntax.Global; LuaTableInitializerExpression table = new LuaTableInitializerExpression(); foreach (var baseType in baseTypes) { LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(global, baseType); table.Items.Add(new LuaSingleTableItemSyntax(memberAccess)); } LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(global); functionExpression.AddStatement(new LuaReturnStatementSyntax(table)); AddResultTable(LuaIdentifierNameSyntax.Inherits, functionExpression); }
private void CheckMetadata(LuaRenderer renderer) { if (metadata_ != null) { SortMetaData(metaFields_); SortMetaData(metaProperties_); SortMetaData(metaMethods_); if (renderer.Setting.IsClassic) { CheckMetadataMoreThanUpvalues(); } var functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(LuaIdentifierNameSyntax.Global); functionExpression.AddStatement(new LuaReturnStatementSyntax(metadata_)); AddResultTable(LuaIdentifierNameSyntax.Metadata, functionExpression); } }
private void CheckUsingDeclares() { var imports = usingDeclares_.Where(i => !i.IsFromCode).ToList(); if (imports.Count > 0) { imports.Sort((x, y) => x.Prefix.CompareTo(y.Prefix)); foreach (var import in imports) { AddImport(new LuaIdentifierNameSyntax(import.NewPrefix), new LuaIdentifierNameSyntax(import.Prefix)); } } var usingDeclares = usingDeclares_.Where(i => i.IsFromCode).ToList(); if (usingDeclares.Count > 0) { usingDeclares.Sort((x, y) => x.Prefix.CompareTo(y.Prefix)); foreach (var usingDeclare in usingDeclares) { AddImport(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), null); } var global = LuaIdentifierNameSyntax.Global; LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(global); foreach (var usingDeclare in usingDeclares) { if (usingDeclare.Prefix != usingDeclare.NewPrefix) { LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), new LuaIdentifierNameSyntax(usingDeclare.Prefix)); functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment)); } else { LuaMemberAccessExpressionSyntax right = new LuaMemberAccessExpressionSyntax(global, new LuaIdentifierNameSyntax(usingDeclare.Prefix)); LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), right); functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment)); } } LuaInvocationExpressionSyntax invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.UsingDeclare, functionExpression); HeadAreaStatements.Statements.Add(new LuaExpressionStatementSyntax(invocationExpression)); } }
private void CheckStaticCtorFunction(LuaBlockSyntax body) { var statements = new List <LuaStatementSyntax>(); statements.AddRange(staticInitStatements_); if (staticCtor_ != null) { statements.AddRange(staticCtor_.Function.Body.Statements); } if (statements.Count > 0 || IsForceStaticCtor) { var staticCtor = new LuaFunctionExpressionSyntax(); staticCtor.AddParameter(LuaIdentifierNameSyntax.This); staticCtor.Body.Statements.AddRange(statements); AddStaticAssignmentNames(staticCtor.Body); if (staticCtor_ != null && staticCtor_.Document != null) { body.AddStatement(staticCtor_.Document); } AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor); } }
internal override void Render(LuaRenderer renderer) { if (IsPartialMark) { return; } if (IsIgnoreExport) { return; } if (IsClassUsed) { local_.Variables.Add(LuaIdentifierNameSyntax.Class); } document_.Render(renderer); if (typeParameters_.Count > 0) { AddStatements(nestedTypeDeclarations_); nestedTypeDeclarations_.Clear(); LuaFunctionExpressionSyntax wrapFunction = new LuaFunctionExpressionSyntax(); foreach (var type in typeParameters_) { wrapFunction.AddParameter(type); } AddAllStatementsTo(wrapFunction.Body, renderer); Body.Statements.Add(new LuaReturnStatementSyntax(wrapFunction)); } else { AddAllStatementsTo(Body, renderer); } base.Render(renderer); }
private (LuaPropertyOrEventIdentifierNameSyntax, LuaPropertyOrEventIdentifierNameSyntax) AddPropertyOrEvent(bool isProperty, LuaIdentifierNameSyntax name, LuaIdentifierNameSyntax innerName, LuaExpressionSyntax value, bool isImmutable, bool isStatic, bool isPrivate, LuaExpressionSyntax typeExpression, List <LuaStatementSyntax> statements, bool isGetOnly = false) { LuaPropertyOrEventIdentifierNameSyntax get, set; if (isProperty) { get = new LuaPropertyOrEventIdentifierNameSyntax(true, true, name); set = isGetOnly ? null : new LuaPropertyOrEventIdentifierNameSyntax(true, false, name); } else { get = new LuaPropertyOrEventIdentifierNameSyntax(false, true, name); set = new LuaPropertyOrEventIdentifierNameSyntax(false, false, name); } local_.Variables.Add(get); if (set != null) { local_.Variables.Add(set); } if (!isStatic) { var initMethodIdentifier = isProperty ? LuaIdentifierNameSyntax.Property : LuaIdentifierNameSyntax.Event; var assignment = new LuaMultipleAssignmentExpressionSyntax(); assignment.Lefts.Add(get); if (set != null) { assignment.Lefts.Add(set); } var invocation = new LuaInvocationExpressionSyntax(initMethodIdentifier, new LuaStringLiteralExpressionSyntax(innerName)); if (set == null) { invocation.AddArgument(LuaIdentifierNameSyntax.True); } assignment.Rights.Add(invocation); methodList_.Statements.Add(assignment); } else { var memberAccess = typeExpression.MemberAccess(innerName); var getFunction = new LuaFunctionExpressionSyntax(); var setFunction = new LuaFunctionExpressionSyntax(); if (isProperty) { getFunction.AddStatement(new LuaReturnStatementSyntax(memberAccess)); setFunction.AddParameter(LuaIdentifierNameSyntax.Value); setFunction.AddStatement(memberAccess.Assignment(LuaIdentifierNameSyntax.Value)); } else { getFunction.AddParameter(LuaIdentifierNameSyntax.Value); var getAssignment = memberAccess.Assignment(memberAccess.Binary(Tokens.Plus, LuaIdentifierNameSyntax.Value)); getFunction.AddStatement(getAssignment); setFunction.AddParameter(LuaIdentifierNameSyntax.Value); var setAssignment = memberAccess.Assignment(memberAccess.Binary(Tokens.Sub, LuaIdentifierNameSyntax.Value)); setFunction.AddStatement(setAssignment); } methodList_.Statements.Add(get.Assignment(getFunction)); if (set != null) { methodList_.Statements.Add(set.Assignment(setFunction)); } } if (value != null) { if (isStatic) { if (isImmutable) { AddResultTable(innerName, value); } else { if (statements != null) { staticInitStatements_.AddRange(statements); } staticInitStatements_.Add(LuaIdentifierNameSyntax.This.MemberAccess(innerName).Assignment(value)); } } else { if (isImmutable) { AddResultTable(innerName, value); } else { if (statements != null) { initStatements_.AddRange(statements); } AddInitFiled(innerName, value); } } } if (!isPrivate) { AddResultTable(get); if (set != null) { AddResultTable(set); } } return(get, set); }
private void CheckUsingDeclares() { var imports = UsingDeclares.Where(i => !i.IsFromCode).ToList(); if (imports.Count > 0) { imports.Sort(); foreach (var import in imports) { AddImport(import.NewPrefix, import.Prefix); } } var genericImports = GenericUsingDeclares.Where(i => !i.IsFromCode).ToList(); if (genericImports.Count > 0) { genericImports.Sort(); foreach (var import in genericImports) { AddImport(import.NewName, import.InvocationExpression); } } var usingDeclares = UsingDeclares.Where(i => i.IsFromCode).ToList(); var genericDeclares = GenericUsingDeclares.Where(i => i.IsFromCode).ToList(); if (usingDeclares.Count > 0 || genericDeclares.Count > 0) { usingDeclares.Sort(); genericDeclares.Sort(); foreach (var usingDeclare in usingDeclares) { AddImport(usingDeclare.NewPrefix, null); } foreach (var usingDeclare in genericDeclares) { AddImport(usingDeclare.NewName, null); } var global = LuaIdentifierNameSyntax.Global; var functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(global); foreach (var usingDeclare in usingDeclares) { LuaIdentifierNameSyntax newPrefixIdentifier = usingDeclare.NewPrefix; if (usingDeclare.Prefix != usingDeclare.NewPrefix) { functionExpression.Body.AddStatement(newPrefixIdentifier.Assignment(usingDeclare.Prefix)); } else { functionExpression.Body.AddStatement(newPrefixIdentifier.Assignment(global.MemberAccess(usingDeclare.Prefix))); } } foreach (var usingDeclare in genericDeclares) { functionExpression.Body.AddStatement(((LuaIdentifierNameSyntax)usingDeclare.NewName).Assignment(usingDeclare.InvocationExpression)); } var invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Import, functionExpression); importAreaStatements.Statements.Add(invocationExpression); } int index = Statements.FindIndex(i => i is LuaNamespaceDeclarationSyntax); if (index != -1) { Statements.Insert(index, importAreaStatements); } }
private void CheckUsingDeclares() { var imports = usingDeclares_.Where(i => !i.IsFromCode).ToList(); if (imports.Count > 0) { imports.Sort(); foreach (var import in imports) { AddImport(new LuaIdentifierNameSyntax(import.NewPrefix), new LuaIdentifierNameSyntax(import.Prefix)); } } var genericImports = genericUsingDeclares_.Where(i => !i.IsFromCode).ToList(); if (genericImports.Count > 0) { genericImports.Sort(); foreach (var import in genericImports) { AddImport(new LuaIdentifierNameSyntax(import.NewName), import.InvocationExpression); } } var usingDeclares = usingDeclares_.Where(i => i.IsFromCode).ToList(); var genericDeclares = genericUsingDeclares_.Where(i => i.IsFromCode).ToList(); if (usingDeclares.Count > 0 || genericDeclares.Count > 0) { usingDeclares.Sort(); genericDeclares.Sort(); foreach (var usingDeclare in usingDeclares) { AddImport(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), null); } foreach (var usingDeclare in genericDeclares) { AddImport(new LuaIdentifierNameSyntax(usingDeclare.NewName), null); } var global = LuaIdentifierNameSyntax.Global; var functionExpression = new LuaFunctionExpressionSyntax(); functionExpression.AddParameter(global); foreach (var usingDeclare in usingDeclares) { if (usingDeclare.Prefix != usingDeclare.NewPrefix) { var assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), new LuaIdentifierNameSyntax(usingDeclare.Prefix)); functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment)); } else { var right = new LuaMemberAccessExpressionSyntax(global, new LuaIdentifierNameSyntax(usingDeclare.Prefix)); var assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), right); functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment)); } } foreach (var usingDeclare in genericDeclares) { var assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewName), usingDeclare.InvocationExpression); functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment)); } var invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.UsingDeclare, functionExpression); importAreaStatements.Statements.Add(new LuaExpressionStatementSyntax(invocationExpression)); } int index = Statements.FindIndex(i => i is LuaNamespaceDeclarationSyntax); if (index != -1) { Statements.Insert(index, importAreaStatements); } }
private void AddPropertyOrEvent(bool isProperty, LuaIdentifierNameSyntax name, LuaIdentifierNameSyntax innerName, LuaExpressionSyntax value, bool isImmutable, bool isStatic, bool isPrivate, LuaExpressionSyntax typeExpression, List <LuaStatementSyntax> statements) { LuaPropertyOrEventIdentifierNameSyntax get, set; if (isProperty) { get = new LuaPropertyOrEventIdentifierNameSyntax(true, true, name); set = new LuaPropertyOrEventIdentifierNameSyntax(true, false, name); } else { get = new LuaPropertyOrEventIdentifierNameSyntax(false, true, name); set = new LuaPropertyOrEventIdentifierNameSyntax(false, false, name); } local_.Variables.Add(get); local_.Variables.Add(set); if (!isStatic) { LuaIdentifierNameSyntax initMethodIdentifier = isProperty ? LuaIdentifierNameSyntax.Property : LuaIdentifierNameSyntax.Event; LuaMultipleAssignmentExpressionSyntax assignment = new LuaMultipleAssignmentExpressionSyntax(); assignment.Lefts.Add(get); assignment.Lefts.Add(set); LuaInvocationExpressionSyntax invocation = new LuaInvocationExpressionSyntax(initMethodIdentifier); invocation.AddArgument(new LuaStringLiteralExpressionSyntax(innerName)); assignment.Rights.Add(invocation); methodList_.Statements.Add(assignment.ToStatement()); } else { var memberAccess = new LuaMemberAccessExpressionSyntax(typeExpression, innerName); var getFunction = new LuaFunctionExpressionSyntax(); var setFunction = new LuaFunctionExpressionSyntax(); if (isProperty) { getFunction.AddStatement(new LuaReturnStatementSyntax(memberAccess)); setFunction.AddParameter(LuaIdentifierNameSyntax.Value); var assignment = new LuaAssignmentExpressionSyntax(memberAccess, LuaIdentifierNameSyntax.Value); setFunction.AddStatement(assignment.ToStatement()); } else { getFunction.AddParameter(LuaIdentifierNameSyntax.Value); var getAssignment = new LuaAssignmentExpressionSyntax(memberAccess, new LuaBinaryExpressionSyntax(memberAccess, Tokens.Plus, LuaIdentifierNameSyntax.Value)); getFunction.AddStatement(getAssignment.ToStatement()); setFunction.AddParameter(LuaIdentifierNameSyntax.Value); var setAssignment = new LuaAssignmentExpressionSyntax(memberAccess, new LuaBinaryExpressionSyntax(memberAccess, Tokens.Sub, LuaIdentifierNameSyntax.Value)); setFunction.AddStatement(setAssignment.ToStatement()); } methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(get, getFunction).ToStatement()); methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(set, setFunction).ToStatement()); } if (value != null) { if (isStatic) { if (isImmutable) { AddResultTable(innerName, value); } else { if (statements != null) { staticInitStatements_.AddRange(statements); } LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, innerName), value); staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment)); } } else { if (isImmutable) { AddResultTable(innerName, value); } else { if (statements != null) { initStatements_.AddRange(statements); } AddInitFiled(innerName, value); } } } if (!isPrivate) { AddResultTable(get); AddResultTable(set); } }