This repository contains code for Taming Control Flow A Structured Approach to Eliminating Goto Statements.
It's a pet project, so I used advanced tools. For an easy usage I provide a Dockerfile.
Build docker:
docker build -t goto_elimination .
Run docker:
docker run -it -v $(pwd)/inputs:/tmp/inputs goto_elimination:latest /tmp/inputs/elim_while.txt
This code requires inputs in a form of a Mermaid. The following example shows all basic building blocks of the app (assignment, conditional, print).
Example input:
graph TB
A[a=0] --> B[a=a+1]
B --> C{a < 10}
C -->|True| B
C -->|False| D(a)
The same input, but converted into a diagram:
graph TB
A[a=0] --> B[a=a+1]
B --> C{a < 10}
C -->|True| B
C -->|False| D(a)
Parsed code:
#include <iostream>
using namespace std;
int main() {
int a=0;
/***************
* Statement *
***************/
A: a=0;
B: a=a+1;
C: if (a<10) { goto B; }
D: cout << a << endl;
}
Code with eliminated gotos:
#include <iostream>
using namespace std;
int main() {
int a=0;
int _elim_C_f_=0;
/***************
* Statement *
***************/
A: a=0;
_elim_C_f_t_: _elim_C_f_=1;
_elim_C: while (a<10 || _elim_C_f_) {
_elim_C_f_f_: _elim_C_f_=0;
B: a=a+1;
}
C: (void)1; // Empty
D: cout << a << endl;
}