Horizon is a statically-typed, compiled programming language built from the ground up in C++23. This repository contains the source code for the Horizon compiler, featuring a complete lexical and syntactical analysis pipeline that generates an Abstract Syntax Tree (AST).
The project is designed with a strong emphasis on performance, leveraging modern C++ features and custom-built, heap-allocated data structures to ensure efficiency.
- Custom Data Structures: Includes highly optimized, from-scratch implementations of
string,vector,hashtable,sptr(smart pointer), andpairto maximize performance and control memory management. - Modern Language Syntax: A clean, C-like syntax with modern features. See the example below.
- Compiler Pipeline: A classic compiler frontend architecture:
- Lexer: Tokenizes the source code.
- Parser: Performs syntactic analysis and builds an Abstract Syntax Tree (AST).
- Detailed Error Reporting: The compiler provides colorized, easy-to-read error messages, pinpointing the exact location of syntax and lexical errors.
To get a local copy up and running, follow these simple steps.
You will need a modern C++ compiler that supports the C++23 standard.
- Linux/macOS:
g++ - Windows:
cl.exe(Visual Studio)
You will also need one of the following build systems:
- CMake
make
The project can be built using either make or CMake.
The Makefile provides several targets for convenience.
- Build both release and debug versions:
make all
- Build only the release version:
make release
- Build only the debug version (not available on Windows):
make debug
- Clean build artifacts:
make clean
-
Configure CMake:
cmake -B build -DCMAKE_BUILD_TYPE=Release
-
Build the project:
cmake --build build --config Release
After building, you can run the compiler on a Horizon source file (.hr). A sample file is provided in the sample/ directory.
# Using the debug build (recommended for development)
./bin/horizon_debug ./sample/main.hr
# Or using the release build
./bin/horizon ./sample/main.hrThe compiler will currently lex and parse the code, printing the generated AST to the console along with performance metrics for each stage.
Here is an example of the Horizon language syntax from sample/main.hr:
func add(int32: a, int32: $2231): int32 {
return a + b;
}
func add_template<T>(T: a, T: a): T {
return a + b;
}
func main(): int32 {
int32: i = 13, j = 45;
i += j;
i /= 3;
j = add(-1, -5);
dec32: eval = ((((2 + 3) * (6 - 4) + (8 / 2)) / (3 ^ 2) - 5) * (((7 - 1) / 2) + (4 ^ 2))) + ((12 - 6) * (3 + 1) - (9 / 3)) / (5 ^ 2) + (((3 * 7) + (15 / 5)) * (4 - 2) + 8) / (4 ^ 3);
bool: is_true = false;
if(is_true) {
i = 100;
}
else {
i = 200;
}
for(int32: K = 0; K < i; K++)
{
i -= K;
}
let: is_horizon = true, my_name = "Tushar", ret = 0;
return ret;
}
├── deps/ # Custom data structure implementations
│ ├── string/
│ ├── vector/
│ └── ...
├── src/ # Main compiler source code
│ ├── lexer/ # Lexical analyzer
│ ├── parser/ # Syntax analyzer and AST
│ ├── errors/ # Error handling and reporting
│ └── entry/ # Main entry point
├── sample/ # Example Horizon (.hr) code
├── .github/ # CI workflows
├── CMakeLists.txt # CMake build script
├── makefile # Makefile for GNU Make
└── LICENSE # Project license
Distributed under the GNU General Public License v3.0. See LICENSE for more information.