Files
c-prime/README.md
2025-11-17 16:11:12 +01:00

4.8 KiB
Raw Blame History

C — C with Classes. Zero Bloat.

C (C-Prime) is a tiny object-oriented extension to the C language. It adds classes, methods, constructors, operator overloads, inheritance, templates, reflection, and more — while still compiling to pure, readable, high-performance C99.

You write modern OOP code. C rewrites it into C. The C compiler does the rest.

Instant preprocessing speeds (just a few milliseconds) 📦 Tiny compiler (written in clean C99) 🧩 Drop-in compatibility with existing C toolchains 🛠 Uses TinyCC, GCC, or Clang to compile the output


Why C?

C++ gives you a lot… and a lot of baggage.

C gives you just the power of classes without templates nightmares, ABI chaos, or hidden runtime behavior. Everything turns into transparent C functions and structs you can read and debug directly.

Write like OOP — ship like C.


🧠 How it works

C is a source-to-source compiler:

C source → C Preprocessor → Pure C output → Normal C compiler

Under the hood:

  1. Parse code (via custom tokenizing lexer)
  2. Extract classes, methods, properties
  3. Replace OOP syntax with C99 equivalents
  4. Generate .c + .h files
  5. Compile with TinyCC (or any C99 compiler)

Output remains beautiful C that you can read.


Language Features

✔ Classes ✔ Methods ✔ Constructors ✔ Operator Overloading (+, +=, ==, …) ✔ Property access via this->field ✔ Inheritance (multiple!) ✔ Templates / generics ✔ Reflection ✔ SQLite3 support ✔ Console logging with automatic pretty-printing ✔ File manipulation ✔ HTTP server utilities ✔ Zero runtime dependencies


🔤 Example: Hello Classes

Input C code:

#include "console.h"

class application {

    sayHi(char * name) {
        console.log("Hi ", name);
    }

    launch() {
        this->sayHi("John");
    }
}

void main() {
    application * app = new application();
    app->launch();
}

Generated C:

// pseudo-simplified
typedef struct application {} application;

void application_sayHi(application * this, char * name) { ... }
void application_launch(application * this) { ... }

int main() {
    application * a = application_new();
    application_launch(a);
}

Readable. Fast. Pure C.


🕹 Constructors

class vector3 {
    float x;
    float y;
    float z;

    constructor(float x, float y, float z) {
        this->x = x;
        this->y = y;
        this->z = z;
    }

    display() {
        printf("x: %f, y: %f, z: %f", this->x, this->y, this->z);
    }
}

void main() {
    vector3 * v = new vector3(1.0, 0.0, 1.0);
    v->display();
}

🌐 Minimal HTTP Server

#include "../http.h"

void handleRequest(struct request * req, struct text * res) {
    res += "HTTP/1.0 200 OK\r\n\r\nHello from C!\r\n";
}

void main() {
    http * server = new http();
    server->createServer(handleRequest);
    server->listen(8080);
}

📁 Filesystem Handling

Full directory scanning + file reads/writes with class APIs.


🔧 Templates (Generics)

template<T>
class vector {
    T * items;
    int total;
    void add(T item) { ... }
}

Compiler generates correct type-specialized C.


🧱 Multi-Inheritance

class B { ... }
class C { ... }

class A extends B, C {
    ...
}

Properties + methods are merged cleanly and deterministically.


🧩 SQL via SQLite3

One-liner in-memory database setup:

sqlite * sql = new sqlite(":memory:");
sql->selectModel("user");
sql->createTable();

Full CRUD built-in.


🛠 Installation & Build

Requirements

  • TinyCC OR GCC/Clang
  • Linux or macOS

Build

make
./cprime input.cprime

This generates .c + .h files in /output.


🔍 Internals

Lexer → Struct Model → Transform → Emit C

Lexer
 ⮡ Tokens
 ⮡ Syntax Data (structs)
 ⮡ Rewrite rules
 ⮡ Generators

The compiler itself is written in clean C99 using a tiny standard library of:

  • arrays
  • text utilities
  • reflection metadata
  • class loaders

🤝 Contributing

C is young — contributions are very welcome!

Ideas:

✔ Better diagnostics ✔ More docs & examples ✔ Editor syntax support ✔ Windows support ✔ More standard library components


📜 License

MIT License — do what you want, just keep the copyright notice.


Clone C

Publicly available:

git clone https://code.kajdijkstra.com/kaj/c-prime.git

Or SSH:

git clone git@code.kajdijkstra.com:kaj/c-prime.git

💬 Final words

Thank you for checking out C!

If youve ever wondered “How does a compiler really work?”

C is a simple but powerful place to learn — and create.

Future of C coding: Object-oriented when you want it — pure C where it counts.