MyCompiler is a simple programming language with its own syntax and parser. This project includes tokenization, parsing, and evaluation of expressions based on a well-defined grammar. The compiler supports variable assignments, conditional statements, and arithmetic operations. Additionally, the compiler also generates assembly code.
- Variable declaration and assignment (
let ident = Expr;
) - Arithmetic operations (
+
,-
,*
,/
) - Conditional statements (
if
,elif
,else
) - Scope handling with
{}
blocks - Program termination with
exit(Expr);
- Multiple line comments ( /* comment */ )
- Single line comments ( // comment )
The language follows this formal grammar:
Prog -> Stmt*
Stmt -> exit(Expr);
| let ident = Expr;
| ident = Expr;
| if (Expr) Scope IfPred
| Scope
Scope -> { Stmt* }
IfPred -> elif (Expr) Scope IfPred | else Scope | ε
Expr -> Term | BinExpr
BinExpr -> Expr * Expr (prec = 1)
| Expr / Expr (prec = 1)
| Expr + Expr (prec = 0)
| Expr - Expr (prec = 0)
Term -> int_lit | ident | (Expr)
📂 MyCompiler
├── 📄 tokenization.hpp # Tokenizer definitions
├── 📄 parser.hpp # Parser implementation
├── 📄 main # Entry point for execution
├── 📄 tokenization # Tokenization logic
├── 📄 generation # Generating Assembly code
git clone https://github.com/yourusername/MyCompiler.git
cd MyCompiler
make # If a Makefile is provided
To compile and run a source file:
./MyCompiler <source_file>
To compile and run your Hydro Compiler project, follow these steps:
Ensure you have the following installed on your system:
- CMake (version 3.10 or higher)
- GCC or Clang (C++ compiler)
- NASM (Netwide Assembler)
- LD (GNU Linker)
Organize the build files by creating a separate directory:
mkdir build
Run the following command to generate the necessary build files:
cmake -S . -B build/
Build the project using CMake:
cmake --build build/
Create a file named test.hy
in the root directory of your project and add the following:
let y = (10 - 2 * 3) / 2;
let x = 3; // first
// first
if (1) {
x = 1;
} elif (0) {
x = 2;
} else {
x = 3;
}
exit(x);
/*
exit(4);
*/
Feel free to contribute by submitting issues or pull requests!