Example #1
0
        protected virtual TypeCode GetIntegerTypeCode(CiIntegerType integer, bool promote)
        {
            if (integer == CiSystem.LongType)
            {
                return(TypeCode.Int64);
            }
            if (promote || integer == CiSystem.IntType)
            {
                return(TypeCode.Int32);
            }
            CiRangeType range = (CiRangeType)integer;

            if (range.Min < 0)
            {
                if (range.Min < short.MinValue || range.Max > short.MaxValue)
                {
                    return(TypeCode.Int32);
                }
                if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                {
                    return(TypeCode.Int16);
                }
                return(TypeCode.SByte);
            }
            if (range.Max > ushort.MaxValue)
            {
                return(TypeCode.Int32);
            }
            if (range.Max > byte.MaxValue)
            {
                return(TypeCode.UInt16);
            }
            return(TypeCode.Byte);
        }
Example #2
0
 public void SplitBySign(out CiRangeType negative, out CiRangeType positive)
 {
     if (this.Min >= 0)
     {
         negative = null;
         positive = this;
     }
     else if (this.Max < 0)
     {
         negative = this;
         positive = null;
     }
     else
     {
         negative = new CiRangeType(this.Min, -1);
         positive = new CiRangeType(0, this.Max);
     }
 }
Example #3
0
        protected static string GetArrayElementType(CiNumericType type)
        {
            if (type == CiSystem.IntType)
            {
                return("Int32");
            }
            if (type == CiSystem.DoubleType)
            {
                return("Float64");
            }
            if (type == CiSystem.FloatType)
            {
                return("Float32");
            }
            if (type == CiSystem.LongType)
            {
                // TODO: UInt32 if possible?
                return("Float64");        // no 64-bit integers in JavaScript
            }
            CiRangeType range = (CiRangeType)type;

            if (range.Min < 0)
            {
                if (range.Min < short.MinValue || range.Max > short.MaxValue)
                {
                    return("Int32");
                }
                if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                {
                    return("Int16");
                }
                return("Int8");
            }
            if (range.Max > ushort.MaxValue)
            {
                return("Int32");
            }
            if (range.Max > byte.MaxValue)
            {
                return("Uint16");
            }
            return("Uint8");
        }
Example #4
0
 public CiRangeType Union(CiRangeType that)
 {
     if (that == null)
     {
         return(this);
     }
     if (that.Min < this.Min)
     {
         if (that.Max >= this.Max)
         {
             return(that);
         }
         return(new CiRangeType(that.Min, this.Max));
     }
     if (that.Max > this.Max)
     {
         return(new CiRangeType(this.Min, that.Max));
     }
     return(this);
 }
Example #5
0
        protected override TypeCode GetTypeCode(CiIntegerType integer, bool promote)
        {
            if (integer == CiSystem.LongType)
            {
                return(TypeCode.Int64);
            }
            if (promote || integer == CiSystem.IntType)
            {
                return(TypeCode.Int32);
            }
            CiRangeType range = (CiRangeType)integer;

            if (range.Min < 0)
            {
                if (range.Min < short.MinValue || range.Max > short.MaxValue)
                {
                    return(TypeCode.Int32);
                }
                if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                {
                    return(TypeCode.Int16);
                }
                return(TypeCode.SByte);
            }
            if (range.Max > short.MaxValue)
            {
                return(TypeCode.Int32);
            }
            if (range.Max > byte.MaxValue)
            {
                return(TypeCode.Int16);
            }
            if (range.Min == range.Max && range.Max > sbyte.MaxValue)     // CiLiteral
            {
                return(TypeCode.Byte);
            }
            return(TypeCode.SByte);    // store unsigned bytes in Java signed bytes
        }
Example #6
0
File: GenJs.cs Project: 00mjk/cito
        protected override void WriteNewArray(CiType elementType, CiExpr lengthExpr, CiPriority parent)
        {
            if (!(elementType is CiNumericType))
            {
                Write("new Array(");
                lengthExpr.Accept(this, CiPriority.Statement);
                Write(')');
                return;
            }

            string name;
            int    shift;

            if (elementType == CiSystem.IntType)
            {
                name  = "Int32";
                shift = 2;
            }
            else if (elementType == CiSystem.DoubleType)
            {
                name  = "Float64";
                shift = 3;
            }
            else if (elementType == CiSystem.FloatType)
            {
                name  = "Float32";
                shift = 2;
            }
            else if (elementType == CiSystem.LongType)
            {
                // TODO: UInt32 if possible?
                name  = "Float64";        // no 64-bit integers in JavaScript
                shift = 3;
            }
            else
            {
                CiRangeType range = (CiRangeType)elementType;
                if (range.Min < 0)
                {
                    if (range.Min < short.MinValue || range.Max > short.MaxValue)
                    {
                        name  = "Int32";
                        shift = 2;
                    }
                    else if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                    {
                        name  = "Int16";
                        shift = 1;
                    }
                    else
                    {
                        name  = "Int8";
                        shift = 0;
                    }
                }
                else if (range.Max > ushort.MaxValue)
                {
                    name  = "Int32";
                    shift = 2;
                }
                else if (range.Max > byte.MaxValue)
                {
                    name  = "Uint16";
                    shift = 1;
                }
                else
                {
                    name  = "Uint8";
                    shift = 0;
                }
            }

            Write("new ");
            Write(name);
            Write("Array(new ArrayBuffer(");
            if (shift == 0)
            {
                lengthExpr.Accept(this, CiPriority.Statement);
            }
            else if (lengthExpr is CiLiteral literalLength)
            {
                Write(((long)literalLength.Value) << shift);
            }
            else
            {
                lengthExpr.Accept(this, CiPriority.Shift);
                Write(" << ");
                Write(shift);
            }
            Write("))");
        }
Example #7
0
        void Write(CiType type, bool forConst = false)
        {
            switch (type)
            {
            case null:
                Write("void");
                break;

            case CiNumericType _:
                Write("number");
                break;

            case CiStringType _:
                Write("string");
                break;

            case CiEnum enu:
                Write(enu == CiSystem.BoolType ? "boolean" : enu.Name);
                break;

            case CiDictionaryType dict:
                Write("Record<");
                Write(dict.KeyType, forConst);
                Write(", ");
                Write(dict.ValueType, forConst);
                Write('>');
                break;

            case CiListType list:
                Write(list.ElementType, forConst);
                Write("[]");
                break;

            case CiArrayType array:
                CiType elementType = array.ElementType;
                if (!forConst && elementType is CiNumericType)
                {
                    if (!(array is CiArrayStorageType))
                    {
                        if (array.IsReadonlyPtr)
                        {
                            Write("readonly ");
                        }
                        Write("number[] | ");
                    }
                    if (array.IsReadonlyPtr)
                    {
                        Write("Readonly<");
                    }
                    if (elementType == CiSystem.IntType)
                    {
                        Write("Int32");
                    }
                    else if (elementType == CiSystem.DoubleType)
                    {
                        Write("Float64");
                    }
                    else if (elementType == CiSystem.FloatType)
                    {
                        Write("Float32");
                    }
                    else if (elementType == CiSystem.LongType)
                    {
                        Write("Float64");
                    }
                    else
                    {
                        CiRangeType range = (CiRangeType)elementType;
                        if (range.Min < 0)
                        {
                            if (range.Min < short.MinValue || range.Max > short.MaxValue)
                            {
                                Write("Int32");
                            }
                            else if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                            {
                                Write("Int16");
                            }
                            else
                            {
                                Write("Int8");
                            }
                        }
                        else if (range.Max > ushort.MaxValue)
                        {
                            Write("Int32");
                        }
                        else if (range.Max > byte.MaxValue)
                        {
                            Write("Uint16");
                        }
                        else
                        {
                            Write("Uint8");
                        }
                    }
                    Write("Array");
                    if (array.IsReadonlyPtr)
                    {
                        Write('>');
                    }
                }
                else
                {
                    if (forConst || array.IsReadonlyPtr)
                    {
                        Write("readonly ");
                    }
                    if (elementType is CiArrayType)
                    {
                        Write('(');
                    }
                    Write(elementType, forConst);
                    if (elementType is CiArrayType)
                    {
                        Write(')');
                    }
                    Write("[]");
                }
                break;

            default:
                CiType baseType = type is CiClassPtrType classPtr ? classPtr.Class : type;
                if (baseType == CiSystem.RegexClass)
                {
                    Write("RegExp");
                }
                else if (baseType == CiSystem.MatchClass)
                {
                    Write("RegExpMatchArray");
                }
                else
                {
                    Write(type.Name);
                }
                break;
            }
        }
Example #8
0
        protected override void WriteNewArray(CiType elementType, CiExpr lengthExpr, CiPriority parent)
        {
            if (!(elementType is CiNumericType))
            {
                WriteCall("new Array", lengthExpr);
                return;
            }

            string name;

            if (elementType == CiSystem.IntType)
            {
                name = "Int32";
            }
            else if (elementType == CiSystem.DoubleType)
            {
                name = "Float64";
            }
            else if (elementType == CiSystem.FloatType)
            {
                name = "Float32";
            }
            else if (elementType == CiSystem.LongType)
            {
                // TODO: UInt32 if possible?
                name = "Float64";         // no 64-bit integers in JavaScript
            }
            else
            {
                CiRangeType range = (CiRangeType)elementType;
                if (range.Min < 0)
                {
                    if (range.Min < short.MinValue || range.Max > short.MaxValue)
                    {
                        name = "Int32";
                    }
                    else if (range.Min < sbyte.MinValue || range.Max > sbyte.MaxValue)
                    {
                        name = "Int16";
                    }
                    else
                    {
                        name = "Int8";
                    }
                }
                else if (range.Max > ushort.MaxValue)
                {
                    name = "Int32";
                }
                else if (range.Max > byte.MaxValue)
                {
                    name = "Uint16";
                }
                else
                {
                    name = "Uint8";
                }
            }

            Write("new ");
            Write(name);
            WriteCall("Array", lengthExpr);
        }