Last active
January 22, 2018 19:33
-
-
Save gheoan/9968496959e5c9cf27cb98b61679bd63 to your computer and use it in GitHub Desktop.
22nd Century C by @procedural https://web.archive.org/web/20161021004451/https://procedural.github.io/post/22nd-Century-C/
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
function clangs { clang --analyze -Xanalyzer -analyzer-output=text $@ && clang -Werror=assign-enum -Werror=conversion -Werror=enum-conversion -Werror=nonnull -Werror=nullability -Werror=nullability-completeness -Werror=return-type -Werror=switch -Werror=switch-default -Werror=switch-enum -Werror=uninitialized -Werror=unused-result $@; } | |
clangs main.c -fsanitize=leak |
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
#pragma once | |
#include <stdarg.h> | |
#include <stddef.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
// clang-format off | |
typedef char c8; | |
typedef int8_t i8; | |
typedef uint8_t u8; | |
typedef int16_t i16; | |
typedef uint16_t u16; | |
typedef int32_t i32; | |
typedef uint32_t u32; | |
typedef int64_t i64; | |
typedef uint64_t u64; | |
typedef uint16_t f16; | |
typedef float f32; | |
typedef double f64; | |
// clang-format on | |
// clang-format off | |
#define internal static | |
#define var __auto_type | |
#define let __auto_type const | |
#define case break; case | |
#define default break; default | |
#define _Merge(x, y) x##y | |
#define _Anyname(x) _Merge(_Anyname_, x) | |
#define _ _Anyname(__COUNTER__) | |
#define defer(x) __attribute__((cleanup(x))) | |
#define streq(x, y) (strcmp(x, y) == 0) | |
#define use __attribute__((warn_unused_result)) | |
#define bytesof(x) (ptrdiff_t)(sizeof(x)) | |
#define countof(x) (ptrdiff_t)(sizeof(x) / sizeof((x)[0])) | |
#define forcount(index, count) for (ptrdiff_t index = 0, size = count; index < size; ++index) | |
#define foruntil(index, end, array) for (ptrdiff_t index = 0; (array)[index] != end; ++index) | |
#define forrange(index, start, end) for (ptrdiff_t index = start, stop = end; index != stop; ++index) | |
// clang-format on | |
static inline void print(const char * format, ...) | |
{ | |
va_list args; | |
va_start(args, format); | |
vprintf(format, args); | |
printf("\n"); | |
va_end(args); | |
} |
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
#include <stdlib.h> | |
#include <stdio.h> | |
#include "c22.h" | |
struct TwoInput | |
{ | |
i32 one; | |
i32 two; | |
}; | |
void Two(const struct TwoInput * in) | |
{ | |
print(""); | |
print("Two exited the scope"); | |
print("Defer function input"); | |
print("in->one: %d", in->one); | |
print("in->two: %d", in->two); | |
} | |
void One(void * _) | |
{ | |
print(""); | |
print("One exited the scope"); | |
} | |
use f32 Add(f32 x, f32 y) | |
{ | |
return x + y; | |
} | |
void demalloc(void ** alloc) | |
{ | |
free(*alloc); | |
} | |
i32 main(i32 ArgCount, c8 ** Args) | |
{ | |
defer(demalloc) void * foo_address = malloc(100000); | |
defer(Two) struct TwoInput _ = {.one = 1, .two = 2}; | |
defer(One) void * _; | |
let _ = Add(1, 2); | |
let x = 32; | |
let y = &x; | |
print("Address of x: %p", y); | |
print(""); | |
forrange(i, 1, ArgCount) | |
{ | |
let argument = Args[i]; | |
foruntil(j, '\0', argument) | |
{ | |
let character = argument[j]; | |
printf(" %c ", character); | |
} | |
print("\n"); | |
} | |
i32 thenumbers[] = {4, 8, 15, 16, 23, 42}; | |
forcount(i, countof(thenumbers)) | |
{ | |
let number = thenumbers[i]; | |
print("index: %zu, number: %d", i, number); | |
} | |
print(""); | |
switch('F') | |
{ | |
case'A': print("A"); print("Excellent!"); | |
case'B': print("B"); | |
case'C': print("C"); | |
case'D': print("D"); | |
case'F': print("F"); print("High five!"); | |
default: print("?"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment