C, Ocean Edition I

A semantic normalization of C

C, Ocean Edition I is a semantic normalization of C. It keeps the language's computational model, its syntax, and its character, and removes the places where a programmer learns one rule and later discovers that an apparently identical construct follows another.

It derives from ISO/IEC 9899:1999, adopts selectively from later C standards, and adds one facility of its own. An array is an array. A function is a function. An address is obtained with &. A definition defines something. An object begins life with a value. Expressions execute in a defined order. Arithmetic has defined behavior.

Where to start

An ordinary source file

Nothing about an Ocean Edition I file announces that a different language is being used. The differences are visible only where they matter.

#import <stdint.h>

#namespace "buffer"

struct Buffer
{
	uint8_t *data;
	size_t length;
	size_t capacity;
};

static inline size_t min_size(size_t a, size_t b)
{
	return a < b ? a : b;
}

int copy(struct Buffer *dst, const struct Buffer *src)
{
	if (dst == null || src == null)
	{
		return -1;
	}

	size_t count = min_size(dst->capacity, src->length);

	copy_bytes(&dst->data[0], &src->data[0], count);
	dst->length = count;

	return 0;
}

&dst->data[0] says that an element pointer was wanted, because arrays do not silently become pointers. min_size is a function rather than a macro. buffer would begin zero-filled even without an initializer.

The ten laws

The whole specification is an elaboration of ten laws. Where a clause is ambiguous, it is resolved in favor of the law it serves.

  1. Syntax means what it says.
  2. Expressions keep their types.
  3. Evaluation has an order.
  4. Objects begin valid.
  5. Arithmetic describes finite machines.
  6. Target differences must be real.
  7. Optimization does not define semantics.
  8. Dangerous is allowed.
  9. Existing C syntax is preferred.
  10. Fewer categories are better.

An eleventh commitment sits alongside them and is stated in the specification rather than in a paper: translation itself is deterministic. The same inputs produce the same bytes.