Example #1
0
        public static void TestPointerArray()
        {
            var ptrArr = new ABT.ArrayType(
                new ABT.PointerType(
                    new ABT.LongType()
                    ),
                3
                );

            Assert.AreEqual("long *a[3]", ptrArr.Decl("a"));
            Assert.AreEqual("long *[3]", ptrArr.Decl());
        }
Example #2
0
        public ISemantReturn <Tuple <ABT.ExprType, Option <ABT.Initr> > > GetDecoratedTypeAndInitr(ABT.Env env, ABT.ExprType baseType)
        {
            // Get the Type based on the declarator. Note that this might be an incomplete array.
            var type = Semant(this.Declr.DecorateType, baseType, ref env);

            Option <ABT.Initr> initrOption;

            if (this.Initr.IsNone)
            {
                initrOption = Option <ABT.Initr> .None;
            }
            else
            {
                // If an initializer is present:

                var initr = Semant(this.Initr.Value.GetInitr, ref env);

                // Check that the initializer conforms to the Type.
                initr = initr.ConformType(type);

                // If the object is an incomplete array, we must determine the length based on the initializer.
                if (type.Kind == ABT.ExprTypeKind.INCOMPLETE_ARRAY)
                {
                    // Now we need to determine the length.
                    // Find the last element in the Init list.
                    var lastOffset = -1;
                    initr.Iterate(type, (offset, _) => { lastOffset = offset; });

                    if (lastOffset == -1)
                    {
                        throw new InvalidOperationException("Cannot determine the length of the array based on an empty initializer list.");
                    }

                    var elemType = ((ABT.IncompleteArrayType)type).ElemType;

                    var numElems = 1 + lastOffset / ((ABT.IncompleteArrayType)type).ElemType.SizeOf;

                    type = new ABT.ArrayType(elemType, numElems);
                }

                initrOption = Option.Some(initr);
            }

            // Now everything is created. Check that the Type is complete.
            if (!type.IsComplete)
            {
                throw new InvalidOperationException("Cannot create an object with an incomplete Type.");
            }

            return(SemantReturn.Create(env, Tuple.Create(type, initrOption)));
        }
Example #3
0
        public static void TestArrayType()
        {
            var type = new ABT.ArrayType(
                new ABT.LongType(isConst: true),
                3
                );

            Assert.AreEqual("const long a[3]", type.Decl("a"));
            Assert.AreEqual("const long [3]", type.Decl());

            var multiDimArrayType = new ABT.ArrayType(
                new ABT.ArrayType(
                    new ABT.LongType(isConst: true),
                    3
                    ),
                4
                );

            Assert.AreEqual("const long a[4][3]", multiDimArrayType.Decl("a"));
            Assert.AreEqual("const long [3]", type.Decl());
        }
Example #4
0
 public static void TestPointerArray() {
     var ptrArr = new ABT.ArrayType(
         new ABT.PointerType(
             new ABT.LongType()
         ),
         3
     );
     Assert.AreEqual("long *a[3]", ptrArr.Decl("a"));
     Assert.AreEqual("long *[3]", ptrArr.Decl());
 }
Example #5
0
        public static void TestArrayType() {
            var type = new ABT.ArrayType(
                new ABT.LongType(isConst: true),
                3
            );
            Assert.AreEqual("const long a[3]", type.Decl("a"));
            Assert.AreEqual("const long [3]", type.Decl());

            var multiDimArrayType = new ABT.ArrayType(
                new ABT.ArrayType(
                    new ABT.LongType(isConst: true),
                    3
                ),
                4
            );
            Assert.AreEqual("const long a[4][3]", multiDimArrayType.Decl("a"));
            Assert.AreEqual("const long [3]", type.Decl());
        }