public override Node ExitHexLiteral(Token node) { LiteralElement element = IntegralLiteralElement.Create(node.Image, true, MyInUnaryNegate, MyServices); node.AddValue(element); return(node); }
// Emit elements into an array private static void EmitElementArrayLoad(ExpressionElement[] elements, Type arrayElementType, FleeILGenerator ilg, IServiceProvider services) { // Load the array length LiteralElement.EmitInt32Load(elements.Length, ilg); // Create the array ilg.Emit(OpCodes.Newarr, arrayElementType); // Store the new array in a unique local and remember the index LocalBuilder local = ilg.DeclareLocal(arrayElementType.MakeArrayType()); int arrayLocalIndex = local.LocalIndex; Utility.EmitStoreLocal(ilg, arrayLocalIndex); for (int i = 0; i <= elements.Length - 1; i++) { // Load the array Utility.EmitLoadLocal(ilg, arrayLocalIndex); // Load the index LiteralElement.EmitInt32Load(i, ilg); // Emit the element (with any required conversions) ExpressionElement element = elements[i]; element.Emit(ilg, services); ImplicitConverter.EmitImplicitConvert(element.ResultType, arrayElementType, ilg); // Store it into the array Utility.EmitArrayStore(ilg, arrayElementType); } // Load the array Utility.EmitLoadLocal(ilg, arrayLocalIndex); }
// Emit the load of a constant field. We can't emit a ldsfld/ldfld of a constant so we have to get its value // and then emit a ldc. private static void EmitLiteral(FieldInfo fi, FleeILGenerator ilg, IServiceProvider services) { object value = fi.GetValue(null); Type t = value.GetType(); TypeCode code = Type.GetTypeCode(t); LiteralElement elem = default(LiteralElement); switch (code) { case TypeCode.Char: case TypeCode.Byte: case TypeCode.SByte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: elem = new Int32LiteralElement(System.Convert.ToInt32(value)); break; case TypeCode.UInt32: elem = new UInt32LiteralElement((UInt32)value); break; case TypeCode.Int64: elem = new Int64LiteralElement((Int64)value); break; case TypeCode.UInt64: elem = new UInt64LiteralElement((UInt64)value); break; case TypeCode.Double: elem = new DoubleLiteralElement((double)value); break; case TypeCode.Single: elem = new SingleLiteralElement((float)value); break; case TypeCode.Boolean: elem = new BooleanLiteralElement((bool)value); break; case TypeCode.String: elem = new StringLiteralElement((string)value); break; default: elem = null; Debug.Fail("Unsupported constant type"); break; } elem.Emit(ilg, services); }
public override void Emit(FleeILGenerator ilg, System.IServiceProvider services) { int index = ilg.GetTempLocalIndex(typeof(TimeSpan)); Utility.EmitLoadLocalAddress(ilg, index); LiteralElement.EmitInt64Load(MyValue.Ticks, ilg); ConstructorInfo ci = typeof(TimeSpan).GetConstructor(new Type[] { typeof(long) }); ilg.Emit(OpCodes.Call, ci); Utility.EmitLoadLocal(ilg, index); }
public override Node ExitReal(Token node) { LiteralElement element = default(LiteralElement); string image = node.Image; if (image.EndsWith("f", StringComparison.OrdinalIgnoreCase) == true) { image = image.Remove(image.Length - 1); element = SingleLiteralElement.Parse(image, MyServices); } else { element = DoubleLiteralElement.Parse(image, MyServices); } node.AddValue(element); return(node); }
// Attempt to find the first type of integer that a number can fit into public static LiteralElement Create(string image, bool isHex, bool negated, IServiceProvider services) { StringComparison comparison = StringComparison.OrdinalIgnoreCase; ExpressionOptions options = services.GetService(typeof(ExpressionOptions)) as ExpressionOptions; // Convert to a double if option is set if (options.IntegersAsDoubles == true) { return(DoubleLiteralElement.Parse(image, services)); } bool hasUSuffix = image.EndsWith("u", comparison) & !image.EndsWith("lu", comparison); bool hasLSuffix = image.EndsWith("l", comparison) & !image.EndsWith("ul", comparison); bool hasULSuffix = image.EndsWith("ul", comparison) | image.EndsWith("lu", comparison); bool hasSuffix = hasUSuffix | hasLSuffix | hasULSuffix; LiteralElement constant = default(LiteralElement); NumberStyles numStyles = NumberStyles.Integer; if (isHex == true) { numStyles = NumberStyles.AllowHexSpecifier; image = image.Remove(0, 2); } if (hasSuffix == false) { // If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong. constant = Int32LiteralElement.TryCreate(image, isHex, negated); if ((constant != null)) { return(constant); } constant = UInt32LiteralElement.TryCreate(image, numStyles); if ((constant != null)) { return(constant); } constant = Int64LiteralElement.TryCreate(image, isHex, negated); if ((constant != null)) { return(constant); } return(new UInt64LiteralElement(image, numStyles)); } else if (hasUSuffix == true) { image = image.Remove(image.Length - 1); // If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong. constant = UInt32LiteralElement.TryCreate(image, numStyles); if ((constant != null)) { return(constant); } else { return(new UInt64LiteralElement(image, numStyles)); } } else if (hasLSuffix == true) { // If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong. image = image.Remove(image.Length - 1); constant = Int64LiteralElement.TryCreate(image, isHex, negated); if ((constant != null)) { return(constant); } else { return(new UInt64LiteralElement(image, numStyles)); } } else { // If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong. Debug.Assert(hasULSuffix == true, "expecting ul suffix"); image = image.Remove(image.Length - 2); return(new UInt64LiteralElement(image, numStyles)); } }