Repository: Konstantin8105/c4go
Pull request #265 (2019-02-09 15:13:07)

transpileIf: add operation +=


Patch:
diff --git a/transpiler/branch.go b/transpiler/branch.go
index fd245d5..759ac4b 100644
--- a/transpiler/branch.go
+++ b/transpiler/branch.go
@@ -67,6 +67,15 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (
 
 	// The last parameter must be false because we are transpiling an
 	// expression - assignment operators need to be wrapped in closures.
+	if cao, ok := children[1].(*ast.CompoundAssignOperator); ok {
+		// example:
+		// if ( val += step) {...}
+		var bin ast.BinaryOperator
+		bin.Operator = ","
+		bin.AddChild(children[1])
+		bin.AddChild(cao.Children()[1])
+		children[1] = &bin
+	}
 	conditional, conditionalType, newPre, newPost, err := transpileToExpr(children[1], p, false)
 	if err != nil {
 		err = fmt.Errorf("Cannot transpile for condition. %v", err)
diff --git a/transpiler/switch.go b/transpiler/switch.go
index b7eab16..cccbfde 100644
--- a/transpiler/switch.go
+++ b/transpiler/switch.go
@@ -58,27 +58,38 @@ func transpileSwitchStmt(n *ast.SwitchStmt, p *program.Program) (
 	// default: ...
 checkAgain:
 	for i := range body.Children() {
-		if v, ok := body.Children()[i].(*ast.CaseStmt); ok {
-			if vv, ok := v.Children()[len(v.Children())-1].(*ast.CaseStmt); ok {
-				if len(body.Children()) > i+1 {
-					body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
-				} else {
-					up := body.ChildNodes
-					body.ChildNodes = append(up, vv)
-				}
-				v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
-				goto checkAgain
+		found := false
+		if _, ok := body.Children()[i].(*ast.CaseStmt); ok {
+			found = true
+		}
+		if _, ok := body.Children()[i].(*ast.DefaultStmt); ok {
+			found = true
+		}
+
+		if !found {
+			continue
+		}
+
+		v := body.Children()[i]
+		if vv, ok := v.Children()[len(v.Children())-1].(*ast.CaseStmt); ok {
+			if len(body.Children()) > i+1 {
+				body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
+			} else {
+				up := body.ChildNodes
+				body.ChildNodes = append(up, vv)
 			}
-			if vv, ok := v.Children()[len(v.Children())-1].(*ast.DefaultStmt); ok {
-				if len(body.Children()) > i+1 {
-					body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
-				} else {
-					up := body.ChildNodes
-					body.ChildNodes = append(up, vv)
-				}
-				v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
-				goto checkAgain
+			v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
+			goto checkAgain
+		}
+		if vv, ok := v.Children()[len(v.Children())-1].(*ast.DefaultStmt); ok {
+			if len(body.Children()) > i+1 {
+				body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
+			} else {
+				up := body.ChildNodes
+				body.ChildNodes = append(up, vv)
 			}
+			v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
+			goto checkAgain
 		}
 	}
 
@@ -325,7 +336,12 @@ func appendCaseOrDefaultToNormalizedCases(cases []*goast.CaseClause,
 }
 
 func transpileCaseStmt(n *ast.CaseStmt, p *program.Program) (
-	*goast.CaseClause, []goast.Stmt, []goast.Stmt, error) {
+	_ *goast.CaseClause, _ []goast.Stmt, _ []goast.Stmt, err error) {
+	defer func() {
+		if err != nil {
+			err = fmt.Errorf("Cannot transpileCaseStmt: %v", err)
+		}
+	}()
 	preStmts := []goast.Stmt{}
 	postStmts := []goast.Stmt{}
 
@@ -337,7 +353,6 @@ func transpileCaseStmt(n *ast.CaseStmt, p *program.Program) (
 		c, err = types.CastExpr(p, c, cType, "int")
 		p.AddMessage(p.GenerateWarningMessage(err, n))
 	}
-
 	preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
 
 	stmts, err := transpileStmts(n.Children()[1:], p)


Test patch:
diff --git a/tests/if.c b/tests/if.c
index e5e6f05..3fbdee1 100644
--- a/tests/if.c
+++ b/tests/if.c
@@ -8,7 +8,7 @@ int d(int v)
 
 int main()
 {
-    plan(20);
+    plan(21);
 
     int x = 1;
 
@@ -143,6 +143,16 @@ int main()
 			is_true(l[1] == 2);
 		}
 	}
+	
+	int esct = 0;
+	int esc  = 5;
+	if (esct += esc) {
+		pass("ok"); 
+	} else { 
+		fail("fail");
+	}
+	(void)(esc) ;
+	(void)(esct);
 
     done_testing();
 }
diff --git a/tests/switch.c b/tests/switch.c
index ea4e326..6b1155e 100644
--- a/tests/switch.c
+++ b/tests/switch.c
@@ -333,10 +333,26 @@ void switch_bool()
     }
 }
 
+void switch_char()
+{
+	char form = '1';
+	switch (form) {
+	default:
+	case '1':
+		form = 'S';
+		break;
+	case 0:
+		;
+	};
+	is_eq(form,'S');
+}
+
+
 int main()
 {
-    plan(28);
+    plan(29);
 
+	switch_char();
     switch_bool();
     match_a_single_case();
     fallthrough_to_next_case();