0

In C, C++ and some dialects of BASIC, goto labels are declared with the syntax label:. I'm working on a language that uses name: type as the syntax for variable declarations, so I'd prefer if possible to use something a bit more distinctive for labels. I am or at some point was passingly familiar with many languages that use that syntax for variable declarations, but off the top of my head I can't recall a single one of them that doesn't ban goto (not looking to argue about whether that's a good or bad design decision, just noting it did take place in those cases), so I'm trying to figure out where to look for precedent on labels.

What other forms of syntax has been used for goto labels? Are there any criteria I might be overlooking in choosing one over another?

(I just remembered Ada is an exception. But it uses <<label>>, which doesn't mix well with the use of those symbols as C-style shift operators.)

rwallace
  • 1,208

6 Answers6

5

DOS batch scripts use :label.

// I personally would use >label. :)

Fixpoint
  • 742
4

You could make label a keyword and use something like:

label foo;

or

LABEL FOO;

if you want it to stand out more.

But as Mitch says, it really depends on what the rest of your language's syntax looks like.

There's an argument to be made that the label syntax shouldn't fit in cleanly with the rest of the language, so that goto labels stand out more. (That was the rationale for Ada's <<LABEL>> syntax.)

Keith Thompson
  • 6,442
  • 2
  • 30
  • 36
3

If you must use GOTO statements, even knowing that they are unnecessary and an open invitation to mess up your programs, then do what Ichbiah et al (the original designers of Ada) did: MAKE THE SYNTAX UGLY.

Make it stick out like a giant thumb with a really hideous fungal infection that has been smashed with a hammer. Make it EASY for the maintenance programmers to see it, so they will know that the author of the code they are asked to maintain was a total cretin, who needed to be forcibly transferred to the business school, rather than be allowed to matriculate in computer science, so they will be on the lookout for the not-quite-as-obvious idiot mistakes that are bound to be festering all through his alleged code.

Ichbiah admitted they did this, in so many words. The DoD specification for the language explicitly required that it contain a GOTO statement, because the DoD believed (wrongly, in my personal opinion) that the case against it had not been made solidly enough. They couldn't NOT put in a GOTO, but they could and did make it UGLY, to discourage its use. Most places that used Ada outlawed its use immediately, or at least required management signoff on each and every GOTO, to encourage programmer to find a GOTO-less alternative.

Something like ###--->>>LABEL<<<---### would work. The label must be on a line, with massive "decoration", all the way up against the left margin, and ALL BY ITSELF.

Or maybe something like --->>>LABEL ... HEREBEDRAGONS, where "HEREBEDRAGONS" is a reserved word in the language, and any variation on HEREBEDRAGONS that is not all uppper case is defined to be a fatal syntax error. Make it HARD and PAINFUL for the programmer to use it.

0

I am not sure where I am getting this from, but somehoew @<label>: feels natural to me.

But I also suspect the 'right' answer depends on what else is going on in your new language, syntax-wise.

0

How about [label]? For example,

Stack<Solution> partialSolutions = new Stack<Solution>();
partialSolutions.add(initialSolution);

[weirdSearch]
if (partialSolutions.isEmpty()) {
    acceptedSolution = null;
    goto cleanup;
}

[weirdSearchSkipEmptyCheck]
Solution sol = partialSolutions.pop();
switch (rand() % 3) {
    [0] // accept this
    acceptedSolution = sol;
    goto cleanup;

    [1] // reject this and try the next solution
    goto weirdSearch;

    [2] // try a derived solution
    partialSolutions.push(sol.deriveAnotherSolution());
    goto weirdSearchSkipEmptyCheck;
}

[cleanup]
free(stack);

I don't know of any language that uses this syntax, though it reminds me of sections in INI files.

Some other bad ideas:

<label>
.label.
:label:
*label*
>label<

These should all be easy to parse if your language is Java-like, since no other statement can begin with an infix operator.

0

JavaScript does not have goto but it has labels:

outer:for(var i=0;i<10;++i) {...}

and yet it has:

{ outer:"value" }

and yet you can find there:

some? outer:"value";

In JavaScript 2.0 typed variables were added:

var some:int;

All this does not create any conflicts as ':' appears in different contexts.