Language Syntax Suggestion

Matthew Tuck matty@box.net.au
Sun, 14 Mar 1999 10:46:10 +1030


Jeremy Dunn wrote:

> I wanted to say something about IF-THEN and LOOP structures. I have
> often felt that if/then structures in most languages carry more verbiage
> than is necessary. Here is how I would write various expressions:
> 
> If A Then B                     [If,A,B]
> If A Then B Else C              [If,A,B,C]
> If A Then B ElseIf C Then D     [If,A,B,C,D]
> 
> Very simple, no need for all those elses and thens. A SELECT CASE
> structure could be written as [If,X,[,a,b,c][,A,B,C]] where X is the
> expression or item that is to be compared with a,b,c to return one of
> the results A,B,C. No need to have another function.

It would certainly be useful to represent some functions for handling
these things easily.  I'm of the opinion to allow the syntax to have
shorthands that map onto calls underneath.  So for example ifs would
follow the Smallttalk technique.

Expression.ifActn( IfTrueAction, IfFalseAction )
Expression.ifExpr( IfTrueExpression, IfFalseExpression )

and so on ...

This would mean that there were no "if" statements in the underlying
language but you could still use them if you desired in your view.
 
> Looping structures have a multiplicity of functions that could be
> simplified into a single function which I call LOOP. The Loop function
> has this basic form:

We could already do this with shorthands, albeit in a different way.  An
underlying loop ... exit ... end loop structure exists that shorthands
could be built for, eg for, pre-test, mid-test, post-test, repeat,
infinite loop, etc.

> We notice that the test condition is now a two element collection that
> consists of a blank argument and the actual test. The blank argument
> indicates the order in which the body of the loop is to be executed. If
> the first argument is blank then the body is executed first then the
> test condition and vice-versa. No need for separate DoWhile and WhileDo
> functions. A FOREACH loop would be written as
> 
> [Loop,[,X,[,a,b,c]],[<body>]]
> 
> This would be the same as
> 
> Foreach X in [,a,b,c] Do <body>

One thing I don't really like about this is that it doesn't allow a
stream to be the thing to search for the element in.  If you want to
precompute the entire list you might lose some functionality - sometimes
the loop might affect the next element to select (a simple way to
terminate for example).

You can have something like:

for A in range(1,10).forward
 ...
end for


For a 1 to 10 loop.  Obviously here you can precompute the list, but it
illustrates the for loop can delegate the generation logic into another
element.
 
> I am of the opinion that if-structures and loop-structures should be
> handled in single commands rather than a proliferation of special cases
> that are really only representing one general concept.

This is partly what the shorthand concept was created for.  If other
people don't have the shorthand defined, it will automatically translate
out to full form.

What this means for you is that if we write 'if'/'while', you'll likely
end up with a method call instead.  Now I don't know how easily you're
going to be able to massage that into what you want (the semantics of
the language can't differ after all), and I can't guarantee you'll be
able to do it perfectly.  You don't seem to be too interested in OOP so
it might be difficult - although you could define your own shorthands
which, when you get code from another, get scanned for and converted,
allowing you to edit in a form more adapted to your syntax.

-- 
     Matthew Tuck - Software Developer & All-Round Nice Guy
             mailto:matty@box.net.au (ICQ #8125618)
       Check out the Ultra programming language project!
              http://www.box.net.au/~matty/ultra/