A PHP subset compiler written in Rust (oxidizing elephant). This project compiles a subset of PHP to x86-64 assembly, which is then compiled to an executable using GCC.
Oxiphant is a compiler that translates PHP code into executable binaries through a multi-stage compilation process. The compiler supports a subset of PHP features including variables, basic arithmetic operations, control flow statements, and simple I/O.
The following diagram illustrates the data flow through the Oxiphant compiler system:
flowchart TD
A[PHP Source Code] --> B[Lexer]
B --> C[Tokens]
C --> D[Parser]
D --> E[Abstract Syntax Tree]
E --> F[Type Checker]
F --> G[Validated AST]
G --> H[Code Generator]
H --> I[Bytecode Instructions]
I --> J[Assembly Generator]
J --> K[Assembly Code]
K --> L[GCC Compiler]
L --> M[Executable Binary]
subgraph "Lexical Analysis"
B
C
end
subgraph "Syntax Analysis"
D
E
end
subgraph "Semantic Analysis"
F
G
end
subgraph "Code Generation"
H
I
J
K
end
subgraph "Final Compilation"
L
M
end
-
Lexical Analysis: The source code is tokenized by the lexer, which converts the raw text into a sequence of tokens.
-
Syntax Analysis: The parser converts the token stream into an Abstract Syntax Tree (AST), which represents the hierarchical structure of the program.
-
Semantic Analysis: The type checker validates the AST, ensuring that operations are type-safe and semantically correct.
-
Code Generation:
- The code generator converts the AST into bytecode instructions.
- The assembly generator translates these instructions into x86-64 assembly code.
-
Final Compilation: GCC compiles the assembly code into an executable binary.
-
src/: Source code for the compilerlexer.rs: Tokenizes PHP source codeparser.rs: Parses tokens into an ASTtypechecker.rs: Validates the ASTcodegen.rs: Generates bytecode instructionsasmgen.rs: Generates assembly codeerror.rs: Error handling utilitiesast.rs: AST data structureslib.rs: Main library interfacemain.rs: Command-line interface
-
tests/: Test scripts and utilitiesscripts/: PHP test scriptsoutput/: Compiled executables and assembly files
cargo run --bin oxiphant <input.php> [output.exe]Or use the provided PowerShell script:
.\compile_test.ps1 <script_name>./<output.exe>Or use the provided PowerShell script:
.\run_test.ps1 <script_name>The tests/ directory contains test scripts and utilities for testing the compiler. See tests/README.md for more information.
This project is licensed under the MIT License - see the LICENSE file for details.
The name "Oxiphant" is a fusion of two key elements that define this project:
-
Oxi from "oxidizing" - a nod to Rust, the programming language used to build this compiler. Rust is often associated with the term "oxidizing" (as opposed to "corroding"), symbolizing its focus on safety and performance.
-
phant from "elephant" - representing PHP, whose logo is the well-known blue elephant.
