Repository navigation
Expand file tree
/
Copy pathLuaCompilationUnitSyntax.cs
More file actions
165 lines (136 loc) · 5.59 KB
/
Copy pathLuaCompilationUnitSyntax.cs
File metadata and controls
165 lines (136 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2017 YANG Huan ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace CSharpLua.LuaAst {
public sealed class GenericUsingDeclare : IComparable<GenericUsingDeclare> {
public LuaInvocationExpressionSyntax InvocationExpression;
public string NewName;
public List<string> ArgumentTypeNames;
public bool IsFromCode;
public int CompareTo(GenericUsingDeclare other) {
if (other.ArgumentTypeNames.Contains(NewName)) {
return -1;
}
if (ArgumentTypeNames.Contains(other.NewName)) {
return 1;
}
if (NewName.Length != other.NewName.Length) {
return NewName.Length.CompareTo(other.NewName.Length);
}
return NewName.CompareTo(other.NewName);
}
}
public sealed class UsingDeclare : IComparable<UsingDeclare> {
public string Prefix;
public string NewPrefix;
public bool IsFromCode;
public int CompareTo(UsingDeclare other) {
return Prefix.CompareTo(other.Prefix);
}
}
public sealed class LuaCompilationUnitSyntax : LuaSyntaxNode {
public string FilePath { get; }
public readonly LuaSyntaxList<LuaStatementSyntax> Statements = new LuaSyntaxList<LuaStatementSyntax>();
private readonly LuaStatementListSyntax importAreaStatements = new LuaStatementListSyntax();
private bool isImportLinq_;
private int typeDeclarationCount_;
internal readonly List<UsingDeclare> UsingDeclares = new List<UsingDeclare>();
internal readonly List<GenericUsingDeclare> GenericUsingDeclares = new List<GenericUsingDeclare>();
public LuaCompilationUnitSyntax(string filePath = "") {
FilePath = filePath;
var info = Assembly.GetExecutingAssembly().GetName();
LuaShortCommentStatement versonStatement = new LuaShortCommentStatement($" Generated by {info.Name} Compiler");
AddStatement(versonStatement);
var system = LuaIdentifierNameSyntax.System;
AddImport(system, system);
}
public void AddStatement(LuaStatementSyntax statement) {
Statements.Add(statement);
}
public bool IsEmpty {
get {
return typeDeclarationCount_ == 0;
}
}
public void ImportLinq() {
if (!isImportLinq_) {
AddImport(LuaIdentifierNameSyntax.Linq, LuaIdentifierNameSyntax.SystemLinqEnumerable);
isImportLinq_ = true;
}
}
private void AddImport(LuaIdentifierNameSyntax name, LuaExpressionSyntax value) {
importAreaStatements.Statements.Add(new LuaLocalVariableDeclaratorSyntax(name, value));
}
internal void AddTypeDeclarationCount() {
++typeDeclarationCount_;
}
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);
}
}
internal override void Render(LuaRenderer renderer) {
CheckUsingDeclares();
renderer.Render(this);
}
}
}