Skip to content
/ cplus Public

Compiler for an imperative toy programming language

License

Notifications You must be signed in to change notification settings

Sh3b0/cplus

Repository files navigation

made-with-C++ GitHub issues GitHub pull-requests License

Сplus

Compiler for C+ toy imperative language, based on LLVM.

Getting started

  • Code sample.cp

    routine main() is
        println "Hello C+";
        return;
    end
  • Compile

    $ ./cplus sample.cp
    
    Compilation successful. Run ./a.out to execute
  • Run

    $ ./a.out
    Hello C+
  • Generated ir.ll

    ; ModuleID = 'ir.ll'
    source_filename = "ir.ll"
    target triple = "x86_64-pc-linux-gnu"
    
    @fmt_s_ln = private unnamed_addr constant [4 x i8] c"%s\0A\00", align 1
    @str = private unnamed_addr constant [9 x i8] c"Hello C+\00", align 1
    
    define i64 @main() {
    entry:
      %printfCall = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @fmt_s_ln, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @str, i32 0, i32 0))
      ret i64 0
    }
    
    declare i64 @printf(i8*, ...)
  • Debug output from cplus -d sample.cp

    debug

Compilation from source (Linux)

  1. Install prerequisites: cmake flex bison clang llvm-10 using the package manager for your distro.

    • Example (Ubuntu 20.04 LTS): sudo apt install cmake flex bison clang llvm
  2. Clone repo

    git clone https://github.com/Sh3B0/cplus/
    cd cplus
  3. Build cplus binary using CMake

    mkdir build
    cmake -S . -B build
    cmake --build build
  4. Compile a source file *.cp (check tests)

    cd build
    ./cplus -d ../tests/1.cp
  5. Help

    $ ./cplus --help
    usage: cplus [options] infile
    	infile                 path to the source code file (*.cp) to compile.
    
    options:
    	-h, --help             show this help message and exit.
    	-d, --debug            show debug messages.
    	-o, --outfile outfile  specify executable file name.

Contribution

Feel free to report a bug or suggest a feature by creating an issue.

License

The source code is licensed under the MIT license, which you can find in the LICENSE file.