83 lines
3.3 KiB
HTML
83 lines
3.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta content="zc - what if C, but easier?" property="og:title">
|
|
<meta content="zc is a single header library for C which adds some nice QOL features." property="og:description">
|
|
<title>zc</title>
|
|
</head>
|
|
<body>
|
|
<h1>zc - what if C, but easier</h1>
|
|
<h2>NOTE: I don't recommend using this in production. Here be dragons!</h2>
|
|
<p>zc is a single header library for C which adds some nice QOL features, such as:</p>
|
|
<ul>
|
|
<li>less scary looking code (<code>Integer list myList = many(Integer, 10)</code> vs <code>int* myList = malloc(sizeof(int) * 10)</code>)</li>
|
|
<li>OOP patterns (objects, classes, methods, constructors, destructors) (no inheritance, because no one need that anyway)</li>
|
|
<li>No disgusting yuckies from C++ like operator overloading, name mangling, and hidden allocation</li>
|
|
<li>Very small library, easy to understand</li>
|
|
</ul>
|
|
<p>To get started, do this:</p>
|
|
<pre><code>git clone https://chookspace.com/max/zc
|
|
cd zc
|
|
sudo make install</code></pre>
|
|
<p>Read the docs below to know how to use</p>
|
|
<h2>Docs</h2>
|
|
<p>Create an entry point for your program</p>
|
|
<pre><code>entry {
|
|
|
|
}</code></pre>
|
|
<p>From here, do stuff like this:</p>
|
|
<pre><code>entry {
|
|
print("Hello, World!");
|
|
}</code></pre>
|
|
<h3>Variables</h3>
|
|
<p>Variables are defined in a similar way to C.</p>
|
|
<pre><code>Integer x; // defines x with no value
|
|
x is 5; // gives x a value
|
|
print(x);
|
|
|
|
String y is "dingus"; // assign with a value
|
|
print(y)
|
|
|
|
Integer list z is many(Integer, 10); // Creates a list with space for 10 integers
|
|
for (Integer a is 0; a < 10; a increment) z[a] is a * 10; // Give them all a value</code></pre>
|
|
<h3>Classes</h3>
|
|
<p>Due to the nature of the C preprocessor which this project makes heavy use of, defining classes works differently in zc to other programming languages.</p>
|
|
<p>Create a class:</p>
|
|
<pre><code>class(Car, {
|
|
String make;
|
|
Integer year;
|
|
Method(Car, drive, void, Integer fuel); // usage: Method(className, methodName, returnType, args...)
|
|
Destructor(Car);
|
|
});</code></pre>
|
|
<p>Define it's methods:</p>
|
|
<pre><code>method(Car, drive, void, Integer fuel) {
|
|
printf("A %s can drive %d km with %d fuel", self->make, fuel * 5, fuel); // Use self to access the other fields in the class
|
|
}
|
|
|
|
destructor(Car) {
|
|
free(self->make);
|
|
}</code></pre>
|
|
<p>Finally, make a constructor:</p>
|
|
<pre><code>constructor(Car, String make, Integer year) {
|
|
return (Car) {
|
|
.make is make,
|
|
.year is year,
|
|
.drive is __Car_drive, // format for methods is __ClassName_methodName
|
|
.destructor_fn is destroyCar // format for destructor is destroyClassName
|
|
};
|
|
}</code></pre>
|
|
<p>Use your newly made class:</p>
|
|
<pre><code>entry {
|
|
Car myCar = newCar("Toyota", 1996);
|
|
myCar.drive(&myCar, 32);
|
|
|
|
// Don't forget to forget when you're done!
|
|
// If you don't have a destructor, this call won't compile.
|
|
forget(myCar);
|
|
}</code></pre>
|
|
|
|
</body>
|
|
</html
|