Skip to content

Commit

Permalink
Fixed conversion of for(i; i < max; i++). fixes #285 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
SlavaRa authored Oct 20, 2017
1 parent 5329880 commit 2876ef8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## dev
- Fixed conversion of `for(i; i < max; i++)`. fixes #285
- Fixed conversion of `v_numeric += condition1 || condition2`. fixes #275
- Fixed conversion of `v += condition ? 1 : 0`. fixes #274
- Fixed conversion of `private const FOO : int = 1;`. fixes #255
Expand Down
39 changes: 21 additions & 18 deletions src/as3hx/Writer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1900,11 +1900,13 @@ class Writer
}

inline function writeEFor(inits:Array<Expr>, conds:Array<Expr>, incrs:Array<Expr>, e:Expr):BlockEnd {
//Sys.println('inits: ${inits}; conds: ${conds}; incrs: ${incrs}');
openContext();
var useWhileLoop:Void->Bool = function() {
if (inits.empty() || conds.empty()) return true;
switch(inits[0]) {
case EVars(vars): if (vars.length > 1) return true;
case EVars(vars) if(vars.length > 1): return true;
case EIdent(v): return true;
default:
}
if (conds[0].match(EBinop("&&" | "||", _, _, _))) return true;
Expand Down Expand Up @@ -1943,31 +1945,32 @@ class Writer
}
switch(conds[0]) {
case EBinop(op, e1, e2, nl):
//corne case, for "<=" binop, limit value should be incremented
if (op == "<=") {
switch (e2) {
case EConst(CInt(v)):
//increment int constants
var e = EConst(CInt(Std.string(Std.parseInt(v) + 1)));
writeExpr(e2);
default:
//when var used (like <= array.length), no choice but
//to append "+1"
writeExpr(e2);
write(" + 1");
}
} else {
writeExpr(e2);
switch(op) {
//corne case, for "<=" binop, limit value should be incremented
case "<=":
switch(e2) {
case EConst(CInt(v)):
//increment int constants
var e = EConst(CInt(Std.string(Std.parseInt(v) + 1)));
writeExpr(e2);
case _:
//when var used (like <= array.length), no choice but
//to append "+1"
writeExpr(e2);
write(" + 1");
}
case _: writeExpr(e2);
}
writeCloseStatement();
default:
}
} else {
for (init in inits) {
inits = inits.filter(function(it) return !it.match(EIdent(_)));
for(init in inits) {
writeExpr(init);
writeNL(";");
}
writeIndent();
if(!inits.empty()) writeIndent();
write("while (");
if (conds.empty()) {
write("true");
Expand Down
9 changes: 9 additions & 0 deletions test/issues/Issue285.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package {
public class Issue285 {
public function Issue285() {
var x:int = 1;
var max:int = 10;
for (x; x<max; ++x) {}
}
}
}
13 changes: 13 additions & 0 deletions test/issues/Issue285.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class Issue285
{
public function new()
{
var x : Int = 1;
var max : Int = 10;
while (x < max)
{
++x;
}
}
}
11 changes: 8 additions & 3 deletions test/unit/as3hx/AS3HXTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class AS3HXTest {
generate("Issue230.as", "Issue230.hx");
cfg.dictionaryToHash = false;
}
@Test("flash.display3D.Context3D['supportsVideoTexture'] -> Reflect.field(flash.display3D.Context3D, 'supportsVideoTexture')")
public function issue234() {
generate("Issue234.as", "Issue234.hx");
Expand Down Expand Up @@ -481,7 +481,7 @@ class AS3HXTest {
generate("Issue257.as", "Issue257.hx");
}

@Test("https://github.com/HaxeFoundation/as3hx/issues/274")
@Test("v += condition ? 1 : 0")
public function issue274() {
generate("Issue274.as", "Issue274.hx");
}
Expand All @@ -496,11 +496,16 @@ class AS3HXTest {
generate("Issue277.as", "Issue277.hx");
}

@Test("https://github.com/HaxeFoundation/as3hx/issues/275")
@Test("v_numeric += condition1 || condition2 -> v_numeric = (condition1 || condition2) ? 1 : 0")
public function issue275() {
generate("Issue275.as", "Issue275.hx");
}

@Test("for(i; i < max; i++) -> whil(i < max) { ++i; }")
public function issue285() {
generate("Issue285.as", "Issue285.hx");
}

function generate(as3FileName:String, expectedHaxeFileName:String) {
var issuesDirectory = FileSystem.absolutePath("test/issues");
var generatedDirectoryPath = '$issuesDirectory/generated';
Expand Down

0 comments on commit 2876ef8

Please sign in to comment.