opc89

1. About

opc89 is a small, modular language extension to ANSI C89. It works similar to yacc: reads an opc89 source file and writes a C source file that then can be compiled by any C89-compatible compiler.

Extensions are designed to be the smallest possible. Extensions are also strict and explicit: the number of things a reader of an opc89 source would need to look after is kept to the minimum.

2. Source, download, contact

VCS: svn://repo.hu/opc89/trunk

Release tarballs: web

Author: Tibor 'Igor2' Palinkas

contact: mailto: opc89 {at} igor2.repo.hu

3. Extensions

3.1. Operator overloading: opfunc

Limited, controlled operator overloading by type. The code may typedef custom types marked as "opfunc" types with an opc89 keyword. Whenever opc89 detects the type being used in an expression, the expression is replaced by a call to a function the user needs to implement (typically as a static inline).

More on opfunc...

In-function init/uninit: ifiu

A common pattern in C functions is that various local states are initialized throughout the function and are then uninitialized at the end, when the function returns. For example a "dump all data" function may fopen() the output file at some point and fclose() it at the end, may allocate a buffer at a different point which also needs to be free()'d before return.

The code for this is trivial if all such states are to be initialized unconditionally at the beginning of the function but the code tends to get cluttered and hard to read if some states are initialized only conditionally and/or the function has multiple points of returns.

The ifiu extension allows the programmer to specify the init and uninit code for an internal state at once, wherever the initialization happens. Opc89 will make sure that the uninit code is executed once on every return from the function, but only if the initialization ran.

ifiu...

4. Rationale

"But why?" you may ask, when there are so many languages out there featuring operator overload and various mechanisms for handling init/uninit code. In context of C, the most trivial option being C++.

I write most of my code in C89. I am generally satisfied with the features of the language. Unlike C++ and many of the modern languages, C89 is relatively small, simple, well scoped and well specified. It has a large coverage: many independent compiler implementations on all possible platforms.

Coding small and large projects, I rarely need any extra features over C89's standard set. In a few cases, typically in special purpose libraries, I do need them, tho. But switching to C++ for example would not be justified: if we take the feature set C89 offers as 100%, my special lib needs 102% while C++ offers 300%. In theory it is possible to use a subset of C++ that would cover my 102%, but in reality it already brings all the hassle of extra dependencies, lowered portability, increased compilation time and overhead in debugging (e.g. C++ name mangling). What is even worse, I find C++'s solutions to these specific problems inelegant and over-complicated. For example using objects and constructors and destructors for simply getting a local state uninitialized looks like over-engineering to me, compared to ifiu's simple init-uninit code pair.