Category Archives: Compiler

News about the development of the compiler.

Parameter denomination for function calls

There are a some languages which support parameter denomination for function calls.
XieXie is going to become one of them :-D

I actually don’t know the correct denomination for what I call “parameter denomination” ^^
But this was the best fitting name for it.

Consider a function with lots of parameters and all or nearly all have default values.
Now you want to set the last parameter in a function call but for all others you want to use the default values.
In C++ you have to write all arguments in your function call again.
Here is an example:

// C++ code:
void FunctionWithManyParameters(int a = 1, float b = 3.14, double c = (1.0/3.0), long double d = 0.5, unsigned long long int e = 27)
{
	/* ... */
}
FunctionWithManyParameters(1, 3.14, 1.0/3.0, 0.5, 100);
FunctionWithManyParameters(1, 3.14, 1.0/3.0, 0.5, 200);

In XieXie you can do it much easier:

// XieXie code:
void FunctionWithManyParameters(int a := 1, float b := 3.14, double c := (1.0/3.0), quad d := 0.5, ulong e := 27) {
	/* ... */
}
FunctionWithManyParameters(e: 100)
FunctionWithManyParameters(e: 200)

Certainly once you’ve started such parameter denomination you are forced to continue this for the rest of your argument list,
i.e. the following example is not valid:

// XieXie false example:
//                                  ERROR
//                                    V
FunctionWithManyParameters(1, e: 100, 3.14)

Patterns

Today I’ve started with the patterns (which are similar to the C++ template typenames).
This will be a lot of work, but XieXie must instantiate the templates by itself – and not just compile it to C++ templates.
That’s because the type-system works a little different to that of C++.
Here is a small example:

// Small C++ template structure with typename T
template <typename T> struct Test
{
    T foo;
    void Proc()
    {
        foo.Bar();
    }
};

// Small structure for template argument
struct TestFoo
{
    void Bar() { /* ... */ }
};

// No problem
Test<TestFoo> TemplateInstance1;

// ERROR: "foo.Bar" can not be used
// because "TemplateInstance2.foo" is a pointer!
Test<TestFoo*> TemplateInstance2;

In XieXie you can (or rather you will be able to) write this:

// Small XieXie template class with pattern T
class Test<pattern T> {
    T foo
    void Proc() {
        foo.Bar()
    }
}

// Small class for template argument
class TestFoo
{
    void Bar() { /* ... */ }
}

// No problem
Test<TestFoo> TemplateInstance1

// No problem as well
// ( 'TemplateInstance2.foo.Bar()' is automatically
//   translated to '(*TemplateInstance2.foo).Bar()' )
Test<TestFoo*> TemplateInstance2

// No problem as well with shared-pointer of
// raw-pointer of shared-pointer
// ( 'TemplateInstance3.foo.Bar()' is automatically
//   translated to '(***TemplateInstance3.foo).Bar()' )
Test<TestFoo@*@> TemplateInstance3

Anonymous Classes

Today I’ve started with anonymous classes. This is very usful, particular for event handlers and such things.

Here is a small example:

mainWindow.SetEventHandler(
	// Create new object from an anonymous
	// class which inherits from "WindowEventHandler".
	new class WindowEventHandler() {
		void OnResize(const ResizeEvent& event) {
			// Clamp window width
			if event.width < 800 {
				event.window.SetWidth(event.width)
			} else {
				event.window.SetWidth(800)
			}
			event.window.SetHeight(event.height)
		}
	}
)

The ‘copy’ keyword

Somtimes you may want to copy an object and all its sub-pointer objects (and not just the pointers itself). In those cases you have to write your own copy function with resursive calls. When you’ve ever wrote a tree-hierarchy, you should know how awful that can be.

Today I’ve started with a new and very nice keyword: the “copy”  keyword:

class BinaryTree {
	int data
	BinaryTree@ a
	BinaryTree@ b
}
BinaryTree@ treeA := /* create tree here ... */
BinaryTree@ treeB := copy treeA

This will translate to that:

//! BinaryTree class.
class BinaryTree
{
	public:
		int data;
		std::shared_ptr< BinaryTree > a;
		std::shared_ptr< BinaryTree > b;
		BinaryTree() :
			data(0),
			a(nullptr),
			b(nullptr)
		{
		}
		
		virtual ~BinaryTree()
		{
		}
		
		std::shared_ptr< BinaryTree > __XX__Copy() const
		{
			std::shared_ptr< BinaryTree > dest = std::make_shared< BinaryTree >();
			dest.data = data;
			if (a != nullptr)
			{
				dest.a = (*a).__XX__Copy();
			}
			if (b != nullptr)
			{
				dest.b = (*b).__XX__Copy();
			}
			return dest;
		}
};

std::shared_ptr< BinaryTree > treeA = /* create tree here ... */
std::shared_ptr< BinaryTree > treeB = (*treeA).__XX__Copy();

The actual implementation of the “__XX__Copy” function generation is still in progress,
so that part in the above example has been written by hand for presentation purspose ;-)

I already look forward to it :-)

Constness, pointers, procedure-objects …

Today I’ve got the constness, pointers, procedure-objects and arrays  parsing working.

Now you can write C++ like (crazy) interleaved type-denoters:

proc<void()>*[12]@ p :=
    new proc<void()>*[12]()

Which will be translated to:

std::shared_ptr<
  std::array<
    std::function< void () >*,
    12
  >
> p =
    std::make_shared<
      std::array<
        std::function< void () >*,
        12
      >
    >();

Range-based for loops

Today I splitted up the “loop” keyword into several types of loops: “while”, “until”, “do … while/until”, “for”, “forever”.

I also created a range-based ‘for-loop’ which can be unrolled using the #unroll attribute identifier.

More about loop commands will follow …

First executable App

After 6 weeks of development I already got my first executable test application :-D

I’m currently using a batch file on Windows to run all required programs:
1.) The XièXiè Compiler
2.) The GNU C++ Compiler

This is the my current batch file:

set INPUT_FILE=%1
set INTERMEDIATE_FILE=%INPUT_FILE%.cpp
set APP_FILE=%INPUT_FILE%.exe

start "XieXie Compiler" /B "D:\SoftwareEntwicklung\C++\HLC\Tools\XieXie-Compiler\cmake_msvc11\build\Release\xxc.exe" -f %INPUT_FILE%

start "GNU C++ Compiler" /B "D:\Anwendungen\CodeBlocks\MinGW\bin\g++" -std=c++11 -O3 -I "D:\SoftwareEntwicklung\C++\HLC\Tools\XieXie-Compiler\trunk\sources\Library" -o %APP_FILE% .\%INTERMEDIATE_FILE%