Horizon Lang is a custom programming language designed as a learning project to understand the fundamentals of compiler design, including lexing, parsing, semantic analysis, and code generation. The language supports high-level constructs, static typing, and a simplified syntax that allows for expressive programming. It is Turing complete, meaning it can theoretically solve any computational problem or build large-scale applications, demonstrating the power and versatility of its design. It can compile to both Python and C++.
This repository provides all the necessary components to build, extend, and experiment with a custom programming language.
- Static typing: Supports
int
,float
,string
,bool
,list
, andvoid
types. - Control structures: Includes
if
,else
,elseif
,while
, andfor
statements. - Functions: Define reusable functions with
fx
keyword. - Error handling: Supports
try
-catch
blocks for exception management. - Built-in I/O: Use
print
andinput
for standard input/output operations. - Custom operators: Logical (
and
,or
,not
), comparison (==
,!=
,<
,>
), and arithmetic (+
,-
,*
,/
,%
) operators. - List operations: Allows access, modification, and built-in methods for lists.
- Multiple backends: Compile to Python or C++ for flexibility and platform independence.
The full grammar for Horizon Lang can be found in the GRAMMARS.md file. It includes detailed syntax definitions and simple basic example codes for statements, expressions, and type systems.
Examples of Horizon Lang programs are available in the examples folder. These examples demonstrate:
- Variable declarations
- Control flow statements
- Function definitions and calls
- List and String manipulations
- Built-in functions and methods
- Error handling with
try
-catch
Example hello.hl
:
fx hello ( string name ) {
print("Hello, " + name + " !");
}
string name = input("Enter your name: ");
hello(name);
- C++ compiler: Ensure a modern C++ compiler (e.g., GCC, Clang) is installed.
- Python 3: Required for Python code compilation and execution.
- CMake: For building the C++ backend.
- Git: To clone the repository.
- Windows Subsystem for Linux (WSL): Recommended for running Unix-like commands and environments on Windows.
- Installation Guide: Install WSL
⚠️ Important:
Commands such ascmake ..
andmake
do not work in native Windows Command Prompt or PowerShell. To execute these commands on Windows, you must use a Unix-like terminal provided by WSL.
To ensure a smooth setup and build process on Windows, follow the detailed steps below:
WSL allows you to run a Linux environment directly on Windows, which is essential for executing Unix-like commands such as cmake
and make
. Follow these steps to install WSL:
-
Open PowerShell as Administrator:
- Press
Win + X
and select Windows PowerShell (Admin).
- Press
-
Enable WSL:
wsl --install
- This command installs the latest WSL version along with the default Linux distribution (usually Ubuntu).
-
Restart Your Computer:
- After installation, you may be prompted to restart your machine.
-
Set Up Your Linux Distribution:
- Upon restarting, launch the installed Linux distribution from the Start menu.
- Follow the on-screen instructions to create a Unix username and password.
Alternative Installation Guide: If you encounter issues, refer to the official WSL installation guide.
Once WSL is set up, you'll need to install the necessary development tools inside the Linux environment:
-
Update Package Lists:
sudo apt update
-
Install Git, CMake, and a C++ Compiler:
sudo apt install git cmake build-essential
-
Install Python 3:
sudo apt install python3
-
Verify Installations:
git --version cmake --version g++ --version python3 --version
Note for Windows Users:
If you are on Windows, ensure you are executing the following commands within the WSL terminal.
-
Clone the repository and navigate to its directory:
git clone https://github.com/itssodope01/HorizonLang.git cd HorizonLang
-
Build the project using
cmake
:mkdir build && cd build echo 'int main() {}' > output.cpp cmake .. make
Note:
Ensure you are in the WSL terminal if you are on Windows.
-
Create a
.hl
FileCreate a
.hl
file with your HorizonLang code, or use examples from theexamples
folder.Run this command from build directory to see all available example files:
ls ../examples # shows all examples
-
Run the Compiler
Run the compiler with your file as an argument (ensure you are in the
build
directory):./HorizonLang ../examples/Hello.hl
-
Choose Your Target Compiler
When prompted, select your target compiler:
1
for Python2
for C++
-
Execute the Generated Code
- Python: The compiler will transpile and run
output.py
. - C++: The compiler will transpile
output.cpp
, build it, and execute the resulting binary.
- Python: The compiler will transpile and run
-
Optional: View the Transpiled Files
After compilation, you can view the generated Python or C++ files in two ways:
-
Using the Terminal:
While in the
build
directory, use thecat
command to display the contents of the generated file:cat output.py # For Python
cat output.cpp # For C++
-
Using a Code Editor:
Navigate to the
build
directory of the project in your code editor (e.g., VS Code, Sublime Text) to browse and inspect the generatedoutput.py
oroutput.cpp
files visually.cd .. # Navigate to project root from build directory code . # opens the project in VS Code
-
-
Optional: View the Abstract Syntax Tree (AST) Structure
To see the AST structure of your HorizonLang program:
-
Uncomment AST Printing Lines:
Open
main.cpp
and uncomment lines 145 and 146 and save it:// Uncomment lines below to see AST Structure // std::cout << "\nAST structure:" << std::endl; // ASTPrinter::printAST(program);
-
Rebuild and Run:
Rebuild the project and run the compiler again to visualize the AST structure:
# make sure you are in the build directory make ./HorizonLang ../examples/Hello.hl
This will print the AST structure to the console, providing a detailed view of the parsed program.
-
Here’s an example of compiling and running a Horizon Lang program:
-
Create a program
Program.hl
in the examples directory:# Assuming you are still in the build directory nano ../examples/Program.hl
-
Write your program in the
nano
editor or paste the following example code:fx multiply(int a, int b) { int mul = a * b; return mul; } int num1 = INT(input("Enter First number: ")); int num2 = INT(input("Enter Second number: ")); int result = multiply(num1,num2); print("Multiplication result: " + STR(result));
-
Save the file To save the file in the
nano
editor:- Press
Ctrl + X
. - Press
Y
to confirm. - Press
Enter
to finalize.
- Press
-
Run the Compiler (make sure you are in the build directory):
./HorizonLang ../examples/Program.hl
-
Choose
1
to generate and run Python code. The output forProgram.hl
will be:Enter First number: 3 Enter Second number: 2 Multiplication result: 6
lexer/
: Handles lexical analysis, breaking the input into tokens.parser/
: Parses tokens into an Abstract Syntax Tree (AST).ast/
: AST structure and printing.semantic/
: Performs semantic analysis, ensuring type and scope correctness.codegen/
: Generates code for target backends (Python and C++).examples/
: Sample Horizon Lang programs.GRAMMARS.md
: Language grammar documentation.main.cpp
: Entry point for the compiler.
Contributions are welcome! If you’d like to add features, fix bugs, or improve documentation, please:
- Fork the repository.
- Create a feature branch.
- Submit a pull request.
This project was inspired by the desire to explore compiler design and the intricacies of building a custom language. It serves as a stepping stone for anyone interested in programming language theory or implementation.
This project is licensed under the MIT License. See the LICENSE file for details.