"Hello, world!" example for CCI/AST?

Sep 17, 2009 at 12:40 PM
Edited Sep 17, 2009 at 4:47 PM


Sometimes beginning to use a library it's easier when you have a small example that does few things with it, even if it's not very useful or realistic.

You understand some basic features and the rest is much easier to learn, even looking at the source code and browsing the class tree or with intellisense. Then you can have a look at more complex examples (like the Small Basic compiler) with better chances.

For the CCI/Metatada components I found a "hello world" example in your forum and now I'm using them in a prototype of my compiler.

Unfortunately so far I found harder to understand the way I should use CCI/AST and for this reason I'm using my own AST representation.

Could you please make a 100-200 lines example?


[Edit: fixed several typos. Sorry for them.]

Sep 17, 2009 at 2:08 PM

Your observation is spot on and I have that on my list of things to do, but don't hold your breath, my list is pretty long.

If you already have your own AST then my recommendation is that you project your AST onto the CCI Code Model. Of course, you could go directly to the Metadata Model and IL, but going to the Code Model should be easier and you get to reuse the work that has gone into CodeModelToIL.

One way to build a Code Model is to use the classes in MutableCodeModel. Another is to implement the Code Model interfaces on your AST nodes. You can also mix the two approaches, which is what happens in the CCI AST Model.

Making your AST classes be subclasses of the CCI AST Model base classes, is probably the easiest thing to do when you are starting from scratch, since you get to reuse a lot of projection code, symbol table management, name resolution, type infererence, over load resolution and so on. The CCI AST Model also supports incremental compilation for IDE scenarios.

Sep 25, 2009 at 3:14 PM

Hello, Herman.

The problem is that, even if apparently the relationships between the classes in CCI AST are easy to understand, setting up the all the properties and references between the data structures is not obvious.

I'm trying to write a compiler for HelloLang using on CCI AST components. HelloLang should be language with no syntax of any kind (source file must be empty) that can only be used to express the "Hello, World!" program.

It's both an attempt to strip the SmallBasic compiler to the bare minimum to generate a program and an attempt to understand that compiler.

At them moment I get a trivial assembly. Some piece of data is not being processed because I'm not setting some reference somewhere but I don't know where exactly is the problem. The "everything is computed lazily" nature of the framework (that can be appreciated for its efficiency) makes even harder to understand what is needed to get a given result.

A question regarding the SmallBasic compiler: why the sources are a property of the assembly and not of the compilation?


Oct 16, 2009 at 1:54 AM

I have now at long last had a few hours to devote to this. The result is checked in as changeset 32890.

This sample is the smallest I can up with for now and it does not deal with issues such as how to produce a PDB.

As for your question about the SmallBasic compiler. Since the assembly and the compilation have links to each other and they are both immutable, one of them has to be constructed by the other. In the SmallBasic case, constructing an Assembly from the compiler main routine seemed slightly more natural since the object of the main routine is to produce an assembly. The compilation is basically a means to that end.

Oct 16, 2009 at 11:52 PM

Thanks, Herman.

I'm going to check it out right away.

Thanks for the explanation.


Oct 18, 2009 at 12:47 AM
Edited Oct 18, 2009 at 5:27 AM

There is now also a sample that produces the "hello" program using the AST model, which is something closer to what a parser would do (and also closer to one way of using System.CodeDom).

It is quite a bit more involved than the Code Model version, not least because the base classes are very general and provide for incremental compilation.

It should become more readable once I get around to writing in the code comments that are now so conspicously missing and supply an acompanying text. These are planned, but don't hold your breadth, just yet.

Oct 18, 2009 at 2:19 AM


Your examples are readable and clear even without comments.

Very useful because I now have a reference point.

Thanks for the time spent to write them.