I want to translate expressions (conditions) written in VisualBasic style to JavaScript code. Because I haven't found a library that does this, I'll have to do it myself. Or does anyone know a project that I could use?
My requirement:
I get a string from a file with an expression written in VB. It is not a full VB code, but only single VB functions like IF, TRIM, LEFT, etc. and some other functions like RECORD_NR() (I think this is not a native function of vb).
I now want to translate this string into JavaScript (can also come as a string, as I write the JavaScript code into a file afterwards).
One of the problems is that all characters in quotes must not be handled functionally.
Example expression:
IF(RECORD_NR() = 1, "NR,1",IF(RECORD_NR() = 2, "NR,2", "NR3"))
Possible result:
var result = "";
if(record.index == 1){
result = "NR,1";
} else {
if(record.index == 2){
result = "NR,2";
} else {
result = "NR3";
}
}
The following is the approach I am currently using and I am not sure if it will get me to my goal:
- Search areas which are in quotes and store position informations (start and end) in json object (
jsonQuotes). - Search for ranges in brackets and save the positions (start and end) in a json object (
jsonBrackets). Here I will exclude ranges in quotes based onjsonQuotes. - Search keywords (function names) which are not in quotes and add found keyword to json object
jsonBrackets(so I know which function belongs to the bracketed area).
At this point I have the following json object:
[
{
"ID": 1,
"start": 2,
"end": 60,
"fnc": "IF"
},
{
"ID": 2,
"start": 12,
"end": 13,
"fnc": "RECORD_NR"
},
{
"ID": 3,
"start": 29,
"end": 59,
"fnc": "IF"
},
{
"ID": 4,
"start": 39,
"end": 40,
"fnc": "RECORD_NR"
}
]
And now I am struggling. I want to sort the json object or nest it like:
[
{
"ID": 1,
"start": 2,
"end": 60,
"fnc": "IF",
"contains":[
{
"ID": 2,
"start": 12,
"end": 13,
"fnc": "RECORD_NR",
"contains":[]
},
{
"ID": 3,
"start": 29,
"end": 59,
"fnc": "IF",
"contains":[
{
"ID": 4,
"start": 39,
"end": 40,
"fnc": "RECORD_NR",
"contains":[]
}
]
}
]
}
]
Can someone help me to achieve that or better does anyone know a better way to get the desired result (maybe a different, better approach)?