Initializer Lists and new Attributes Syntax

Today I’ve added the initializer lists which should work similiar to the C++11′s equivalent.

// --- Already implemented: ---
int[] array1 := { 1, 2, 3 }
string[] array2 := { "Hello", "World" }
int[][] array3 := { { 1, 2, 3 }, { 42, 19 } }

// --- Currently just in planning: ---

// Test class
class Test { int foo, bar }

// Default initialization
Test t1 := { 12, 5 }

// Initialization with member denomination (like with the named parameters)
Test t2 := { foo: 12, bar: 5 }
Test t3 := { bar: 5, foo: 12 }

I also changed the syntax for the attributes. Now you can write several attributes for a statement:

void DoSomething(int x) { /* ... */ }

// Auto-unroll loop
[[unroll]]
for i := 1 .. 3 {
    DoSomething(i)
}

// Pack class (and disable alignment)
[[pack]] class Foo {
    [[set,get]] int bar
}

And this is the generated code:

// Example.xx.cpp
// XieXie generated source file
// Sat Feb 01 21:10:13 2014

//! DoSomething function
//! \param[in] x Input parameter x.
void DoSomething(int x)
{
}

// Unrolled Range-Based For Loop
{
    // Iteration 1
    unsigned char i = 1;
    {
        DoSomething(i);
    }
    
    // Iteration 2
    i = 2;
    {
        DoSomething(i);
    }
    
    // Iteration 3
    i = 3;
    {
        DoSomething(i);
    }
    
}
// Structure Packing Alignment
#if defined(_MSC_VER)
#   pragma pack(push, packing)
#   pragma pack(1)
#   define __XX__PACK_STRUCT__
#elif defined(__GNUC__)
#   define __XX__PACK_STRUCT__ __attribute__((packed))
#else
#   define __XX__PACK_STRUCT__
#endif

//! Foo class.
class Foo
{
    public:
        int bar;
        Foo() :
            bar(0)
        {
        }
        
        ~Foo()
        {
        }
        
    public:
        //! Setter for "bar" member variable.
        //! \param[in] bar Specifies the new value.
        //! \see bar
        inline void SetBar(const int& bar)
        {
            this->bar = bar;
        }
        //! Getter for "bar" member variable.
        //! \see bar
        //! \return Current value.
        inline const int& GetBar() const
        {
            return bar;
        }
        
} __XX__PACK_STRUCT__;

// /Structure Packing Alignment
#ifdef _MSC_VER
#   pragma pack(pop, packing)
#endif
#undef __XX__PACK_STRUCT__

// ================

The old syntax with the ‘#’ character was just a temporary solution.
The new one works correct together with the rest of the grammar.

Leave a Reply