Example #1
0
        private static LiteralNode ParseRational(ProjectParser parser, string value, TypeReference requestedType, SequencePoint point)
        {
            try
            {
                switch (requestedType.MetadataType)
                {
                case MetadataType.Single:
                    return(new LiteralNode(Convert.ToSingle(value, CultureInfo.InvariantCulture), requestedType, point));

                case MetadataType.Double:
                    return(new LiteralNode(Convert.ToDouble(value, CultureInfo.InvariantCulture), requestedType, point));

                default:
                    ContractsHelper.AssertUnreachable("Unexpected type {0} in ParseRational", requestedType.MetadataType);
                    return(Utils.Utils.Fail <LiteralNode>());
                }
            }
            catch (OverflowException)
            {
                ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, overflow", value, requestedType.FullName);
                return(Utils.Utils.Fail <LiteralNode>());
            }
            catch (FormatException)
            {
                ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, format error", value, requestedType.FullName);
                return(Utils.Utils.Fail <LiteralNode>());
            }
        }
Example #2
0
 private static LiteralNode ParseInteger(ProjectParser parser, string value, TypeReference requestedType, SequencePoint point)
 {
     try
     {
         var parsed = BigInteger.Parse(value, CultureInfo.InvariantCulture);
         var type   = GetLowestConversion(parser, parsed);
         if (type == null)
         {
             ErrorCode.IntegerOverflow.ReportAndThrow(point, "Cannot fit {0} into an integer, use BigInteger.Parse", value);
         }
         else
         {
             //return what we parsed, if requested type can't fit
             if (type.IsAssignableTo(requestedType))
             {
                 type = requestedType;
             }
         }
         if (parsed.Sign >= 0)
         {
             return(new LiteralNode((ulong)parsed, type, point));
         }
         else
         {
             return(new LiteralNode((long)parsed, type, point));
         }
     }
     catch (FormatException)
     {
         ContractsHelper.AssumeUnreachable("Error parsing {0} as integer", value);
     }
     return(Utils.Utils.Fail <LiteralNode>());
 }
Example #3
0
        /// <summary>
        /// Returns lowest possible type for this integer or null, unsigned if possible
        /// </summary>
        private static TypeReference GetLowestConversion(ProjectParser parser, BigInteger integer)
        {
            var conversions = GetImplicitConversions(parser, integer);

            if (integer >= 0)
            {
                return(conversions.FirstOrDefault(type => type.IsUnsignedInteger()));
            }
            else
            {
                return(conversions.FirstOrDefault());
            }
        }
Example #4
0
        private static LiteralNode ParseValue(ProjectParser parser, string value, TypeReference type, SequencePoint point)
        {
            try
            {
                switch (type.MetadataType)
                {
                case MetadataType.String:
                case MetadataType.Boolean:
                    return(new LiteralNode(value, type, point));

                case MetadataType.SByte:
                case MetadataType.Byte:
                case MetadataType.Int16:
                case MetadataType.UInt16:
                case MetadataType.Int32:
                case MetadataType.UInt32:
                case MetadataType.Int64:
                case MetadataType.UInt64:
                    return(ParseInteger(parser, value, type, point));

                case MetadataType.Char:
                {
                    if (value.Length > 1)
                    {
                        ErrorCode.MultipleCharacterLiteral.ReportAndThrow(point, "Character literal must not be longer than one character (found: '{0}').", value);
                    }

                    return(new LiteralNode(value, type, point));
                }

                case MetadataType.Single:
                case MetadataType.Double:
                    return(ParseRational(parser, value, type, point));

                default:

                    ContractsHelper.AssumeUnreachable(String.Format("Unexpected literal type {0}", type.FullName));
                    return(null);   //unreachable
                }
            }
            catch (OverflowException)
            {
                ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, overflow", value, type.FullName);
                return(null);//unreachable
            }
            catch (FormatException)
            {
                ErrorCode.InvalidStructure.ReportAndThrow(point, "Could not parse {0} as {1}, format error", value, type.FullName);
                return(null);//unreachable
            }
        }
Example #5
0
        public static ProjectParser ParseAll(AssemblyEmitter assembly, CompilerArguments compilerArguments, string[] sources, bool emit)
        {
            Contract.Requires(compilerArguments.SourceFiles.Count == sources.Length);

            ProjectParser projectParser = new ProjectParser(assembly, emit);
            var           roots         = sources.Select(s => Lexer.Lexer.Lex(s)).ToArray();

            for (int i = 0; i < sources.Length; i++)
            {
                var filePath = compilerArguments.SourceFiles[i];
                projectParser.parsers.Add(new Parser(projectParser, filePath, compilerArguments.FileToNamespaceMap[filePath]));
            }

            for (int i = 0; i < sources.Length; i++)
            {
                projectParser.parsers[i].ParseDeclarations(roots[i]);
            }
            projectParser.parsers.ForEach(p => p.ParseInitializers());
            projectParser.parsers.ForEach(p => p.Emit());

            return(projectParser);
        }
Example #6
0
        private static IEnumerable <TypeReference> GetImplicitConversions(ProjectParser parser, BigInteger integer)
        {
            List <TypeReference> ret = new List <TypeReference>();

            if (integer >= 0)
            {
                if (integer <= sbyte.MaxValue)
                {
                    ret.Add(parser.Int8);
                }
                if (integer <= byte.MaxValue)
                {
                    ret.Add(parser.UInt8);
                }
                if (integer <= short.MaxValue)
                {
                    ret.Add(parser.Int16);
                }
                if (integer <= ushort.MaxValue)
                {
                    ret.Add(parser.UInt16);
                }
                if (integer <= char.MaxValue)
                {
                    ret.Add(parser.Char);
                }
                if (integer <= int.MaxValue)
                {
                    ret.Add(parser.Int32);
                }
                if (integer <= uint.MaxValue)
                {
                    ret.Add(parser.UInt32);
                }
                if (integer <= long.MaxValue)
                {
                    ret.Add(parser.Int64);
                }
                if (integer <= ulong.MaxValue)
                {
                    ret.Add(parser.UInt64);
                }
            }
            else
            {
                if (integer >= sbyte.MinValue)
                {
                    ret.Add(parser.Int8);
                }
                if (integer >= short.MinValue)
                {
                    ret.Add(parser.Int16);
                }
                if (integer >= int.MinValue)
                {
                    ret.Add(parser.Int32);
                }
                if (integer >= long.MinValue)
                {
                    ret.Add(parser.Int64);
                }
            }
            return(ret);
        }