Skip to content

Added stepsize in for loop #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions vb2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = "}";
Expand Down