Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yanghuan/CSharp.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Oct 7, 2023
2 parents 467585d + ca2cb31 commit 9f7195b
Show file tree
Hide file tree
Showing 12 changed files with 752 additions and 19 deletions.
116 changes: 110 additions & 6 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,93 @@ local trunc = System.trunc

local math = math
local floor = math.floor
local ceil = math.ceil
local min = math.min
local max = math.max
local abs = math.abs
local log = math.log
local sqrt = math.sqrt
local ln2 = log(2)

local function acosh(a)
return log(a + sqrt(a ^ 2 - 1))
end

local function asinh(a)
return log(a + sqrt(a ^ 2 + 1))
end

local function atanh(a)
return 0.5 * log((1 + a) / (1 - a))
end

local function cbrt(a)
if a >= 0 then
return a ^ (1 / 3)
else
return -abs(a) ^ (1 / 3)
end
end

local function copySign(a, b)
if b >= 0 then
return a >= 0 and a or -a
else
return a >= 0 and -a or a
end
end

local function fusedMultiplyAdd(a, b, c)
return a * b + c
end

local function ilogB(a)
return a == 0 and -2147483648 or floor(log(abs(a)) / ln2)
end

local function log2(a)
return log(a) / ln2
end

local function maxMagnitude(a, b)
local x = abs(a)
local y = abs(b)
if x > y then
return a
elseif x < y then
return b
else
return a > b and a or b
end
end

local function minMagnitude(a, b)
local x = abs(a)
local y = abs(b)
if x < y then
return a
elseif x > y then
return b
else
return a < b and a or b
end
end

local function reciprocalEstimate(a)
return 1 / a
end

local function reciprocalSqrtEstimate(a)
return sqrt(1 / a)
end

local function scaleB(a, b)
return a * 2 ^ b
end

local function sinCos(a)
return System.ValueTuple(math.sin(a), math.cos(a))
end

local function bigMul(a, b)
return a * b
Expand All @@ -38,11 +121,17 @@ local function round(value, digits, mode)
local i = value * mult
if mode == 1 then
value = trunc(i + (value >= 0 and 0.5 or -0.5))
elseif mode == 2 then
value = i >= 0 and floor(i) or ceil(i)
elseif mode == 3 then
value = floor(i)
elseif mode == 4 then
value = ceil(i)
else
value = trunc(i)
if value ~= i then
local dif = i - value
if value >= 0 then
if i >= 0 then
if dif > 0.5 or (dif == 0.5 and value % 2 ~= 0) then
value = value + 1
end
Expand Down Expand Up @@ -115,30 +204,45 @@ local tanh = math.tanh or function (x) return sinh(x) / cosh(x) end
local Math = math
Math.Abs = abs
Math.Acos = math.acos
Math.Acosh = acosh
Math.Asin = math.asin
Math.Asinh = asinh
Math.Atan = math.atan
Math.Atanh = atanh
Math.Atan2 = math.atan2 or math.atan
Math.BigMul = bigMul
Math.Ceiling = math.ceil
Math.Cbrt = cbrt
Math.Ceiling = ceil
Math.Clamp = clamp
Math.CopySign = copySign
Math.Cos = math.cos
Math.Cosh = cosh
Math.DivRem = divRem
Math.Exp = exp
Math.Floor = math.floor
Math.Floor = floor
Math.FusedMultiplyAdd = fusedMultiplyAdd
Math.IEEERemainder = IEEERemainder
Math.ILogB = ilogB
Math.Log = log
Math.Log10 = log10
Math.Max = math.max
Math.Min = math.min
Math.Log2 = log2
Math.Max = max
Math.MaxMagnitude = maxMagnitude
Math.Min = min
Math.MinMagnitude = minMagnitude
Math.Pow = pow
Math.ReciprocalEstimate = reciprocalEstimate
Math.ReciprocalSqrtEstimate = reciprocalSqrtEstimate
Math.Round = round
Math.ScaleB = scaleB
Math.Sign = sign
Math.Sin = math.sin
Math.SinCos = sinCos
Math.Sinh = sinh
Math.Sqrt = math.sqrt
Math.Sqrt = sqrt
Math.Tan = math.tan
Math.Tanh = tanh
Math.Truncate = truncate

System.define("System.Math", Math)
System.define("System.MathF", Math)
28 changes: 28 additions & 0 deletions CSharp.lua/LuaAst/LuaMemberAccessExpressionSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace CSharpLua.LuaAst {
public sealed class LuaMemberAccessExpressionSyntax : LuaExpressionSyntax {
Expand Down Expand Up @@ -110,4 +112,30 @@ internal override void Render(LuaRenderer renderer) {
renderer.Render(this);
}
}

public sealed class LuaPropertyTemplateExpressionSyntax : LuaExpressionSyntax {
public string GetTemplate { get; private set; }
public string SetTemplate { get; private set; }
public LuaCodeTemplateExpressionSyntax GetExpression { get; private set; }
public LuaIdentifierNameSyntax Name { get; private set; }
public readonly LuaArgumentListSyntax ArgumentList = new();

public LuaPropertyTemplateExpressionSyntax(string getExpression, string setExpression) {
GetTemplate = getExpression;
SetTemplate = setExpression;
}

public void Update(LuaExpressionSyntax expression, LuaCodeTemplateExpressionSyntax getExpression) {
Name = new LuaSymbolNameSyntax(expression);
GetExpression = getExpression;
}

internal override void Render(LuaRenderer renderer) {
renderer.Render(GetExpression);
}

internal IEnumerable<LuaExpressionSyntax> GetArguments(params LuaExpressionSyntax[] luaBinaryExpressionSyntax) {
return ArgumentList.Arguments.Concat(luaBinaryExpressionSyntax);
}
}
}
2 changes: 2 additions & 0 deletions CSharp.lua/LuaAst/LuaStatementSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public enum AttributeFlags {
MetadataAll = 1 << 3,
Template = 1 << 4,
Params = 1 << 5,
Get = 1 << 6,
Set = 1 << 7
}

public readonly List<LuaStatementSyntax> Statements = new();
Expand Down
15 changes: 14 additions & 1 deletion CSharp.lua/LuaSyntaxGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ private List<string> GetSymbolNames(ISymbol symbol) {
switch (symbol.Kind) {
case SymbolKind.Property:{
var propertySymbol = (IPropertySymbol)symbol;
if (IsPropertyField(propertySymbol)) {
if (IsPropertyField(propertySymbol) && !IsPropertyTemplate(propertySymbol)) {
names.Add(symbol.Name);
} else {
string baseName = GetSymbolBaseName(symbol);
Expand Down Expand Up @@ -1409,6 +1409,7 @@ public bool IsMonoBehaviourSpecialMethod(IMethodSymbol symbol) {
private readonly Dictionary<INamedTypeSymbol, Dictionary<ISymbol, ISymbol>> implicitInterfaceTypes_ = new(SymbolEqualityComparer.Default);
private readonly HashSet<INamedTypeSymbol> typesOfExtendSelf_ = new(SymbolEqualityComparer.Default);

private readonly ConcurrentDictionary<IPropertySymbol, bool> isPropertyTemplates_ = new(SymbolEqualityComparer.Default);
private readonly ConcurrentDictionary<IPropertySymbol, bool> isFieldProperties_ = new(SymbolEqualityComparer.Default);
private readonly ConcurrentDictionary<IEventSymbol, bool> isFieldEvents_ = new(SymbolEqualityComparer.Default);
private readonly ConcurrentDictionary<ISymbol, bool> isMoreThanLocalVariables_ = new(SymbolEqualityComparer.Default);
Expand Down Expand Up @@ -1630,6 +1631,18 @@ private bool IsPropertyFieldInternal(IPropertySymbol symbol) {
return symbol.IsAutoProperty();
}

internal bool IsPropertyTemplate(IPropertySymbol symbol) {
return isPropertyTemplates_.GetOrAdd(symbol, symbol => {
var node = symbol.GetDeclaringSyntaxNode();
if (node == null) {
return false;
} else {
return node.HasCSharpLuaAttribute(LuaDocumentStatement.AttributeFlags.Get)
|| symbol.GetDeclaringSyntaxNode().HasCSharpLuaAttribute(LuaDocumentStatement.AttributeFlags.Set);
}
});
}

internal bool IsPropertyField(IPropertySymbol symbol) {
return isFieldProperties_.GetOrAdd(symbol, symbol => {
bool? isMateField = XmlMetaProvider.IsPropertyField(symbol);
Expand Down
18 changes: 17 additions & 1 deletion CSharp.lua/LuaSyntaxNodeTransform.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, Lua
return InternalBuildCodeTemplateExpression(codeTemplate, null, null, null, targetExpression);
}

private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, IEnumerable<LuaExpressionSyntax> arguments, LuaIdentifierNameSyntax memberBindingIdentifier) {
return InternalBuildCodeTemplateExpression(codeTemplate, null, arguments.Select<LuaExpressionSyntax, Func<LuaExpressionSyntax>>(i => () => i), null, memberBindingIdentifier);
}

private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, ExpressionSyntax targetExpression, IEnumerable<LuaExpressionSyntax> arguments, IList<ITypeSymbol> typeArguments) {
return InternalBuildCodeTemplateExpression(codeTemplate, targetExpression, arguments.Select<LuaExpressionSyntax, Func<LuaExpressionSyntax>>(i => () => i), typeArguments);
}
Expand Down Expand Up @@ -417,6 +421,10 @@ private void AddExportEnum(ITypeSymbol enumType) {
generator_.AddExportEnum(enumType);
}

private bool IsPropertyTemplate(IPropertySymbol symbol) {
return generator_.IsPropertyTemplate(symbol);
}

private bool IsPropertyField(IPropertySymbol symbol) {
return generator_.IsPropertyField(symbol);
}
Expand Down Expand Up @@ -1108,6 +1116,14 @@ private LuaExpressionSyntax BuildFieldOrPropertyMemberAccessExpression(LuaExpres
return name;
}*/

if (name is LuaPropertyTemplateExpressionSyntax propertyTemplate) {
var getExpression = propertyTemplate.GetTemplate != null
? (LuaCodeTemplateExpressionSyntax)BuildCodeTemplateExpression(propertyTemplate.GetTemplate, new LuaSymbolNameSyntax(expression))
: null;
propertyTemplate.Update(expression, getExpression);
return propertyTemplate;
}

if (name is LuaPropertyAdapterExpressionSyntax propertyMethod) {
var arguments = propertyMethod.ArgumentList.Arguments;
if (arguments.Count == 1) {
Expand Down Expand Up @@ -1233,7 +1249,7 @@ private static IParameterSymbol GetParameterSymbol(IMethodSymbol symbol, Argumen
}

private void CheckValueTypeClone(ITypeSymbol typeSymbol, IdentifierNameSyntax node, ref LuaExpressionSyntax expression, bool isPropertyField = false) {
if (typeSymbol.IsCustomValueType() && !generator_.IsReadOnlyStruct(typeSymbol) && !typeSymbol.IsNullableWithBasicElementType()) {
if (typeSymbol.IsCustomValueType() && !generator_.IsReadOnlyStruct(typeSymbol) && !typeSymbol.IsNullableWithBasicElementType() && expression is not LuaPropertyTemplateExpressionSyntax) {
bool need = false;
if (isPropertyField) {
need = true;
Expand Down
5 changes: 5 additions & 0 deletions CSharp.lua/LuaSyntaxNodeTransform.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,11 @@ private void BuildOperatorMethodDeclaration(BaseMethodDeclarationSyntax node) {
}

public override LuaSyntaxNode VisitConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax node) {
var symbol = semanticModel_.GetDeclaredSymbol(node);
var codeTemplate = XmlMetaProvider.GetMethodCodeTemplate(symbol);
if (codeTemplate != null) {
return BuildCodeTemplateExpression(codeTemplate, node.ParameterList.Accept<LuaParameterListSyntax>(this).Parameters, null);
}
BuildOperatorMethodDeclaration(node);
return base.VisitConversionOperatorDeclaration(node);
}
Expand Down
Loading

0 comments on commit 9f7195b

Please sign in to comment.