Compiler for Virtual Language

This is an actual compiler I created for a virtual language.

The virtual language, let’s call it PC (PasCal), resembles the C and Pascal programming languages in terms of commands, grammar and syntax.

The compiler (developed with the tools flex & byacc) translates programs written in PC to MIXAL, an assembly language for D. Knuth’s hypothetical computer, MIX (from his book: “The Art Of Computer Programming”).

MIX computer

MIX computer

The compiler successfully translates PC code to machine language that can then run on MIX simulators.

As an example, for the following PC code snippet that calculates and prints the first 10 numbers of the Fibonacci sequence:

{ 
  var first, second, i, tmp: int; 
  i=0;
  first=0; 
  second=1; 
  while (i<10) { 
   i=i+1; 
   tmp=first+second; 
   print tmp; 
   if (i==10) break; 
   first=second; 
   second=tmp; 
  } 
}

One could compile it using the command prompt:

Compiler < Fibonacci

Producing the corresponding machine instructions:

PRINTER EQU  18
        ORIG 2000
START   NOP
        ENTA 0
        STA 2
        ENTA 0
        STA 0
        JMP 2017
        ...

The generated machine code would then be given as input to a MIX computer simulator for assembling.

Assembling the instructions

Assembling the instructions

And finally, after the program is assembled, it would then run on the MIX simulator carrying out its initial task. In this case, print the 10 first numbers of the Fibonacci sequence.

MIX devices output

MIX devices output

Leave a Reply