Keywords Arent Special

Problem:

C keywords like 'for', 'if', 'case' can be thought of as functions operating on code blocks. However they have a non standard syntax as compared to ordinary every day C functions. This unusual syntax is valuable. It improves the readability of C programs. For example:

   for( i=0; i<100; i++)
      { DoSomething(); } 

for most programmers is significantly more readable than:

   for( {i=0}, {i<100}, {i++}, {DoSomething()}  );

However, treating intrinsic keywords on a different footing to standard function names actually within the compiler may lead to additional code, making the compiler larger and harder to maintain than it needs to be.

Therefore:

Put intrinsic keywords on the same footing as user defined function names. Use one symbol table, and therefore one search, for both. To achieve this, FactorSyntax from the core of the compiler design. Write a table driven module for checking the syntax of commands which is separate from the module that generates compiled code. Base the syntax checker on rules like:

   for(*,*,*,*) <=> for(*;*;*)*;
The best way to represent such rules is open to discussion, and good enough ways may already exist within yacc. Process syntax errors in a generic way e.g. reporting that a comma was found where a semicolon was expected, or that <n> arguments were found where <m> were expected for <function_name> rather than having special error reporting facilities for each different keyword.

To preserve C's distinctive readability style, assuming this approach is being used for C, disallow use of non-standard function syntax in user defined functions.

Advantages:

Reduced code to do the same work.

This is a particular instance of OnceAndOnlyOnce.

Makes onward development of the compiler to support other languages with the same compilation engine easier.

But:

Need to be aware of evaluation orderings, which still make these a special case to be handled in the compiler. Further, for error analysis, you may need special messages. From experience, the need for distinguished error analysis can mess up all sorts of elegant factoring.



Discussion:

Yes, you really should take a close look at the ForthLanguage: In FORTH, language constructs, such as loops and conditionals are "on the same level" as functions. In some ways, they're indistinguishable.

See also LispLanguage, where function calls and loops/conditionals have the same syntax -- and source code is valid LISP data. You can define your own control flow constructs using LispMacros. Note that in general you can't treat conditional operators as functions in any StrictLanguage because they need LazyEvaluation to work the way you expect.

EDS had a research language, OWL (inspired by the SmalltalkLanguage), where language constructs, like loops and conditionals, were really nested instances of special classes. You could define your own language constructs and method/attribute types. This was used, for example, to integrate rule based expert system libraries into the system. It had a "SyntacticSugar" interface that would translate the language instances into or out of a more conventional-looking language syntax, for programmer convenience. This shows that the actual syntax of the language makes little difference to a compiler.

Contributors: JeffGrigg, NoelWelsh


It's possible to design languages with fewer reserved words. That is, "if" is a keyword but that doesn't prevent users declaring variables or functions also named "if". Normal scoping applies - you can't use the conditional while the variable is in scope.

This makes it easier to evolve the language. For example, when C++ added "template" as a reserved word, it broke all my classes that used that name. If normal scoping applied, my "template" class would merely hide the "template" keyword - which I hadn't wanted to use anyway. The C++ committee knows this and so is reluctant to add new keywords, prefering to overload "static" yet again.

Good points. This is addressed, for example, in DrScheme by allowing the LispMacros and special forms (aka reserved words) to be imported and exported via the module system. I believe Scheme48 also has this ability -- NoelWelsh


CategoryRefactoring


EditText of this page (last edited January 16, 2008) or FindPage with title or text search