Obfuscated Tiny C

From Esolang
Jump to navigation Jump to search

Obfuscated Tiny C is a small subset of the C programming language created by Fabrice Bellard, published in 2001. The goal is to be able to compile the language with a small self-hosting compiler (the Obfuscated Tiny C Compiler, OTCC) written in Obfuscated Tiny C, so small that Bellard could submit it to the International Obfuscated C Contest (IOCCC) and win.

Obfuscated Tiny C is compiled to x86_32 machine code. Obfuscated Tiny C has only one data type, a 32-bit word, which can also be dereferenced as a pointer to a word, to a byte, or to a function. Obfuscated Tiny C links to the native C library, and it doesn't come with any extra library.

Bellard has later blown OTCC up to a full tiny compiler, TCC.


Another tiny variant of C is implemeted by Lennart Augustsson's 1996 IOCCC entry. The goal is similar to Bellard's goal.

That variant is compiled to an unknown bytecode that is interpreted by the same source file. It only has restricted operations available:

	OC grammar
	==========
	Terminals are in quotes, () is used for bracketing.

	program:	decl*

	decl:		vardecl
			fundecl

	vardecl:	type NAME ;
			type NAME "[" INT "]" ;

	fundecl:	type NAME "(" args ")" "{" body "}"

	args:		/*empty*/
			( arg "," )* arg

	arg:		type NAME

	body:		vardecl* stmt*

	stmt:		ifstmt
			whilestmt
			dowhilestmt
			"return" expr ";"
			expr ";"
			"{" stmt* "}"
			";"

	ifstmt:		"if" "(" expr ")" stmt
			"if" "(" expr ")" stmt "else" stmt

	whilestmt:	"while" "(" expr ")" stmt

	dowhilestmt:	"do" stmt "while" "(" expr ")" ";"

	expr:		expr binop expr
			unop expr
			expr "[" expr "]"
			"(" expr ")"
			expr "(" exprs ")"
			NAME
			INT
			CHAR
			STRING

	exprs:		/*empty*/
			(expr ",")* expr

	binop:		"+" | "-" | "*" | "/" | "%" |
			"=" |
			"<" | "==" | "!="

	unop:		"!" | "-" | "*"

	type:		"int" stars
			"char" stars

	stars:		"*"*

Links