Skip to content

Instantly share code, notes, and snippets.

@Wohlstand
Last active October 8, 2021 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wohlstand/3895db6a738aebce296c2f284cb163a7 to your computer and use it in GitHub Desktop.
Save Wohlstand/3895db6a738aebce296c2f284cb163a7 to your computer and use it in GitHub Desktop.
The code piece used by me in early process of SMBX to TheXTech porting
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VB6 to CPP</title>
<script type="text/javascript">
function replaceType(tpe)
{
tpe = tpe.trim();
if(tpe === "Boolean")
return "bool";
else if(tpe === "Long")
return "long";
else if(tpe === "Integer")
return "int";
else if(tpe === "Single")
return "float";
else if(tpe === "Double")
return "double";
else if(tpe === "String")
return "std::string";
else
return tpe + "_t";
}
function defaultValue(tpe)
{
if(tpe === "bool")
return " = false";
else if(tpe === "int" || tpe === "long")
return " = 0";
else if(tpe === "float")
return " = 0.0f";
else if(tpe === "double ")
return " = 0.0";
return "";
}
function arrayDeclare(tpe, arr)
{
let re = /(-?\d+|\w+)( To )?(-?\d+|\w+)?/gim;
let m = null;
let out = "";
let arraysStack = [];
do
{
m = re.exec(arr);
if (m)
{
if(m[1] && !m[2] && !m[3])
{
// out += "RangeArr<" + tpe + ", 0, " + m[1] + ">";
arraysStack.push({type: tpe, begin: 0, end: m[1]});
}
else if(m[1] && m[2] && m[3])
{
// out += "RangeArr<" + tpe + ", " + m[1] + ", " + m[3] + ">";
arraysStack.push({type: tpe, begin: m[1], end: m[3]});
}
}
} while(m);
out = tpe;
for(let i = arraysStack.length - 1; i >= 0; --i)
{
let q = arraysStack[i];
out = "RangeArr<" + out + ", " + q.begin + ", " + q.end + ">";
}
return out;
}
function convertComment(com)
{
com = com.trim();
if(com.startsWith('\''))
{
com = com.replace(/^'/, "// ") + "\n";
}
else
return "";
return com;
}
function convertArgs(args)
{
let re = /((\w+) As (\w+))(, *)?/g;
let m = null;
let out = "";
do{
m = re.exec(args);
if(m)
{
out += replaceType(m[3]);
out += " " + m[2];
if(m[4])
out += m[4];
}
} while(m);
return out;
}
function convertVarsProtos(makeExtern)
{
let input = document.getElementById('in').value;
// output = output.replace(/\/\/( +)(\w+) As (\w+).*/gi, "$&\n$1$3 $2;");
// output = output.replace(/\/\/Public (\w+) As (\w+).*/gi, "$&\nextern $2 $1;");
// output = output.replace(/\/\/( +)(\w+)\((\d+) To (\d|\w+)\) As (\w+).*/gi, "$&\n$1RangeArr<$5, $3, $4> $2;");
// output = output.replace(/\/\/Public (\w+)\((\d+) To (\d|\w+)\) As (\w+).*/gi, "$&\nextern RangeArr<$4, $2, $3> $1;");
// output = output.replace(/^(?!\/\/)(.*)(String) (.*)/gm, "$1std::string $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(String),(.*)/gm, "$1RangeArr<std::string,$3");
// output = output.replace(/^(?!\/\/)(.*)(Integer) (.*)/gm, "$1int $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(Integer),(.*)/gm, "$1RangeArr<int,$3");
// output = output.replace(/^(?!\/\/)(.*)(Long) (.*)/gm, "$1int $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(Long),(.*)/gm, "$1RangeArr<long,$3");
// output = output.replace(/^(?!\/\/)(.*)(Boolean) (.*)/gm, "$1bool $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(Boolean),(.*)/gm, "$1RangeArr<bool,$3");
// output = output.replace(/^(?!\/\/)(.*)(Single) (.*)/gm, "$1float $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(Single),(.*)/gm, "$1RangeArr<float,$3");
// output = output.replace(/^(?!\/\/)(.*)(Double) (.*)/gm, "$1double $3");
// output = output.replace(/^(?!\/\/)(.*)RangeArr<(Double),(.*)/gm, "$1RangeArr<double,$3");
let re = /^( +|Public +|Private +|Dim +|)(\w+)\(?(.*)?\)? As (\w+).*/gim;
let output = "";
let m = null;
do
{
m = re.exec(input);
if(m)
{
output += "// " + m[0] + "\n";
let out = "";
let type = replaceType(m[4]);
if(m[1] === "private")
out += "/*PRIVATE*/ ";
if(makeExtern)
out += "extern ";
if(m[3])
out += arrayDeclare(type, m[3]) + " ";
else
out += type + " ";
out += m[2];
if(!makeExtern && !m[3])
out += defaultValue(type);
out += ";";
output += out + "\n";
}
} while(m);
document.getElementById('out').value = output;
}
function extractFunctionsAndSubs()
{
let input = document.getElementById('in').value;
let re = /^(Public|Private)? ?(?:Sub|Function) (\w+)\(.*\)(.*)/gim;
let output = "";
let m = null;
do
{
m = re.exec(input);
if(m)
{
let re_f = /(Public|Private)? ?Function +(\w+)\((.*)\) As (\w+)(.*)/g;
let re_s = /(Public|Private)? ?Sub +(\w+)\((.*)\)(.*)/g;
let mf = re_f.exec(m[0]);
let ms = re_s.exec(m[0]);
let out = "";
output += "// " + m[0] + "\n";
if(mf)
{
out += convertComment(mf[5]);
if(mf[1] === "Private")
out += "/*Private*/ ";
out += replaceType(mf[4]);
out += " " + mf[2] + "(" + convertArgs(mf[3]) + ");";
output += out + "\n";
}
else if(ms)
{
out += convertComment(ms[4]);
if(ms[1] === "Private")
out += "/*Private*/ ";
out += "void " + ms[2] + "(" + convertArgs(ms[3]) + ");";
output += out + "\n";
}
}
} while(m);
document.getElementById('out').value = output;
}
</script>
</head>
<body>
<p>
<textarea id="in" style="width: 800px; height: 600px;">
</textarea><br>
<input type="button" value="Преобразовать прототипы" onclick="convertVarsProtos(false);">
<input type="button" value="Преобразовать прототипы (extern)" onclick="convertVarsProtos(true);">
<input type="button" value="Извлечь прототипы функций" onclick="extractFunctionsAndSubs();">
</p>
<textarea id="out" style="width: 800px; height: 600px;">
</textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment