Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="shape">The bus shape.</param>
 public DataType(ParseToken token, BusShape shape)
     : base(token)
 {
     Type     = ILType.Bus;
     BitWidth = -1;
     Shape    = shape ?? throw new ArgumentNullException(nameof(shape));
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="parent">The array data type.</param>
 public DataType(ParseToken token, DataType parent)
     : base(token)
 {
     Type     = parent.Type;
     BitWidth = parent.BitWidth;
     Shape    = parent.Shape;
     EnumType = parent.EnumType;
 }
Esempio n. 3
0
        /// <summary>
        /// Constructs a new type definition for a bus shape
        /// </summary>
        /// <param name="source">The source parse token</param>
        /// <param name="name">The name of this type definition</param>
        /// <param name="alias">The shape to use</param>
        public TypeDefinition(ParseToken source, Identifier name, IEnumerable <BusSignalDeclaration> signals)
            : base(source)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
            if (string.IsNullOrWhiteSpace(Name.Name))
            {
                throw new ParserException($"The name of a type definition cannot be anonymous", source);
            }

            if (signals == null)
            {
                throw new ArgumentNullException(nameof(signals));
            }

            Shape = new BusShape(source, signals);

            Initializers = signals
                           .ToDictionary(
                x => x.Name.Name,
                x => x.Initializer
                );
        }
Esempio n. 4
0
        /// <summary>
        /// Returns a value indicating if <paramref name="a"/> can be assigned to <paramref name="b"/>
        /// </summary>
        /// <param name="a">One shape</param>
        /// <param name="b">Another shape</param>
        /// <param name="throwOnError">A value indicating if an error message is thrown for incompatible bus shapes</param>
        /// <returns><c>true</c> if <paramref name="a"/> can be assigned to <paramref name="b"/>; <c>false</c> otherwise</returns>
        public static bool CanAssignTo(BusShape a, BusShape b, bool throwOnError = false)
        {
            foreach (var signal in b.Signals)
            {
                if (!a.Signals.TryGetValue(signal.Key, out var t) || !object.Equals(t.Type, signal.Value.Type) || !object.Equals(t.Direction, signal.Value.Direction))
                {
                    if (throwOnError)
                    {
                        if (t.Type == null)
                        {
                            throw new Exception($"Incompatible bus shapes, missing signal {signal.Key} of type {signal.Value}");
                        }
                        else
                        {
                            throw new Exception($"Incompatible bus shapes, signal {signal.Key} has type {t} bus should be type {signal.Value}");
                        }
                    }

                    return(false);
                }
            }

            return(true);
        }