Carl Witthoft
Carl Witthoft

Reputation: 21532

Converting FOCAL to language without GOTO: can an "if" statement with multiple GOTO be parsed algorithmically?

I know that I can translate simple GOTO inside a for or while loop by using if/else or break statements. My question is about trying to write a 'translator' function which automatically converts FOCAL ternary IF statements, e.g IF (x-5) 3.1, 4, 6.2 , where those numbers indicate GOTO the designated line numbers elsewhere in the code.
Since the target lines may then lead to other IF statements or even back to the original IF statement, can an algorithm successfully "de-spaghettify" the code, or is this one of those software problems that is known to be incomplete?
Presumably a chunk of translated code, ending only when a QUIT command is found (terminate the program), would end up inside each section of if{} else if{} else{} output code. But even so, can the translation algorithm "know" when to stop loading input code lines? One of those "chunks" could have another IF statement, for example.

Upvotes: 1

Views: 39

Answers (1)

Diego Ferruchelli
Diego Ferruchelli

Reputation: 874

An "arithmetic IF". Long time no see one of these... :)

If you actually want to "move" the code, some programs won't be "de-spaghetizable" (except, maybe, under certain well known conditions). Suppose, for instance, that you have an endless loop going back and forth between two (or more) IFs.

But if you choose a procedural/modular approach (not "moving" the target code, but placing it in a separate routine and calling it), and your target language allows for recursion, it may work (at least, until you actually exhaust the computer resources).

Upvotes: 1

Related Questions