JSFiddle - React, Tailwind, and code Playground
HTML
Input:
<br><textarea id="input" style="width: 500px; height: 200px;"></textarea><br>
<button id="submit">Submit</button>
<br>Results:<br>
<em>Basic format:</em>
<pre id="basic">
N/A
</pre>
<em>Linear format:</em>
<pre id="linear">
N/A
</pre>
<em>Python layout format:</em>
<pre id="python">
N/A
</pre>
<em>Implied Python format:</em>
<pre id="implied">
N/A
</pre>
JavaScript
document.getElementById("submit").addEventListener("click", function(){
var j = new JCode(document.getElementById('input').value);
document.getElementById('basic').innerText = j.basicFormat();
document.getElementById('linear').innerText = j.linearFormat();
document.getElementById('python').innerText = j.pythonFormat();
document.getElementById('implied').innerText = j.impliedFormat();
});
var JCode = function(input)
{
this.cmd = {
FOR: /for_[^.]*\./g,
IF: /if\./g,
DO: /do\./g,
ELSE: /else\./g,
END: /end\./g
};
var s = input.split(/\n|label_./), split = [], arr, index, ends = 0, tmp, cmd = this.cmd;
for (var i = 0; i < s.length; i++)
{
txt = s[i];
arr = txt.match(cmd.IF);
if (arr) { ends += arr.length; }
arr = txt.match(cmd.FOR);
if (arr) { ends += arr.length; }
arr = txt.match(cmd.END);
if (arr) { ends -= arr.length; }
arr = [];
tmp = '';
for (var c in cmd)
{
tmp += cmd[c].source + '|';
}
arr = txt.match(new RegExp(tmp.substring(0, tmp.length - 1), 'g'));
if (arr)
{
for (var j = 0; j < arr.length; j++)
{
index = txt.indexOf(arr[j]);
tmp = txt.substring(0, index).trim();
if (tmp != '') { split.push(tmp); }
txt = txt.substring(index);
if (arr[j] == (tmp = 'do.') || arr[j] == (tmp = 'else.') || arr[j] == (tmp = 'end.')) { split.push(tmp); txt = txt.substring(tmp.length); }
}
}
txt = txt.trim();
if (txt != '') { split.push(txt); }
}
while (ends > 0)
{
split.splice(split.length - 1, 0, 'end.');
ends--;
}
var missing_do = false, mindex = -1, tmp, has_key, min;
for (var i = 0; i < split.length; i++)
{
s = split[i];
if (missing_do)
{
min = s.length;
has_key = false;
for (var c in cmd)
{
tmp = s.search(cmd[c]);
has_key |= tmp > -1;
if (tmp >= 0 && tmp < min) { min = tmp;}
}
if (has_key)
{
if ((tmp = s.search(cmd.DO)) != min)
{
split.splice(mindex+1, 0, 'do.');
i++;
}
missing_do = false;
s =...