Mercredi 30 mai 2007
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, ...
Par duff - Publié dans : mono
Ecrire un commentaire - Voir les commentaires - Recommander
Retour à l'accueil

Calendrier

Novembre 2009
L M M J V S D
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
<< < > >>

Recherche

Créer un blog sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus