Skip to content

jdao55/toy-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 

Repository files navigation

toy-compiler

A toy (Kaleidoscope) compiler based on llvm's guide https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html, currently compiles to object files, which can be linked using clang/gcc

Building

Dependencies

  • cmake
  • C++ 20 compiler
  • conan
  • Flex
  • llvm

Build using Cmake

  1. Clone dirrectory
git clone https://github.com/jdao55/toy-compiler && cd toy-compiler
  1. Create build directory
mkdir build && cd build
  1. Run cmake
cmake ../
  1. Build using make
make

Usage

Usage:
  toycomp <filename> [--out=filename] [--opt=level]
  toycomp (-h | --help)

Options:
  -h --help                       Show this screen.
  -o filname --out=filename       Specify output object file name
  -O level --opt=level            Specify optimization level [1,2,3])";

Example

Kaleidoscope program test.toy

def add(x y)
   x + y

def add3(x y z)
   x + y + z

Compile with toycompiler toycompiler test.toy -out=add.o

Calling Kaleidoscope from c++

sample c++ program example.cpp

#include <iostream>
extern "C" {
  double add(double, double);
  double add3(double, double, double);
}
int main()

{
  std::cout << add(1.2, 2.3) << std::endl;
  std::cout << add3(1.0, 2.0, 3.0) << std::endl;
}

Compile and link using gcc/clang g++ example.cpp add.o -o example

TODO

Language features

Compiler features

About

toy compiler implemented using c++ and llvm

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published