Skip to content

Instantly share code, notes, and snippets.

@theideasmith
Last active June 14, 2017 02:36
Show Gist options
  • Save theideasmith/338c31dbb12e6f135729 to your computer and use it in GitHub Desktop.
Save theideasmith/338c31dbb12e6f135729 to your computer and use it in GitHub Desktop.
Using macros to push the boundaries of C by changing its syntax

unC

You've always loved the lower-level power of C, but wanted its syntax to feel more modern and cutting edge. Look no further and useunC!

unC is a simple, lightweight macro library that makes C more readable.

Just add #import "unC.h" to your code.

Control Flow

IF

IF <statement> DO 
	<insert code here>
END

ELSIF

ELSIF <statement> DO
END

ELSE

By now, you probably get the drill. else statements in unC look like

IF <statement> DO 
		
ELSIF <statement> DO

ELSE	

END

Loops

RUN

Now, to run a loop n times, just implement the RUN macro. Pass it a variable name(declaring the variable will raise compiler errors) and a number of times to run, and walla!

RUN(i, 15)
	printf("%d\n",i);
END	

WHILE

WHILE <statement> DO
END

UNTIL

Runs loop until the given <statement> becomes true

UNTIL <statement> DO

END

Operators

Type Reimplementation
&& AND
|| OR

| MORE < | LESS == | EQUALS = (Assignment) | EQUAL

Variable types

Some commonly used variable types are reimplemented in unC as

Type Reimplementation
int NUMBER
float FLOAT
double DOUBLE

Readability

The following macros are defined to make code more readable if wanted

the
it
a
to
make

And, for those who are tired of typing out int main(int argc, const char*argv[]) every time they start a new project, unC has

MAIN
	<code>
END
//Main
#define MAIN int main(int argc, const char*argv[]) START_SCOPE
//Helper macros
#define START_SCOPE {
#define END_SCOPE }
#define END END_SCOPE
#define BLOCK(code) START_SCOPE code END_SCOPE
//Language specific
#define IF if(
#define DO ) START_SCOPE
#define ELSIF END_SCOPE else if (
#define ELSE END_SCOPE else START_SCOPE
#define RUN(var, n) for (int var=0;var<n;var++ DO
#define WHILE while(
#define UNTIL WHILE !
//Variable types
#define NUMBER int
#define FLOAT float
#define DOUBLE double
//Readability macros
#define the
#define it
#define a
#define to
#define make
//Operators
#define AND &&
#define OR ||
#define MORE >
#define LESS <
#define EQUALS ==
#define EQUAL =
#define PRINT(n,f) printf(n,f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment