Add example and readme, update header

This commit is contained in:
2026-02-07 14:04:32 +11:00
parent 6b3aa87b13
commit b31f2aaaa5
3 changed files with 246 additions and 15 deletions

63
zc.h
View File

@@ -1,18 +1,23 @@
#if 0
# This is under construction!
# zc compiler (in a Bash script)
# If you dont want to write in the style of C, write as a zc script!
# If you dont want to write in the style of C, write as a .zc script!
# What this does:
# * Creates a .zcbuild folder
# * Creates a .zc-build folder
# * Adds some boilerplate
# * Inserts your code
# * Compiles and produces an executable
# * Removes the .zc-build folder, unless there was an error, so you can debug
# Alternatively, you can use zc as a C library, if you wish.
echo "Work in progress! Please come back later"
set -e
mkdir -p .zc-build
echo "#include <zc.h>" > .zc-build/main.c
cat "$@" >> .zc-build/main.c
gcc .zc-build/main.c
rm -r .zc-build
exit
@@ -21,6 +26,12 @@ exit
#ifndef ZC_H
#define ZC_H
/*
* zc - What if C, but easier?
* A work in progress project by Maxwell Jeffress
* Licenced to you under the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
@@ -64,6 +75,8 @@ exit
#define and &&
#define or ||
#define pass (void)0
/*
* TYPES
* Creates friendlier names for different types.
@@ -102,19 +115,15 @@ default: printf("unknown type\n") \
__new_something;\
end)
#define newlist(...) (then\
void* __new_something is malloc(sizeof({__VA_ARGS__}));\
if (__new_something equals NULL) { print("Couldn't malloc :("); exit(1); }\
__new_something;\
end)
#define many(x, size) (then\
x* __new_something is malloc(sizeof(x) * size);\
if (__new_something equals NULL) { print("Couldn't malloc :("); exit(1); }\
__new_something;\
end)
#define forget free
#define forget(var) _Generic((var), \
default: var.destructor_fn(&var) \
)
#define resize(var, size) then\
typeof(*var)* __resized_something is realloc(var, sizeof(typeof(*var)) * size);\
@@ -125,15 +134,39 @@ end
/*
* CLASSES
* These macros assist with creating classes.
*/
*/
#define constructor(class, ...) class new##class(__VA_ARGS__)
// #define destructor(class, ...) void destroy##class(class* self, __VA_ARGS__)
#define destructor(class, ...) void destroy##class(class* self, ##__VA_ARGS__)
#define class typedef struct
#define Destructor(class) void (*destructor_fn)(struct class* self)
#define Method(class, name, return, ...) return (*name)(struct class* self, ##__VA_ARGS__)
#define method(class, name, return, ...) return __##class##_##name(class* self, ##__VA_ARGS__)
#define class(className, body) struct className; typedef struct className body className
#define entry Integer main(Integer argc, String list argv)
static inline Integer newInteger() {
return 0;
}
static inline Float newFloat() {
return 0;
}
static inline Double newDouble() {
return 0;
}
static inline Character newCharacter() {
return 0;
}
static inline String newString() {
return "";
}
static inline Boolean newBoolean() {
return false;
}
static inline void destroyInteger(Integer* self) {}
static inline void destroyFloat(Float* self) {}
static inline void destroyDouble(Double* self) {}