Files
c-prime/README.md

383 lines
6.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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
---
## 🏗 Build Flow Overview (How C Compiles)
C uses a **three-stage build pipeline**:
```
[1] Build the preprocessor → [2] Translate C → C → [3] Build C output
```
### 1 — Compile the Preprocessor Compiler
The script:
```
scripts/compilePreprocessor.sh
```
Compiles all code in `/source` into a standalone binary:
```
/binaries/preprocessor
```
This binary **is the C compiler**.
It takes `.cprime` projects and rewrites them into pure C99 source.
---
### 2 — Convert C code into pure C99
All example/demo projects live in:
```
/application/source
```
The script:
```
scripts/runPreprocessor.sh
```
runs the freshly built compiler on a demo project and outputs C files into:
```
/application/demos/<demo-name>/
```
The structure stays clean: every project has its own demo folder.
---
### 3 — Compile the generated C99 project
The script:
```
scripts/compileApplication.sh
```
compiles the generated C99 code using **TinyCC (tcc)** and links any needed libraries.
This produces final runnable binaries in:
```
/binaries/
```
---
### In short
```
+----------------+ +------------------+ +----------------------+
| Your C source | --> | C Preprocessor | --> | Pure C99 + Binaries |
| (application) | | compiler | | (example.opengl, etc)|
+----------------+ +------------------+ +----------------------+
^ ^
| |
/application/source /binaries/preprocessor
```
You can automate everything through the helper menu program in `/scripts/menu.c`.
---
# 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.**