Ejemplo n.º 1
0
        private DcParameter CreateParameterFromName(string name, uint divisor = 1, DcLongRange range = null)
        {
            // Builtin types:
            if (name == "uint8")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt8, divisor, range));
            }
            else if (name == "uint16")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt16, divisor, range));
            }
            else if (name == "uint32")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt32, divisor, range));
            }
            else if (name == "uint64")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt64, divisor, range));
            }
            else if (name == "int8")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int8, divisor, range));
            }
            else if (name == "int16")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int16, divisor, range));
            }
            else if (name == "int32")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int32, divisor, range));
            }
            else if (name == "int64")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int64, divisor, range));
            }
            else if (name == "float64")
            {
                return(new DcSimpleParameter(DcSubatomicType.Float64, range: range));
            }
            else if (name == "string")
            {
                return(new DcSimpleParameter(DcSubatomicType.String, range: range));
            }
            else if (name == "char")
            {
                return(new DcSimpleParameter(DcSubatomicType.Char, range: range));
            }
            else if (name == "blob")
            {
                return(new DcSimpleParameter(DcSubatomicType.Blob, range: range));
            }
            else if (name == "blob32")
            {
                return(new DcSimpleParameter(DcSubatomicType.Blob32, range: range));
            }
            else if (name == "int8array")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int8Array, divisor));
            }
            else if (name == "int16array")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int16Array, divisor));
            }
            else if (name == "int32array")
            {
                return(new DcSimpleParameter(DcSubatomicType.Int32Array, divisor));
            }
            else if (name == "uint8array")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt8Array, divisor));
            }
            else if (name == "uint16array")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt16Array, divisor));
            }
            else if (name == "uint32array")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt32Array, divisor));
            }
            else if (name == "uint32uint8array")
            {
                return(new DcSimpleParameter(DcSubatomicType.UInt32UInt8Array));
            }

            // Check against typedefs
            if (_dcFile.TryGetTypedef(name, out var typedef))
            {
                return(typedef.Parameter);
            }

            // Check for class
            if (_dcFile.TryGetClassByName(name, out var dclass))
            {
                return(new DcClassParameter(dclass));
            }

            throw new Exception($"Couldn't resolve name {name}");
        }
Ejemplo n.º 2
0
        private DcParameter BuildDcParameterFromContext(DcParser.ParameterContext context)
        {
            DcParameter parameter;

            uint divisor = 1;

            if (context.divisor != null)
            {
                divisor = uint.Parse(context.divisor.Text);
            }

            DcLongRange?paramRange = null;

            if (context.parameter_range() != null)
            {
                paramRange = new DcLongRange();
                ReadRangeArgumentsIntoRange(context.parameter_range().range_arguments(), paramRange);
            }

            // Array parameters
            var array = context.array;

            if (array != null)
            {
                var elementType = CreateParameterFromName(context.type.Text, divisor, paramRange);

                var range = new DcUIntRange();
                if (context.array.range != null)
                {
                    ReadRangeArgumentsIntoRange(context.array.range, range);
                }

                parameter = new DcArrayParameter(elementType, range);

                array = array.next;
                while (array != null)
                {
                    range = new DcUIntRange();
                    if (context.array.range != null)
                    {
                        ReadRangeArgumentsIntoRange(context.array.range, range);
                    }

                    parameter.AppendArraySpecification(range);
                    array = array.next;
                }
            }
            else
            {
                parameter = CreateParameterFromName(context.type.Text, divisor, paramRange);
            }

            parameter.Name = context.name?.Text ?? "";

            if (context.default_value() != null)
            {
                var bw = new SpanBufferWriter(stackalloc byte[512]);
                bw.WriteValueConstant(parameter, context.default_value().value);
                parameter.DefaultValue    = bw.Data.ToArray();
                parameter.HasDefaultValue = true;
            }

            return(parameter);
        }
Ejemplo n.º 3
0
        private void ReadRangeArgumentsIntoRange(DcParser.Range_argumentsContext context, DcLongRange range)
        {
            while (context != null)
            {
                var arg = context.range_argument();

                if (arg.single != null)
                {
                    if (!long.TryParse(arg.single.GetText(), out var single))
                    {
                        throw new Exception($"Error parsing array range '{arg.single.GetText()}' on line {arg.single.Start.Line}");
                    }

                    range.Add(single, single);
                }
                else if (arg.@char != null)
                {
                    // Index 1 because the text includes quotes
                    var value = (uint)[email protected][1];
                    range.Add(value, value);
                }
                else
                {
                    if (!long.TryParse(arg.min.GetText(), out var min))
                    {
                        throw new Exception($"Error parsing array range min '{arg.min.GetText()}' on line {arg.min.Start.Line}");
                    }

                    if (!long.TryParse(arg.min.GetText(), out var max))
                    {
                        throw new Exception($"Error parsing array range max '{arg.max.GetText()}' on line {arg.max.Start.Line}");
                    }

                    range.Add(min, max);
                }

                context = context.next;
            }
        }
Ejemplo n.º 4
0
        public DcSimpleParameter(DcSubatomicType type, uint divisor = 1, DcLongRange?range = null)
        {
            Type             = type;
            Divisor          = divisor;
            HasModulus       = false;
            HasFixedByteSize = false;
            PackType         = DcPackType.Invalid;
            NestedType       = DcSubatomicType.Invalid;
            HasNestedFields  = false;
            BytesPerElement  = 0;
            NumLengthBytes   = 2;

            switch (Type)
            {
            case DcSubatomicType.Int8Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.Int8;
                HasNestedFields = true;
                BytesPerElement = 1;
                break;

            case DcSubatomicType.Int16Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.Int16;
                HasNestedFields = true;
                BytesPerElement = 2;
                break;

            case DcSubatomicType.Int32Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.Int32;
                HasNestedFields = true;
                BytesPerElement = 4;
                break;

            case DcSubatomicType.UInt8Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.UInt8;
                HasNestedFields = true;
                BytesPerElement = 1;
                break;

            case DcSubatomicType.UInt16Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.UInt16;
                HasNestedFields = true;
                BytesPerElement = 2;
                break;

            case DcSubatomicType.UInt32Array:
                PackType        = DcPackType.Array;
                NestedType      = DcSubatomicType.UInt32;
                HasNestedFields = true;
                BytesPerElement = 4;
                break;

            case DcSubatomicType.UInt32UInt8Array:
                PackType        = DcPackType.Array;
                HasNestedFields = true;
                BytesPerElement = 5;
                break;

            case DcSubatomicType.Blob32:
                NumLengthBytes  = 4;
                PackType        = DcPackType.Blob;
                NestedType      = DcSubatomicType.UInt8;
                HasNestedFields = true;
                BytesPerElement = 1;
                break;

            case DcSubatomicType.Blob:
                PackType        = DcPackType.Blob;
                NestedType      = DcSubatomicType.UInt8;
                HasNestedFields = true;
                BytesPerElement = 1;
                break;

            case DcSubatomicType.String:
                PackType        = DcPackType.String;
                NestedType      = DcSubatomicType.Char;
                HasNestedFields = true;
                BytesPerElement = 1;
                break;

            case DcSubatomicType.Char:
                PackType         = DcPackType.String;
                HasFixedByteSize = true;
                BytesPerElement  = 1;
                break;

            case DcSubatomicType.Int8:
                PackType         = DcPackType.Int;
                HasFixedByteSize = true;
                FixedByteSize    = 1;
                break;

            case DcSubatomicType.Int16:
                PackType         = DcPackType.Int;
                HasFixedByteSize = true;
                FixedByteSize    = 2;
                break;

            case DcSubatomicType.Int32:
                PackType         = DcPackType.Int;
                HasFixedByteSize = true;
                FixedByteSize    = 4;
                break;

            case DcSubatomicType.Int64:
                PackType         = DcPackType.Int64;
                HasFixedByteSize = true;
                FixedByteSize    = 8;
                break;

            case DcSubatomicType.UInt8:
                PackType         = DcPackType.UInt;
                HasFixedByteSize = true;
                FixedByteSize    = 1;
                break;

            case DcSubatomicType.UInt16:
                PackType         = DcPackType.UInt;
                HasFixedByteSize = true;
                FixedByteSize    = 2;
                break;

            case DcSubatomicType.UInt32:
                PackType         = DcPackType.UInt;
                HasFixedByteSize = true;
                FixedByteSize    = 4;
                break;

            case DcSubatomicType.UInt64:
                PackType         = DcPackType.UInt64;
                HasFixedByteSize = true;
                FixedByteSize    = 8;
                break;

            case DcSubatomicType.Float64:
                PackType         = DcPackType.Double;
                HasFixedByteSize = true;
                FixedByteSize    = 8;
                break;
            }

            HasFixedStructure = HasFixedByteSize;

            LongRange = range ?? new DcLongRange();
            SetDivisor(divisor);

            if (NestedType != DcSubatomicType.Invalid)
            {
                NestedField = CreateNestedField(NestedType, divisor);
            }
            else if (Type == DcSubatomicType.UInt32UInt8Array)
            {
                NestedField = UInt32UInt8Type;
            }
            else
            {
                NestedField = null;
            }
        }