~mcf/cproc

implementation of defer statement

Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Details
Message ID
<D5F252VB56Y9.3K38CA0ADU0YT@vlhl.dev>
DKIM signature
pass
Download raw message
for anyone insterested, i've started implementing `defer`, as specified
in this proposal: https://open-std.org/JTC1/SC22/WG14/www/docs/n3199.htm

the proposal got accepted by wg14 and will be part of c2y, probably not
as is, but likely very close to the current specification

i didn't intend to upstream this implementation, but more so just have
some fun hacking at a c compiler, but since i've figured someone else
might be interested, decided to make a post in the mailing list

the current impl lacks error checkings yet, but can compile example code
such as:

int nya(void) {
	char *foo = strdup("nya");
	defer free(foo);

	// defer blocks are executed last in first out
	defer printf("print this last\n");

	printf("%s\n", foo);

	// blocks also work:
	defer {
		printf("print this first\n");
		printf("print this second\n");
	}

	// deferred statments:
	defer {
		if (true)
			printf("true!\n");
		else
			printf("false!\n");
	}

	return strlen(foo);
}

for the interested, the code can be improved as well, but this is a
first draft at it, on this repo:
https://git.vlhl.dev/navi/cproc.git/commit/?h=defer&id=3e51be9d94e561549281e386ce1b7554018ef4dd
Reply to thread Export thread (mbox)