Created
November 1, 2023 07:31
-
-
Save dasshiva/03064046dfc0dd3f9e7b427c1ba2eb73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (C) 2023 dasshiva | |
#include <stdlib.h> | |
// Compiles on GCC 11.4.0 ubuntu idk about other systems | |
// Abusing macros to make C look a tiny bit better (maybe worse for some) | |
#define class(x, contents) typedef struct x x; struct x contents; // declare a class x | |
#define var(ty, name) ty name; // declare a variable | |
#define func(ty, x, ...) ty (*##x) (__VA_ARGS__); // declare a member function maybe static or non-static | |
#define static_func_def(ty, x, ...) ty x (__VA_ARGS__) // declare a static function | |
#define func_defnp(class, ty, x) ty x (class* self) // define a non-static function taking no parameters | |
#define func_def(class, ty, x, ...) ty x (class* self, __VA_ARGS__) // define a non-static function taking parameters | |
#define new(class, ...) class* new_##class(__VA_ARGS__) // define the class's constructor always called new_<class-name> | |
class(system, { | |
var(int, hi) | |
var(int, hey) | |
func(void, function) | |
}) | |
func_defnp(system, int, function) { | |
return 0; | |
} | |
new(system, user_input) { | |
system* p = malloc(sizeof(system)); | |
p->function = function; | |
p->hey = 0; | |
p->hi = user_input; | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment