An open API service indexing awesome lists of open source software.

https://github.com/tinybiggames/cpascal

๐Ÿš€ CPascal is a systems programming language that combines Pascal's clean, readable syntax with C's semantic model and full ABI compatibility. โšก Compiles to LLVM with zero runtime overhead, enabling seamless interoperation with any C library without bindings. ๐ŸŽฎ Ideal for game development, ๐Ÿ”ง device drivers, and modernizing C codebases. ๐Ÿ’Ž
https://github.com/tinybiggames/cpascal

abi-compatibility better-c c compiler delphi embedded game-development llvm native-code pascal performance programming-language systems-programming win64 windows-11 zero-overhead

Last synced: 6 months ago
JSON representation

๐Ÿš€ CPascal is a systems programming language that combines Pascal's clean, readable syntax with C's semantic model and full ABI compatibility. โšก Compiles to LLVM with zero runtime overhead, enabling seamless interoperation with any C library without bindings. ๐ŸŽฎ Ideal for game development, ๐Ÿ”ง device drivers, and modernizing C codebases. ๐Ÿ’Ž

Awesome Lists containing this project

README

          

![CPascal](media/cpascal.jpg)
[![License: BSD 3-Clause](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![Language](https://img.shields.io/badge/Language-CPascal-blue.svg)]()
[![LLVM](https://img.shields.io/badge/Target-LLVM-orange.svg)]()
[![C ABI](https://img.shields.io/badge/C%20ABI-Compatible-green.svg)]()

> ๐Ÿšง **CPascal is a Work in Progress**
>
> CPascal is currently under active development and evolving quickly. Some features described in this documentation may be incomplete, experimental, or subject to significant changes as the project matures.
>
> We welcome your feedback, ideas, and issue reports โ€” your input will directly influence the direction and quality of CPascal as we strive to build the ultimate modern Pascal development platform.

# CPascal
### **A systems programming language with Pascal syntax and C semantics**

CPascal combines the readable syntax of Pascal with the semantic model and binary compatibility of C. It compiles to LLVM IR and maintains **full C ABI compatibility**, enabling seamless interoperation with existing C libraries and codebases without translation layers or bindings.

## ๐ŸŽฏ Design Philosophy

> **"Better C with Pascal Syntax"**

CPascal is designed as a modernized systems programming language where every construct maps directly to equivalent C constructs at the binary level. This ensures zero-overhead abstraction and complete compatibility while providing a cleaner, more structured syntax.

## โœจ Key Features

### ๐Ÿ”— **Full C ABI Compatibility**
- All data types, function calls, and memory layouts identical to C
- Direct linking with any C library without bindings
- Binary-compatible function pointers and structures
- Identical memory layout and alignment rules

### ๐Ÿ“ **Clean Pascal Syntax**
- Structured programming constructs (`begin`/`end`, `if`/`then`/`else`)
- Strong typing with type safety
- Clear procedure/function distinction
- Readable control flow statements

### โšก **Zero Runtime Overhead**
- Direct compilation to efficient machine code via LLVM
- No garbage collection or automatic memory management
- No runtime type information (RTTI)
- Minimal runtime requirements

### ๐ŸŒŸ **Modern Language Features**
- **Multiple Assignment**: `A, B := GetCoordinates()`
- **Ternary Operator**: `Max := (A > B) ? A : B`
- **Compound Assignment**: `Value += 10`, `Flags or= $80`
- **Inline Assembly**: Extended assembly with input/output constraints
- **Variadic Functions**: Full C-style variadic function support
- **Module System**: Clean imports without preprocessor complexity

### ๐ŸŽ›๏ธ **Advanced Systems Programming**
- Inline assembly with GCC-compatible extended syntax
- Atomic operations and memory barriers
- Direct hardware access and register manipulation
- Platform-specific optimizations

## ๐Ÿš€ Quick Start

### Hello World

```cpascal
program HelloWorld;

function printf(const AFormat: PChar, ...): Int32; external;

begin
printf("Hello, World!\n");
end.
```

### Graphics Application

```cpascal
program GraphicsDemo;

import Graphics, Math;
{$LIBPATH /usr/local/lib}
{$LINK SDL2}
{$LINK OpenGL32}

// External SDL2 functions
function SDL_Init(const AFlags: UInt32): Int32; external "SDL2";
function SDL_CreateWindow(const ATitle: PChar; const AX, AY, AW, AH, AFlags: Int32): Pointer; external "SDL2";

const
SDL_INIT_VIDEO = $00000020;
SDL_WINDOW_OPENGL = $00000002;

var
Window: Pointer;
Running: Boolean;

begin
if SDL_Init(SDL_INIT_VIDEO) <> 0 then
exit;

Window := SDL_CreateWindow("CPascal Demo", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
if Window = nil then
exit;

Running := true;
while Running do
begin
// Game loop
ProcessEvents();
UpdateGame();
RenderFrame();
end;

SDL_DestroyWindow(Window);
SDL_Quit();
end.
```

### Advanced Features

```cpascal
module DataStructures;

// Multiple assignment
function GetCoordinates(): (Int32, Int32);
begin
Result := (100, 200);
end;

// Ternary operator and compound assignment
function ProcessData(const AValues: ^Int32; const ACount: Int32): Int32;
var
LIndex, LSum, LMax: Int32;
begin
LSum := 0;
LMax := 0;

for LIndex := 0 to ACount - 1 do
begin
LSum += AValues[LIndex]; // Compound assignment
LMax := (AValues[LIndex] > LMax) ? AValues[LIndex] : LMax; // Ternary
end;

Result := LSum;
end;

// Inline assembly with constraints
function AtomicIncrement(const APtr: ^Int32): Int32; inline;
begin
asm
"lock incl %1"
"movl %1, %0"
: "=r" (Result) // Output: register
: "m" (APtr^) // Input: memory location
: "memory" // Clobbered: memory
end;
end;

// Union types
type
TValue = union
AsInt: Int32;
AsFloat: Single;
AsBytes: array[0..3] of UInt8;
end;

var
LValue: TValue;
begin
LValue.AsInt := $3F800000; // IEEE 754 for 1.0
// LValue.AsFloat is now 1.0
end;
```

## ๐Ÿ“Š C ABI Type Mapping

| CPascal Type | C Equivalent | Size | Description |
|--------------|--------------|------|-------------|
| `Int8` | `int8_t` | 1 | Signed 8-bit integer |
| `UInt8` | `uint8_t` | 1 | Unsigned 8-bit integer |
| `Int32` | `int32_t` | 4 | Signed 32-bit integer |
| `UInt32` | `uint32_t` | 4 | Unsigned 32-bit integer |
| `Int64` | `int64_t` | 8 | Signed 64-bit integer |
| `Single` | `float` | 4 | IEEE 754 single precision |
| `Double` | `double` | 8 | IEEE 754 double precision |
| `Boolean` | `bool` | 1 | C99 boolean |
| `Pointer` | `void*` | 8/4 | Platform pointer |
| `PChar` | `char*` | 8/4 | Null-terminated string |

**All record types map to C structs with identical layout and alignment.**

## ๐Ÿ—๏ธ Project Structure

```
project/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ main.cpas # Program entry point
โ”‚ โ”œโ”€โ”€ graphics.cpas # Graphics module
โ”‚ โ”œโ”€โ”€ audio.cpas # Audio module
โ”‚ โ””โ”€โ”€ platform/
โ”‚ โ”œโ”€โ”€ windows.cpas # Platform-specific code
โ”‚ โ”œโ”€โ”€ linux.cpas
โ”‚ โ””โ”€โ”€ macos.cpas
โ”œโ”€โ”€ lib/
โ”‚ โ”œโ”€โ”€ SDL2.lib # External libraries
โ”‚ โ””โ”€โ”€ OpenGL32.lib
โ””โ”€โ”€ build/
โ”œโ”€โ”€ obj/ # Compiled objects
โ””โ”€โ”€ bin/ # Final executables
```

## ๐ŸŽฏ Target Applications

- **Systems Programming**: Device drivers, OS components
- **Game Development**: High-performance game engines
- **Embedded Systems**: Microcontroller programming
- **Performance-Critical Applications**: Real-time systems
- **C Codebase Modernization**: Gradual migration from C

## ๐Ÿ“– Language Features

### Module System

```cpascal
module Graphics;

import Math, Utilities;
{$LIBPATH /usr/local/lib}
{$LINK SDL2}
{$LINK OpenGL32}

public function InitializeGraphics(): Boolean;
private function InternalHelper(): Int32;
```

### Calling Conventions

```cpascal
function StandardFunc(const A: Int32): Int32; // Default (C)
function CdeclFunc(const A: Int32): Int32; cdecl; // C calling convention
function StdcallFunc(const A: Int32): Int32; stdcall; // Windows API
function InlineFunc(const A: Int32): Int32; inline; // Force inline
function FastFunc(const A: Int32): Int32; fastcall; inline; // Combined
```

### Memory Management

```cpascal
var
LBuffer: ^UInt8;
LSize: NativeUInt;

begin
LSize := 1024;
LBuffer := GetMem(LSize); // Allocate

if LBuffer <> nil then
begin
ProcessData(LBuffer, LSize);
FreeMem(LBuffer); // Free
end;
end;
```

### External Library Integration

```cpascal
// External functions
function malloc(const ASize: NativeUInt): Pointer; external;
function SDL_Init(const AFlags: UInt32): Int32; external "SDL2";
function GetTickCount(): UInt32; stdcall; external "kernel32";

// Variadic functions
function printf(const AFormat: PChar, ...): Int32; external;
```

## ๐Ÿ”ง Compiler Features

### Conditional Compilation

```cpascal
{$IFDEF DEBUG}
{$DEFINE ENABLE_LOGGING}
{$ENDIF}

{$IFDEF WINDOWS}
link user32, kernel32;
{$ELSE}
link X11, pthread;
{$ENDIF}
```

### Build Configuration

```cpascal
{$LINK SDL2}
{$LIBPATH /usr/local/lib}
{$APPTYPE CONSOLE}
{$EXEPATH bin/release}
```

## ๐Ÿ“š Documentation

- **[Language Specification](docs/CPASCAL-SPECIFICATION.md)** - Complete language reference
- **[BNF Grammar](docs/CPASCAL-BNF.md)** - Formal grammar specification
- **[Coverage Examples](docs/CPASCAL-COVERAGE.cpas)** - Comprehensive syntax examples
- **[C Migration Guide](docs/C-MIGRATION.md)** - Migrating from C to CPascal
- **[API Reference](docs/CPASCAL-APIREFERENCE.md)** - Standard library documentation

### ๐Ÿ“‹ **Coding Standards**
- **Naming**: `L` prefix for locals, `A` prefix for parameters
- **Declarations**: Each variable on separate line, no inline `var`
- **Parameters**: Always `const` unless `var`/`out` needed
- **Testing**: Comprehensive tests for all new functionality
- **Documentation**: Update relevant guides and inline docs

### ๐Ÿ† **Contributors**



*Join our growing community of developers building the future of Pascal development!*

## ๐Ÿ“„ License

This project is licensed under the **BSD 3-Clause License** - see the [LICENSE](LICENSE) file for details.

```
BSD 3-Clause License

Copyright ยฉ 2025-present tinyBigGAMESโ„ข LLC
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice
2. Redistributions in binary form must reproduce the above copyright notice
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
```

## ๐Ÿ“ž Support & Community

### ๐Ÿ“š **Documentation & Resources**
- ๐Ÿ“– **[DEVELOPER-GUIDE.md](DEVELOPER-GUIDE.md)** - 12-section comprehensive development guide
- ๐Ÿ“Š **[TEST-REPORT.md](TEST-REPORT.md)** - Current test coverage & performance metrics
- ๐Ÿ”ง **API Reference** - Inline documentation in `CPascal.pas`
- ๐Ÿ“‹ **Examples** - Real-world usage patterns in test suites

### ๐Ÿ’ฌ **Community Channels**
- ๐Ÿ› **Issues**: [GitHub Issues](https://github.com/tinyBigGAMES/CPascal/issues) - Bug reports & feature requests
- ๐Ÿ’ฌ **Discussions**: [GitHub Discussions](https://github.com/tinyBigGAMES/CPascal/discussions) - General questions & ideas
- ๐Ÿ’ฌ **Discord**: [Join our community](https://discord.gg/tPWjMwK) - Real-time chat & support
- ๐Ÿฆ **Bluesky**: [@tinybiggames.com](https://bsky.app/profile/tinybiggames.com) - Updates & announcements

## ๐ŸŒŸ Acknowledgments

- ๐Ÿ’– **Built with love** for the Delphi community
- ๐ŸŽฏ **Inspired by** Pascal's readable syntax and C's systems programming power
- ๐Ÿš€ **Focused on** practical, real-world usage scenarios
- ๐Ÿ† **Committed to** enterprise-grade quality and performance
- ๐ŸŒ **Supporting** the future of Pascal application development

### ๐ŸŽ–๏ธ **Special Thanks**
- **Delphi Community** - For continued support and feedback
- **Beta Testers** - Early adopters who helped shape CPascal
- **Contributors** - Everyone who has submitted issues, PRs, and improvements

---

**Made with โค๏ธ by [tinyBigGAMESโ„ข](https://github.com/tinyBigGAMES)**

**CPascal** - *Where Pascal meets C, systems programming becomes elegant.*