Tisp is a lightweight compiler for a lisp-like programming language that compiles to LLVM IR, then generates native code through the LLVM toolchain. The shortcut for a tisp file is .tsp, like teaspoon (so pround of this name). The entire "codebase" is one file, and a little bit under 300 lines (excluding newlines and comments).
- Type system: Supports integers and floating-point numbers with automatic type promotion
- Functions: Define and call functions (with recursion support)
- Control flow:
ifexpressions for conditional branchingcondfor multi-way conditionalsloopfor iteration
- Arithmetic operations:
+,-,*,/ - Comparisons:
<,>,= - Variables: Local variable definitions with
define - LLVM backend: Compiles to LLVM IR, then to native code
- C++ compiler with filesystem support
- LLVM toolchain (specifically
llc) - GCC or Clang for linking
Compile the compiler itself:
g++ -o tisp src/tisp.cppCompile a .tsp file to an executable:
tisp program.tspThis will generate an executable with the same name as the input file (without the .tsp extension).
Usage: tisp <input.tsp> [options]
Options:
-o <output> Specify output executable name
--emit-ir Emit LLVM IR only (.ll)
--emit-asm Emit assembly only (.s)
--emit-obj Emit object file only (.o)
--verbose Preserve all intermediate files
--help Show this help message
--version Show version information
- Lexing & Parsing: The compiler tokenizes and parses the source code into an abstract syntax tree
- IR Generation: Generates LLVM Intermediate Representation (IR)
- Optimization: Uses
llc -O2to optimize and convert IR to assembly - Linking: Links the assembly code with GCC or Clang to produce a native executable
This project is provided as-is for all purposes, and was really just for my own fun - it's pretty bad code, and really not practical, but if you want to use it, it's yours. Feel free to use, modify, and distribute as you wish.
- The LLVM Project - The real backbone of all these little fun projects I do.
- The Dragon Book - For foundational compiler theory, and lots on lexing and parsing.
- Wisp by Adam McDaniel - A minimal Lisp-like language built with LLVM.
- LLVM IR with the C++ API - Tutorial by Mukul Rathi
- “Hello, LLVM” - Tutorial by James Hamilton
- Awesome LLVM - A curated list of useful LLVM resources.
- Jesse Squires' Blog - A great roundup of compiler and LLVM learning materials.
- Building an Optimizing Compiler - Old, but still very good, helpful with high level design.