Skip to content

ERPLAG compiler source tree developed as part of the course CS F363: Compiler Construction, at BITS Pilani.

License

Notifications You must be signed in to change notification settings

MayankJasoria/Compiler-Project

Repository files navigation

ERPLAG: a minimalist compiler

ERPLAG is a strongly typed, compiled language. It is minimalist because it currently does not encompass the full capabilities of most modern programming languages like character and string handling or defining structs/classes.

Prerequisites

  • gcc >= 7.0
  • nasm >= 2.14.02

How to build

The code has been tested in Ubuntu >= 16.04, and the README has been prepared accordingly. Users of other OSes will need to figure out the equivalent commands or supporting software wherever applicable.

  1. Clone the repository
    $ git clone https://github.com/MayankJasoria/Compiler-Project.git

  2. Build

    $ cd Compiler-Project  
    $ make
    

    This should create a binary file named 'compiler' in the same directory.

  3. Execute
    Run the ERPLAG compiler in the following manner:

    $ ./compiler <source-code> <assembly-file>.asm
    $ nasm -f elf64 <assembly-file>.asm
    $ gcc -no-pie <assembly-file>.o -o <output>.out
    $ ./<output>.out
    

Language Features

ERPLAG supports the following features:

  • Primitive datatypes - integers, floating points
  • Non-primitive - arrays, boolean
  • Basic arithmetic operations - add, subtract, multiply, divide
  • Statements:
    • Comments
      Examples:

       ** single line comment **
      
       **
       Multiline comment
       **
      
    • Declarative
      Examples:
      Declaring variables:

       declare index:integer;
       declare t:boolean;
       declare s:real;
      

      Declaring a module:

       declare module readArr;
      
    • Assignment
      Examples:

       sum := sum + 4; ** Integer assignment **
       pi := 3.14; ** Floating point assignment **
       bool2 := a < b ** Boolean assignment **
       arr2 := arr1; ** Assigning one array into another variable **
      
    • Input/output
      Examples:
      Input:

       get_value(var);
      

      Output

       print(var);
      
    • Conditional
      Examples:

       switch(c) ** switch case on boolean **
       start
       	case true:
       		** statements **;
       		break;
       	case false: 
       		** statements **;
       		break;
       end
      
       switch(int1) ** switch case on integer **
       start
       	case 1:
       		** statements **
       		break;
       	case 2:
       		** statements **
       	case 3:
       		** statements **
       		break;
       	default:
       		** statements **
       end
      
    • Iterative
      Examples: While loop

       while(a >= b)
       start
       	** statements **
       end
      

      For loop

       for(idx1 in 1..10)
       start
       	** statements **
       end
      
    • Module declaration and call
      Examples:

      Driver (main) module
      Note: Driver module is the entry-point of an ERPLAG program.

       <<<driver program>>>
       start
       	** statements **
       end
      

      Module declaration

       declare module m1; ** allows module definition to be given after 'driver' **
      
       <<module arraySum>> ** Declaration and definition may be combined this way, if it appears in the code before driver module **
       takes input[list:array[1..15] of real, n:integer];
       returns  [sum:real]; 
       start
       	** statements **
       end
       
       <<module printReal>>
       takes input[n:real];
       start
       	print(n);
       end
      

      Module call

       [sum]:= use module arraySum with parameters A,k;
       use module printReal with parameters pi;
      

A detailed language specification can be found here.

Contributing

This project is no longer under active development. Any issues opened on this repository are likely to be ignored. However, well worked out pull requests are entertained.