Hand Compiling Code


The goal is to understand how a compiler works.  The method will be to translate normal C++ into a minimal set of C, known as Baby-C.

Note:  Sometimes it's a pain to declare a pointer to a function.  You can read about it at http://gethelp.devx.com/techtips/cpp_pro/10min/10min0300.asp

Allowed operations
Math Operators
+, -, *, /, %, ()
Pointer Operators
*, &
Variable declarations
int a, float b
Only int, float, char, double, short and long
Signed or unsigned
NO COMPLEX TYPES
Memory allocation
a = new char(10);
delete a;
b = malloc(10);
free(b);

YOU CAN ONLY ALLOCATE ARRAYS OF CHARS
Casting
You can cast to any simple type or simple pointer type you want.
Whatever you need to make the compiler happy.
NO COMPLEX TYPES.
Control structures
if (a == 10) goto foo;
Only if who's body is a goto.
No Elses.
No while, do, or for loops.
I/O
You may use cout and cin.
Functions
Yes.  You may make as many as you need.
Boolean Arithmatic
All normal operators
<, >, ==, !=, <=, >=, &&, ||, !, |, &




C++ Code
Baby-C Code
int main() {
   int a = 3;
   while (a < 10) {
      cout << a;
      a++;
   }
}
int main() {
    int a = 3;
  loop:
    if (a >= 10) goto exit;
    cout << a;
    a++;
    goto loop;
 exit:
}

C++ Code
Baby-C Code
class Person {
    int age;
    int size;
}

int main() {
    Person P;
    p.age = 300;
    p.size = 10;
    cout << p.age << p.size;
}





int main() {
   // Person P
   char *p = new char(8);
   // p.age = 300;
   *(int *)(p+0) = 300;
   // p.size = 10;
   *(p+4) = 10;
   // cout
   cout << *(int *)(p+0) << *(int *)(p+4)) ;
}


C++ Code
Baby-C Code
class Person {
   int age;
   int size;
   void Print() {
      cout << age << size;
   }
}

int main() {
    Person P;
    p.age = 3;
    p.size = 10;
    p.Print();
}
void Person_Print_void(char *self) {
    cout << *(int *)(self+0) << *(int *)(self+4);
}

int main() {
   // Person P
   char *p = new char(8);
   // p.age = 300;
   *(int *)(p+0) = 300;
   // p.size = 10;
   *(p+4) = 10;
   // p.Print()
   Person_Print_void(p);
}

C++ Code
Baby-C Code
class Person {
   int age;
   int size;
   virtual void Print() {
      cout << age << size;
   }
}

int main() {
    Person P;
    p.age = 3;
    p.size = 10;
    p.Print();
}
void Person_Print_void(char *self) {
    cout << *(int *)(self+0) << *(int *)(self+4);
}

// declare a typedef for a function pointer
typedef void (*fp) (char *);

int main() {

   // Make Virtual Function Table for Person
   char *vft_Person = new char(4);
   *(int *)vft_Person = (int)Person_Print_void;

   // Person P
   char *p = new char(12);
   *(int *)(p+8) = (int)vft_Person;
   // p.age = 300;
   *(int *)(p+0) = 300;
   // p.size = 10;
   *(p+4) = 10;
   // p.Print()
   int *pointer_to_objects_vft_pointer = (int *)(p+8);
   int *vft_pointer = (int *)(*pointer_to_objects_vft_pointer);
   fp function = (fp)(*(vft_pointer+0));
   (function)(p);
}


Things For You To Compile

main()
{
     int a, b, c;
     a = 10;
     b = a-3;
     if (b == 7)
       cout << a;
     else
       cout << b;
}



main() {
    int array[100];
    for(int i = 0; i < 100; i++)
        array[i] = i;
    int sum = 0;
    for(int i = 0; i < 100; i++)
        sum = sum + array[i];
    cout << sum;
}

class Plane {
    float wingspan;
    int engines;
}
void main() {
   Plane p;
   p.wingspan = 35;
   p.engines = 2;
   cout << p.wingspan << p.engines;

class Plane {
    float wingspan;
    int engines;
    void clear() { wingspan = 0; engines=0; }
}
void main() {
   Plane p;
   p.wingspan = 350;
   p.engines = 2;
   p.clear();
   cout << p.wingspan << p.engines;
}
class Plane {
    float wingspan;
    int engines;
    virtual void clear() { wingspan = 0; engines=0; }
    virtual void killEngine() { engines = 0; }
}
void main() {
   Plane p;
   p.wingspan = 350;
   p.engines = 2;
   p.killEngine();
   cout << p.wingspan << p.engines;
}