Files
c-prime/README.md

293 lines
4.8 KiB
Markdown
Raw Normal View History

2025-11-17 10:33:35 +01:00
# 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:**
```c
#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:**
```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
```c
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
```c
#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)
```c
template<T>
class vector {
T * items;
int total;
void add(T item) { ... }
}
```
Compiler generates correct type-specialized C.
---
## 🧱 Multi-Inheritance
```c
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:
```c
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
```bash
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:
```sh
git clone https://code.kajdijkstra.com/kaj/c-prime.git
```
Or SSH:
```sh
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.**
2025-11-17 10:18:10 +01:00