P is a statically-typed, general-purpose programming language with a C-like syntax. This repository contains the source code for the P compiler, which translates P source code into x86-64 assembly. The compiler is written in C++17 and includes a custom standard library for core functionalities.
- C-like Syntax: Familiar syntax including
if/else,forloops,structs,unions,enums, and namespaces. - Static Typing: Strong, static type system with support for primitive types (
i32,f64, etc.), pointers, arrays, and user-defined types. - Modern Compiler Pipeline: The compilation process involves:
- Tokenizer: Converts source code into a stream of tokens.
- Parser: Builds an Abstract Syntax Tree (AST) from the tokens.
- Semantic Analyzer (Sema): Performs type checking and other semantic validations.
- Intermediate Representation (IR): Generates a low-level, platform-independent IR.
- Code Generator: Translates the IR into x86-64 assembly (AT&T syntax).
- Custom Runtime (
libp): A minimal, freestanding standard library providing:- Memory management (
malloc,free). - Basic I/O (
printi32,printstr, etc.). - System call wrappers.
- Threading support (
spawn_thread,mutex_lock). - Signal handling.
- Memory management (
- Advanced Language Constructs: Supports function pointers, dynamic dispatch, and namespaces for code organization.
Here's a small example of what P code looks like (from examples/simple.p):
i32 i = 0;
i32 hello = 16 + 4 * i + 1 / 3;
fn printi32(i32 x) -> ;
fn main() ->
{
i + 1 - 2;
i += 2;
if (i + 1 - 1)
{
i32 big = 69;
}
else
{
i32 small = 67;
printi32(small);
}
printi32(i);
return 0;
}For more details, see the language specification in the spec/ directory.
You'll need a C++17 compatible compiler (like GCC or Clang), cmake (version 3.20 or newer), as, and ld.
- Clone the repository:
git clone https://github.com/Pastoray/P.git cd P - Configure and build the project with CMake:
The compiler executable will be at
cmake -S . -B build cmake --build buildbuild/Pand the static library atbuild/libp/libp.a.
To compile and run a P source file:
- Generate Assembly: Use the
Pcompiler to generate an assembly file (.s)../build/P your_source_file.p -o output.s
- Assemble and Link: Use an assembler (
as) and linker (ld) to create an executable, linking against thePruntime library (libp.a).as -g -o output.o output.s ld -g -o my_program output.o ./build/libp/libp.a
- Run your program:
./my_program
The repository includes a suite of unit and end-to-end tests. After building the project, you can run them using ctest.
cd build
ctest --output-on-failure --verbose.
├── src/ # Compiler frontend/backend (Tokenizer, Parser, Sema, IR, CodeGen)
├── include/P/ # Header files for the compiler
├── libp/ # Custom standard library/runtime (C and ASM)
├── examples/ # Example P programs
├── tests/ # Unit and end-to-end tests
│ ├── e2e/ # End-to-end tests for language features
│ └── diagnostics/ # Tests for compiler error reporting
├── spec/ # Language and syntax specifications
├── bench/ # Compiler performance benchmarks
└── CMakeLists.txt # Main CMake build script
This project is licensed under the MIT License. See the LICENSE file for details.