Skip to content

Simple compiler for a subset of the C programming language. Project for CS 4550.

Notifications You must be signed in to change notification settings

human37/simple-compiler

Repository files navigation

simple-compiler

A simple compiler written in C++. Can be configured to interpret to C++, or compile source code to machine code and execute. Supports a subset of the C programming language.

Features

  • integers
  • cout statements
  • endl for a newline character
  • line and block comments
  • basic comparision operators (>, <, <=, >=, ==)
  • logical && and || operators
  • bitwise & operator
  • nested statements with PEMDAS (4 + (4 * 2) / (1 & 1))
  • += and -=

Here is an example of some source code it currently supports:

void main()
{
    // integers and math
    int sum;
    sum = (35 + 1) / (36 * 1) * (1 & 1);
    cout << sum << endl;

    // cout statements
    cout << endl << 0 << sum << 0 << endl;

    // plus equals & minus equals
    int test;
    test = 0;
    cout << test << endl;
    test += 99;
    cout << test << endl;
    test -= 99;
    cout << test << endl;

    // while loops
    int max;
    max = 10;
    while (max >= 0)
    {
        cout << max << endl;
        max -= 1;
    }
}

Running

Your input file must be named INPUT.c and be in the projects root directory. You can then run

$ make interpret

in order to interpret the source code to C++, and

$ make compile

in order to compile the source code into machine code and execute. (currently this is only available with g++-9 on <= ubuntu 16)

About

Simple compiler for a subset of the C programming language. Project for CS 4550.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages