diff --git a/README.md b/README.md index 4525980..763377c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Currently, this translator works with: * Basic IF THEN Statements * SELECT CASE Statements * Most Comparison Operators -* Basic FOR x TO y Loops +* Basic FOR x TO y STEP z Loops * Most variations of DO LOOPS * Basic MSGBOX calls @@ -24,7 +24,6 @@ This tool currently does NOT accurately translate * Built in VBA functions * References to Office Objects * WITH Statements -* STEP Constructs in FOR Loops * FOR EACH Loops (in fact these cause the tool to return nothing at all. I'm still working out why that is) I offer the source code up to anyone interested in helping make enhancements to this tool in order to develop a more robust utility for the VBA community. diff --git a/vb2js.js b/vb2js.js index 618345c..303cb79 100644 --- a/vb2js.js +++ b/vb2js.js @@ -92,9 +92,19 @@ function vbsTojs(vbs){ a[i]=a[i].replace(counter,"").replace(from,"").replace(/\bTO\b/i,""); to = a[i].match(/\s*[\w\(\)]+\s*/)[0]; to=to.replace(/=/,"").replace(/\s+/g,""); - a[i] = "for(" + counter + "=" + from + "; " + counter + "<=" + to + "; " + counter + "++){" + // stepsize + if(a[i].search(/\bSTEP\b\s+/i)==-1) a[i] = a[i] + " STEP 1"; + steps = a[i].match(/\bSTEP\b\s*(-?)([*+-\/\s\w\(\)]*)$/i); + steps[1] = steps[1].replace(/\s*/g,"")=="" ? "+" : "-"; + cmpop = (steps[1] == "+") ? "<=" : ">="; + if (steps[2] == "1") { + stepsize = counter + steps[1] + steps[1]; + }else{ + stepsize = (counter + " = " + counter + " " + steps[1] + " " + steps[2]).replace(/\s*/g," "); + } + + a[i] = "for(" + counter + "=" + from + "; " + counter + cmpop + to + "; " + stepsize + "){"; - //NEXT }else if(a[i].search(/^NEXT\b/i)>-1){ a[i] = "}";