A new post about the new task I take on mono. I have joined mono and worked a little bit on winforms.
But, now, I am more on a new thing:
The mono javascript compiler for Moonlight.I have based my worked on a beautiful
dotnetguru tutorial!
It is a real pleasure to make a big thing like that for mono. I discover that done a compiler is really easy and can be done quickly.To make a good compiler you must think how the language work.
First, you must do a Lexer wich is a kind of system which parse source file a create token foreach gramatical word/punctuation/operator of my language.
To sum up, It is a big switch case for each character of the source file.
It is a bit more complicated for keyword/number but we get the main idea with that function
public int GetNextToken()
{
StripWhiteSpace();
char next = source[index];
switch (next)
{
case '+':
return TokenType.OperatorPlus;
// ...
}
}
With that you will have a good start.
But after, you must check the syntax and here it is not so easy...
You must perfectely understand the language to make rules and build a tree.
For example, a small basic tree:
This tree will generate the code or will be base for runtime directely.
And it is all.
You have a lot of tool to help generate your lexer and parser.
lex/yacc,
ANTLR, jay can help you to build your own language and his grammar!!
There are a lot of existing language which can be very funny as the brainfuck or F, the D, ...