Skip to content

Repository files navigation

P

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.

Features

  • C-like Syntax: Familiar syntax including if/else, for loops, 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.
  • Advanced Language Constructs: Supports function pointers, dynamic dispatch, and namespaces for code organization.

Language Sneak Peek

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.

Building from Source

You'll need a C++17 compatible compiler (like GCC or Clang), cmake (version 3.20 or newer), as, and ld.

  1. Clone the repository:
    git clone https://github.com/Pastoray/P.git
    cd P
  2. Configure and build the project with CMake:
    cmake -S . -B build
    cmake --build build
    The compiler executable will be at build/P and the static library at build/libp/libp.a.

Usage

To compile and run a P source file:

  1. Generate Assembly: Use the P compiler to generate an assembly file (.s).
    ./build/P your_source_file.p -o output.s
  2. Assemble and Link: Use an assembler (as) and linker (ld) to create an executable, linking against the P runtime library (libp.a).
    as -g -o output.o output.s
    ld -g -o my_program output.o ./build/libp/libp.a
  3. Run your program:
    ./my_program

Running Tests

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

Project Structure

.
├── 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

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A statically‑typed, general‑purpose programming language with C‑like syntax. This repository contains the x86‑64 compiler written in C++17, featuring a full frontend‑to‑backend pipeline and a custom runtime library.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages