Quick-and-Dirty Guide to Transforming Compact into your Compiler

From Wiki**3

OBSOLETE: do not use

THIS GUIDE IS NOT NECESSARY IF YOU HAVE ACCESS TO THE COURSE REPOSITORY. THIS REPOSITORY ALREADY HAS AN INITIAL VERSION OF THE COMPILER -- IN THIS CASE, JUST GET THE PROJECT FROM THE REPOSITORY AND CONTINUE FROM THERE

Compact is a very simple compiler. Nevertheless, it already incorporates all major components in a compiler (except for type validation).

The following is a quick guide to start your compiler from the Compact sources.

Unpacking the Goods

The first thing you have to do is check whether your development environment is friendly. Your basic start up kit (CDK, RTS, and Compact) was made for Linux, specifically, openSUSE. It may, however, compile and run without problems on almost any Linux- and Unix-like system. Some restrictions apply (e.g., the 32-bit vs. 64-bit issue could be a major source of unhappiness, since the backend is only able to generate 32bit code).

To check whether your system can be used, first check that you have yasm (nasm can also be used, if yasm is unavailable, although some non-essential but convenient features will be missing) and byacc (version 1.9 should work). flex is ubiquitous, but ensure that you are using version 2.5.35 (other versions can also be made to work, YMMV). The recommended C++ compiler is GCC 4.5.0 (again, others may work). In the commands below, tar is GNU tar, make is GNU make.

The versions indicated in this page are just examples, but note that there may be problems, especially with byacc: it may be described as being of version 1.9 but may present different results in generated code. This may bring "surprises" when using the generated code in ways other that the plain C compilation classically associated with compiler building using Lex and YACC. Crucially, it affects our development, since we use C++.

LibRTS

To unpack, compile, and install LibRTS, do the following:

 tar xvfj librts-200903170001.tar.bz2 
 cd librts-200903170001
 make
 make install

If you are unlucky enough not to have GNU tar, try it this other way:

 bzcat librts-200903170001.tar.bz2 | tar xvf -
 cd librts-200903170001
 make
 make install

The last command will install the compiled version of librts (librts.a) in the libs directory, relative to ROOT (in the Makefile). By default, ROOT is defined as $HOME/compiladores/root.

LibCDK

Similarly, to unpack, compile, and install LibCDK, do the following:

 tar xvfj  libcdk4-200903152029.tar.bz2
 cd libcdk4-200903152029
 make
 make install

Compact

If the previous sections went well, then you should be ready to build the Compact compiler. All Makefiles share the same assumptions about ROOT, so if you make the same decisions regarding that variable in all cases, all should work.

Try it, just to be sure you can do it:

 tar xvfj compact-200903152029.tar.bz2
 cd compact-200903152029
 make 

Ok? Good.

Now that the trouble is behind, lets begin with a clean version: remove the old version and unpack Compact again:

 rm -rf compact-200903152029
 tar xvfj compact-200903152029.tar.bz2
 cd compact-200903152029

Now rename all files, so that the "Compact" part becomes the name corresponding to your compiler. For the sake of simplicity, we will call it "Potato".

If you have zsh:

 rename Compact Potato ./**/*(.)

Or, if you have the other rename (but still with zsh):

 rename 's/Compact/Potato/g' ./**/*(.)

If you have other shell variants (also good for zsh), try these:

 find . -type f -print -exec rename Compact Potato {} \;

Or:

 find . -type f -print -exec rename 's/Compact/Potato/g' {} \;

If all went well, all the "Compact" bits in the file names should now read "Potato".

Now that the outside is changed, lets concentrate on the inside. For this, we will use grep (GNU grep, if you must know) to find all occurrences of compact, Compact, and COMPACT, to change them, respectively, into potato, Potato, and POTATO. The idea is that all the factories and classes depending on the name will now depend on the new name.

We will start with uppercase names (you may wish to repeat each of these cycles, to ensure that, for whatever reason, you did not forget any occurrence):

 for i in `grep -rl COMPACT .`
 do
   sed -i 's/COMPACT/POTATO/g' $i
 done

Then:

 for i in `grep -rl Compact .`
 do
   sed -i 's/Compact/Potato/g' $i
 done

And, finally:

 for i in `grep -rl compact .`
 do
   sed -i 's/compact/potato/g' $i
 done

If you changed mknodedecls.pl with any of the above commands, it's almost certain it is no longer executable. Just do:

 chmod 755 mknodedecls.pl

That's all. If you did everything right, a simple make should give you your "potato" compiler, albeit still compiling the original language. The idea is to regard this as a particularly annoying bug and go from there in the direction of the language you want to implement. Step by step. Baby steps.

For now, just type make and see what happens:

 make

Start with some concept that already exists and shares some of the features you want to implement: for instance, you may want to start by adding a "for" cycle concept after noting that the original compiler already provides a "while" cycle. Just add the new "for" features wherever you find "while" features.

Good luck.