Proposal to add modules to C with a simple extension.

PAUL VAN DE GRAAF ee163ahj at sdcc13.UUCP
Fri May 3 11:51:20 AEST 1985


[ Frobozz magic bug repellent ]

Overview:
	I'm sure most of you (like me) find the method of information hiding
in C a bit excentric, but learn to live with it.  Some code I've seen uses 
macros like LOCAL, EXPORT, and IMPORT to make it more logical.  I still don't
think this completely solves the problem.  Since C doesn't allow nested
functions, you must resort to using global variables when functions need to
share variables.  Of course you can put the related functions in a separate
file and declare the globals static, or depend on the order of declaration of
the globals, but these tricks are not always explicit and can cause problems
with maintainance and revision later.

Proposal:
	Allow blocks (modules) at the outermost (global) level of scope.
The code inside of these blocks is treated as if it were in a separate file. 
I think global variables within a module should be implied to be of "static"
type within the module, and functions implied to be of "extern" type within
the file that includes the module.  This explicitly shows which variables are
shared within the module. [A minor point, I'll leave to language sematicians]
Modules, of course, could be nested with the expected results.

Simple example: (just a demonstration off the top of my head, don't flame it.)
===============================================================================
int global_var;

{      					/* <--- start of module */
	char buffer[BUFSIZE];
	int  buf_index = 0;		/* <--- these vars are static.  */

/* putch(): buffered putchar() aproximation, an extern int outside module */

	putch(c)			
	char c;
	{
		buffer[buf_index++] = c;

		if (buf_index >= BUFSIZE)
			return(write(1, buffer, BUFSIZE));
		else 
			return(1);
	}

/* flush(): flushes the buffer for putch() similar to fflush of stdio.     */

	flush()
	{
		return(write(1, buffer, buf_index + 1));
	}
}				/* <--- end of module */

main()
{
	putch('f'); putch('o'); putch('o'); putch('!');
	flush();
}
===============================================================================

Analysis:
	Implementing this change to the language is relatively painless.  All
that is necessary is to treat modules as separate files.  Nested modules and
my suggestion that a modules's globals be static may complicate things a bit,
but not unduly.  As far as I can tell, only extra production need be added to
the grammar (IE.  whatever -> '{' file '}').
	Furthermore, it is downwardly (is this a word?) compatible to old C, and
a logical extension of the blocks availiable within functions.

Questions:
	Does anyone see anthing wrong with this?  I just came up with this idea
this morning, and have not thought it out too much, but the concept seems so
simple that I decided to post anyway.  More importantly, do you think this 
belongs in the language?  Or is it one of those things that will inspire
religious discussions about how much of a travesty this is to the language etc.
[ I must say I am growing weary of indentation and if (10 == foo) articles! ]

Comments are welcome...

Paul van de Graaf	sdcsvax!sdcc13!ee163ahj		U. C. San Diego	



More information about the Comp.lang.c mailing list