This commit is contained in:
2025-10-08 09:49:37 +08:00
commit 284e764345
99 changed files with 21742 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals
{
internal class BooleanLiteralElement : LiteralElement
{
private readonly bool _myValue;
public BooleanLiteralElement(bool value)
{
_myValue = value;
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
EmitLoad(_myValue, ilg);
}
public override System.Type ResultType => typeof(bool);
}
}

View File

@@ -0,0 +1,24 @@
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals
{
internal class CharLiteralElement : LiteralElement
{
private readonly char _myValue;
public CharLiteralElement(char value)
{
_myValue = value;
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
int intValue = Convert.ToInt32(_myValue);
EmitLoad(intValue, ilg);
}
public override System.Type ResultType => typeof(char);
}
}

View File

@@ -0,0 +1,42 @@
using System.Reflection;
using System.Reflection.Emit;
using System.Globalization;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
using Flee.PublicTypes;
using Flee.Resources;
namespace Flee.ExpressionElements.Literals
{
internal class DateTimeLiteralElement : LiteralElement
{
private DateTime _myValue;
public DateTimeLiteralElement(string image, ExpressionContext context)
{
ExpressionParserOptions options = context.ParserOptions;
if (DateTime.TryParseExact(image, options.DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out _myValue) == false)
{
base.ThrowCompileException(CompileErrorResourceKeys.CannotParseType, CompileExceptionReason.InvalidFormat, typeof(DateTime).Name);
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
int index = ilg.GetTempLocalIndex(typeof(DateTime));
Utility.EmitLoadLocalAddress(ilg, index);
LiteralElement.EmitLoad(_myValue.Ticks, ilg);
ConstructorInfo ci = typeof(DateTime).GetConstructor(new Type[] { typeof(long) });
ilg.Emit(OpCodes.Call, ci);
Utility.EmitLoadLocal(ilg, index);
}
public override System.Type ResultType => typeof(DateTime);
}
}

View File

@@ -0,0 +1,84 @@
using System.Globalization;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals.Integral
{
internal class Int32LiteralElement : IntegralLiteralElement
{
private Int32 _myValue;
private const string MinValue = "2147483648";
private readonly bool _myIsMinValue;
public Int32LiteralElement(Int32 value)
{
_myValue = value;
}
private Int32LiteralElement()
{
_myIsMinValue = true;
}
public static Int32LiteralElement TryCreate(string image, bool isHex, bool negated)
{
if (negated == true & image == MinValue)
{
return new Int32LiteralElement();
}
else if (isHex == true)
{
Int32 value = default(Int32);
// Since Int32.TryParse will succeed for a string like 0xFFFFFFFF we have to do some special handling
if (Int32.TryParse(image, NumberStyles.AllowHexSpecifier, null, out value) == false)
{
return null;
}
else if (value >= 0 & value <= Int32.MaxValue)
{
return new Int32LiteralElement(value);
}
else
{
return null;
}
}
else
{
Int32 value = default(Int32);
if (Int32.TryParse(image,out value) == true)
{
return new Int32LiteralElement(value);
}
else
{
return null;
}
}
}
public void Negate()
{
if (_myIsMinValue == true)
{
_myValue = Int32.MinValue;
}
else
{
_myValue = -_myValue;
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
EmitLoad(_myValue, ilg);
}
public override System.Type ResultType => typeof(Int32);
public int Value => _myValue;
}
}

View File

@@ -0,0 +1,82 @@
using System.Globalization;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals.Integral
{
internal class Int64LiteralElement : IntegralLiteralElement
{
private Int64 _myValue;
private const string MinValue = "9223372036854775808";
private readonly bool _myIsMinValue;
public Int64LiteralElement(Int64 value)
{
_myValue = value;
}
private Int64LiteralElement()
{
_myIsMinValue = true;
}
public static Int64LiteralElement TryCreate(string image, bool isHex, bool negated)
{
if (negated == true & image == MinValue)
{
return new Int64LiteralElement();
}
else if (isHex == true)
{
Int64 value = default(Int64);
if (Int64.TryParse(image, NumberStyles.AllowHexSpecifier, null, out value) == false)
{
return null;
}
else if (value >= 0 & value <= Int64.MaxValue)
{
return new Int64LiteralElement(value);
}
else
{
return null;
}
}
else
{
Int64 value = default(Int64);
if (Int64.TryParse(image, out value) == true)
{
return new Int64LiteralElement(value);
}
else
{
return null;
}
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
EmitLoad(_myValue, ilg);
}
public void Negate()
{
if (_myIsMinValue == true)
{
_myValue = Int64.MinValue;
}
else
{
_myValue = -_myValue;
}
}
public override System.Type ResultType => typeof(Int64);
}
}

View File

@@ -0,0 +1,34 @@
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals.Integral
{
internal class UInt32LiteralElement : IntegralLiteralElement
{
private readonly UInt32 _myValue;
public UInt32LiteralElement(UInt32 value)
{
_myValue = value;
}
public static UInt32LiteralElement TryCreate(string image, System.Globalization.NumberStyles ns)
{
UInt32 value = default(UInt32);
if (UInt32.TryParse(image, ns, null, out value) == true)
{
return new UInt32LiteralElement(value);
}
else
{
return null;
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
EmitLoad(Convert.ToInt32(_myValue), ilg);
}
public override System.Type ResultType => typeof(UInt32);
}
}

View File

@@ -0,0 +1,34 @@
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals.Integral
{
internal class UInt64LiteralElement : IntegralLiteralElement
{
private readonly UInt64 _myValue;
public UInt64LiteralElement(string image, System.Globalization.NumberStyles ns)
{
try
{
_myValue = UInt64.Parse(image, ns);
}
catch (OverflowException ex)
{
base.OnParseOverflow(image);
}
}
public UInt64LiteralElement(UInt64 value)
{
_myValue = value;
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
EmitLoad(Convert.ToInt64(_myValue), ilg);
}
public override System.Type ResultType => typeof(UInt64);
}
}

View File

@@ -0,0 +1,16 @@
using System.Reflection.Emit;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals
{
internal class NullLiteralElement : LiteralElement
{
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
ilg.Emit(OpCodes.Ldnull);
}
public override System.Type ResultType => typeof(Null);
}
}

View File

@@ -0,0 +1,76 @@
using System.Reflection.Emit;
using System.Reflection;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
using Flee.PublicTypes;
namespace Flee.ExpressionElements.Literals.Real
{
internal class DecimalLiteralElement : RealLiteralElement
{
private static readonly ConstructorInfo OurConstructorInfo = GetConstructor();
private readonly decimal _myValue;
private DecimalLiteralElement()
{
}
public DecimalLiteralElement(decimal value)
{
_myValue = value;
}
private static ConstructorInfo GetConstructor()
{
Type[] types = {
typeof(Int32),
typeof(Int32),
typeof(Int32),
typeof(bool),
typeof(byte)
};
return typeof(decimal).GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.Any, types, null);
}
public static DecimalLiteralElement Parse(string image, IServiceProvider services)
{
ExpressionParserOptions options = (ExpressionParserOptions)services.GetService(typeof(ExpressionParserOptions));
DecimalLiteralElement element = new DecimalLiteralElement();
try
{
decimal value = options.ParseDecimal(image);
return new DecimalLiteralElement(value);
}
catch (OverflowException ex)
{
element.OnParseOverflow(image);
return null;
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
int index = ilg.GetTempLocalIndex(typeof(decimal));
Utility.EmitLoadLocalAddress(ilg, index);
int[] bits = decimal.GetBits(_myValue);
EmitLoad(bits[0], ilg);
EmitLoad(bits[1], ilg);
EmitLoad(bits[2], ilg);
int flags = bits[3];
EmitLoad((flags >> 31) == -1, ilg);
EmitLoad(flags >> 16, ilg);
ilg.Emit(OpCodes.Call, OurConstructorInfo);
Utility.EmitLoadLocal(ilg, index);
}
public override System.Type ResultType => typeof(decimal);
}
}

View File

@@ -0,0 +1,46 @@
using System.Reflection.Emit;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
using Flee.PublicTypes;
namespace Flee.ExpressionElements.Literals.Real
{
internal class DoubleLiteralElement : RealLiteralElement
{
private readonly double _myValue;
private DoubleLiteralElement()
{
}
public DoubleLiteralElement(double value)
{
_myValue = value;
}
public static DoubleLiteralElement Parse(string image, IServiceProvider services)
{
ExpressionParserOptions options = (ExpressionParserOptions)services.GetService(typeof(ExpressionParserOptions));
DoubleLiteralElement element = new DoubleLiteralElement();
try
{
double value = options.ParseDouble(image);
return new DoubleLiteralElement(value);
}
catch (OverflowException ex)
{
element.OnParseOverflow(image);
return null;
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
ilg.Emit(OpCodes.Ldc_R8, _myValue);
}
public override System.Type ResultType => typeof(double);
}
}

View File

@@ -0,0 +1,45 @@
using System.Reflection.Emit;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
using Flee.PublicTypes;
namespace Flee.ExpressionElements.Literals.Real
{
internal class SingleLiteralElement : RealLiteralElement
{
private readonly float _myValue;
private SingleLiteralElement()
{
}
public SingleLiteralElement(float value)
{
_myValue = value;
}
public static SingleLiteralElement Parse(string image, IServiceProvider services)
{
ExpressionParserOptions options = (ExpressionParserOptions)services.GetService(typeof(ExpressionParserOptions));
SingleLiteralElement element = new SingleLiteralElement();
try
{
float value = options.ParseSingle(image);
return new SingleLiteralElement(value);
}
catch (OverflowException ex)
{
element.OnParseOverflow(image);
return null;
}
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
ilg.Emit(OpCodes.Ldc_R4, _myValue);
}
public override System.Type ResultType => typeof(float);
}
}

View File

@@ -0,0 +1,23 @@
using System.Reflection.Emit;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
namespace Flee.ExpressionElements.Literals
{
internal class StringLiteralElement : LiteralElement
{
private readonly string _myValue;
public StringLiteralElement(string value)
{
_myValue = value;
}
public override void Emit(FleeILGenerator ilg, IServiceProvider services)
{
ilg.Emit(OpCodes.Ldstr, _myValue);
}
public override System.Type ResultType => typeof(string);
}
}

View File

@@ -0,0 +1,38 @@
using System.Reflection;
using System.Reflection.Emit;
using Flee.ExpressionElements.Base.Literals;
using Flee.InternalTypes;
using Flee.PublicTypes;
using Flee.Resources;
namespace Flee.ExpressionElements.Literals
{
internal class TimeSpanLiteralElement : LiteralElement
{
private TimeSpan _myValue;
public TimeSpanLiteralElement(string image)
{
if (TimeSpan.TryParse(image, out _myValue) == false)
{
base.ThrowCompileException(CompileErrorResourceKeys.CannotParseType, CompileExceptionReason.InvalidFormat, typeof(TimeSpan).Name);
}
}
public override void Emit(FleeILGenerator ilg, System.IServiceProvider services)
{
int index = ilg.GetTempLocalIndex(typeof(TimeSpan));
Utility.EmitLoadLocalAddress(ilg, index);
LiteralElement.EmitLoad(_myValue.Ticks, ilg);
ConstructorInfo ci = typeof(TimeSpan).GetConstructor(new Type[] { typeof(long) });
ilg.Emit(OpCodes.Call, ci);
Utility.EmitLoadLocal(ilg, index);
}
public override System.Type ResultType => typeof(TimeSpan);
}
}