There are two aspects to your question:
- Can we arrange a convenient syntax to express one control structure in terms of another?
- Can we actually define new control flow mechanisms.
For example, suppose we have a language which only has the if
statements without else
:
if A then B
Can we express if A then B else C
somehow? Yes, like this:
a = A ;
if a then B ;
if not a then C
It would be even better if we could package up the above piece of code into convenient syntax so that we could write just if A then B else C
.
Sometimes we can define new control structures using existing ones. For instance, a while
loop
while A do B
can be replaced with a call to a recursive function (under some technical assumptions):
function whileLoop():
if A then (B ; whileLoop ())
whileLoop();
This sort of transformation is a bit more involved because it requires that for each while
loop we define a new recursive function. If we were to automate the process, we would have to worry about many details (how to invent unique names of generated functions, do the functions have access to the local variables, etc.) than when we're performing just a straightforward syntactic translation of one variant of loop to another.
On the other hand, there are control mechanism which simply are not expressible in a language, unless already supported. One such example would be continuations. You can't get these in C, for example, but they exist in Scheme.
Your question is about syntax: can we make a syntactic transformation that replaces one piece of syntax with another piece of syntax. Languages usually deal with this sort of thing through a macro mechanism or syntax extensions. Here are some examples:
- C has macros which will get you some of the way
- Scheme has a more elaborate macro system which gets you a lot further
- Racket, a descendant of Scheme, has an elaborate multi-stage computation system which allows essentially arbitrary syntax extensions
- OCaml has the camplp5 syntax extension tool which allows pretty much arbitrary syntax extensions
- Template Haskell is an extension of Haskell which allows meta-level transformations of syntax.
I am sure there are many other languages with similar facilities that I am not familiar with.