Skip to content

Commit

Permalink
Do not wrap assignement expressions thats are statements
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Aug 12, 2017
1 parent 7a7f13d commit 87517d7
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 77 deletions.
30 changes: 17 additions & 13 deletions transpiler/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func transpileBinaryOperatorComma(n *ast.BinaryOperator, p *program.Program) (
return right[0], preStmts, nil
}

func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program, exprIsStmt bool) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
Expand Down Expand Up @@ -95,7 +95,10 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
bComma.Type = c.Type
bComma.AddChild(c)
bComma.AddChild(&bSecond)
return transpileBinaryOperator(&bComma, p)

// exprIsStmt now changes to false to stop any AST children from
// not being safely wrapped in a closure.
return transpileBinaryOperator(&bComma, p, false)
}
}
}
Expand Down Expand Up @@ -124,14 +127,14 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
// | `-ImplicitCastExpr 0x21a7898 <col:6> 'int' <LValueToRValue>
// | `-DeclRefExpr 0x21a7870 <col:6> 'int' lvalue Var 0x21a7748 'y' 'int'
if getTokenForOperator(n.Operator) == token.COMMA {
stmts, st, newPre, newPost, err := transpileToExpr(n.Children[0], p)
stmts, st, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "unknown50", nil, nil, err
}
preStmts = append(preStmts, newPre...)
preStmts = append(preStmts, util.NewExprStmt(stmts))
postStmts = append(postStmts, newPost...)
stmts, st, newPre, newPost, err = transpileToExpr(n.Children[1], p)
stmts, st, newPre, newPost, err = transpileToExpr(n.Children[1], p, false)
if err != nil {
return nil, "unknown51", nil, nil, err
}
Expand All @@ -140,14 +143,14 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
return stmts, st, preStmts, postStmts, nil
}

left, leftType, newPre, newPost, err := transpileToExpr(n.Children[0], p)
left, leftType, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "unknown52", nil, nil, err
}

preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)

right, rightType, newPre, newPost, err := transpileToExpr(n.Children[1], p)
right, rightType, newPre, newPost, err := transpileToExpr(n.Children[1], p, false)
if err != nil {
return nil, "unknown53", nil, nil, err
}
Expand All @@ -174,8 +177,9 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
p.AddMessage(ast.GenerateWarningMessage(err, n))
}

return util.NewBinaryExpr(left, operator, right, resolvedLeftType), "bool",
preStmts, postStmts, nil
expr := util.NewBinaryExpr(left, operator, right, resolvedLeftType, exprIsStmt)

return expr, "bool", preStmts, postStmts, nil
}

// The right hand argument of the shift left or shift right operators
Expand All @@ -189,8 +193,8 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
right = util.NewNil()
}

return util.NewBinaryExpr(left, operator, right, "uint64"), leftType,
preStmts, postStmts, nil
return util.NewBinaryExpr(left, operator, right, "uint64", exprIsStmt),
leftType, preStmts, postStmts, nil
}

if operator == token.NEQ || operator == token.EQL {
Expand All @@ -212,7 +216,7 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
allocSize := GetAllocationSizeNode(n.Children[1])

if allocSize != nil {
allocSizeExpr, _, newPre, newPost, err := transpileToExpr(allocSize, p)
allocSizeExpr, _, newPre, newPost, err := transpileToExpr(allocSize, p, false)
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)

if err != nil {
Expand All @@ -237,7 +241,7 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
right = util.NewCallExpr(
"make",
util.NewTypeIdent(toType),
util.NewBinaryExpr(allocSizeExpr, token.QUO, util.NewIntLit(elementSize), "int"),
util.NewBinaryExpr(allocSizeExpr, token.QUO, util.NewIntLit(elementSize), "int", false),
)
} else {
right, err = types.CastExpr(p, right, rightType, returnType)
Expand Down Expand Up @@ -292,7 +296,7 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program) (
p.AddMessage(ast.GenerateWarningMessage(err, n))
}

return util.NewBinaryExpr(left, operator, right, resolvedLeftType),
return util.NewBinaryExpr(left, operator, right, resolvedLeftType, exprIsStmt),
types.ResolveTypeForBinaryOperator(p, n.Operator, leftType, rightType),
preStmts, postStmts, nil
}
Expand Down
4 changes: 2 additions & 2 deletions transpiler/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (
panic("non-nil child 0 in IfStmt")
}

conditional, conditionalType, newPre, newPost, err := transpileToExpr(children[1], p)
conditional, conditionalType, newPre, newPost, err := transpileToExpr(children[1], p, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func transpileForStmt(n *ast.ForStmt, p *program.Program) (
if children[2] != nil {
var conditionType string
var newPre, newPost []goast.Stmt
condition, conditionType, newPre, newPost, err = transpileToExpr(children[2], p)
condition, conditionType, newPre, newPost, err = transpileToExpr(children[2], p, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion transpiler/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) (
argTypes := []string{}
i := 0
for _, arg := range n.Children[1:] {
e, eType, newPre, newPost, err := transpileToExpr(arg, p)
e, eType, newPre, newPost, err := transpileToExpr(arg, p, false)
if err != nil {
return nil, "unknown2", nil, nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions transpiler/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) (
util.NewTypeIdent("os."+util.Ucfirst(name[2:len(name)-1])),
),
"*noarch.File",
true,
),
)

Expand All @@ -248,6 +249,7 @@ func transpileVarDecl(p *program.Program, n *ast.VarDecl) (
util.NewTypeIdent("os."+util.Ucfirst(name)),
),
theType,
true,
),
)

Expand Down
4 changes: 3 additions & 1 deletion transpiler/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ func ctypeEnumValue(value int, t token.Token) goast.Expr {
token.SHL,
util.NewIntLit(value),
"int",
false,
),
},
t,
util.NewIntLit(8),
"int",
false,
),
}
}
Expand Down Expand Up @@ -86,7 +88,7 @@ func transpileEnumConstantDecl(p *program.Program, n *ast.EnumConstantDecl) (
default:
if len(n.Children) > 0 {
var err error
value, _, preStmts, postStmts, err = transpileToExpr(n.Children[0], p)
value, _, preStmts, postStmts, err = transpileToExpr(n.Children[0], p, false)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion transpiler/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func transpileReturnStmt(n *ast.ReturnStmt, p *program.Program) (
return &goast.ReturnStmt{}, nil, nil, nil
}

e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p)
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions transpiler/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ func transpileConditionalOperator(n *ast.ConditionalOperator, p *program.Program
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}

a, aType, newPre, newPost, err := transpileToExpr(n.Children[0], p)
a, aType, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}

preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)

b, bType, newPre, newPost, err := transpileToExpr(n.Children[1], p)
b, bType, newPre, newPost, err := transpileToExpr(n.Children[1], p, false)
if err != nil {
return nil, "", nil, nil, err
}

preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)

c, cType, newPre, newPost, err := transpileToExpr(n.Children[2], p)
c, cType, newPre, newPost, err := transpileToExpr(n.Children[2], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func transpileParenExpr(n *ast.ParenExpr, p *program.Program) (
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}

e, eType, newPre, newPost, err := transpileToExpr(n.Children[0], p)
e, eType, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand All @@ -121,13 +121,13 @@ func transpileParenExpr(n *ast.ParenExpr, p *program.Program) (
return r, eType, preStmts, postStmts, nil
}

func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.Program) (
func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.Program, exprIsStmt bool) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
operator := getTokenForOperator(n.Opcode)
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}

right, rightType, newPre, newPost, err := transpileToExpr(n.Children[1], p)
right, rightType, newPre, newPost, err := transpileToExpr(n.Children[1], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.P
argLHS := util.NewCallExpr(getterName)
argOp := getTokenForOperator(binaryOperation)
argRHS := right
argValue := util.NewBinaryExpr(argLHS, argOp, argRHS, "interface{}")
argValue := util.NewBinaryExpr(argLHS, argOp, argRHS, "interface{}", exprIsStmt)

// Make Go expression
resExpr := util.NewCallExpr(setterName, argValue)
Expand All @@ -169,7 +169,7 @@ func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.P
}
}

left, leftType, newPre, newPost, err := transpileToExpr(n.Children[0], p)
left, leftType, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand All @@ -193,7 +193,7 @@ func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.P
p.AddMessage(ast.GenerateWarningMessage(err, n))
}

return util.NewBinaryExpr(left, operator, right, resolvedLeftType),
return util.NewBinaryExpr(left, operator, right, resolvedLeftType, exprIsStmt),
"", preStmts, postStmts, nil
}

Expand Down
4 changes: 2 additions & 2 deletions transpiler/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func transpileSwitchStmt(n *ast.SwitchStmt, p *program.Program) (

// The condition is the expression to be evaulated against each of the
// cases.
condition, _, newPre, newPost, err := transpileToExpr(n.Children[len(n.Children)-2], p)
condition, _, newPre, newPost, err := transpileToExpr(n.Children[len(n.Children)-2], p, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func transpileCaseStmt(n *ast.CaseStmt, p *program.Program) (
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}

c, _, newPre, newPost, err := transpileToExpr(n.Children[0], p)
c, _, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions transpiler/transpiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod
return err
}

func transpileToExpr(node ast.Node, p *program.Program) (
func transpileToExpr(node ast.Node, p *program.Program, exprIsStmt bool) (
expr goast.Expr,
exprType string,
preStmts []goast.Stmt,
Expand Down Expand Up @@ -88,7 +88,7 @@ func transpileToExpr(node ast.Node, p *program.Program) (
expr, exprType, preStmts, postStmts, err = transpileArraySubscriptExpr(n, p)

case *ast.BinaryOperator:
expr, exprType, preStmts, postStmts, err = transpileBinaryOperator(n, p)
expr, exprType, preStmts, postStmts, err = transpileBinaryOperator(n, p, exprIsStmt)

case *ast.UnaryOperator:
expr, exprType, preStmts, postStmts, err = transpileUnaryOperator(n, p)
Expand All @@ -97,7 +97,7 @@ func transpileToExpr(node ast.Node, p *program.Program) (
expr, exprType, preStmts, postStmts, err = transpileMemberExpr(n, p)

case *ast.ImplicitCastExpr:
expr, exprType, preStmts, postStmts, err = transpileToExpr(n.Children[0], p)
expr, exprType, preStmts, postStmts, err = transpileToExpr(n.Children[0], p, false)

case *ast.DeclRefExpr:
expr, exprType, err = transpileDeclRefExpr(n, p)
Expand All @@ -109,7 +109,7 @@ func transpileToExpr(node ast.Node, p *program.Program) (
expr, exprType, preStmts, postStmts, err = transpileParenExpr(n, p)

case *ast.CStyleCastExpr:
expr, exprType, preStmts, postStmts, err = transpileToExpr(n.Children[0], p)
expr, exprType, preStmts, postStmts, err = transpileToExpr(n.Children[0], p, false)

case *ast.CharacterLiteral:
expr, exprType, err = transpileCharacterLiteral(n), "char", nil
Expand All @@ -118,7 +118,7 @@ func transpileToExpr(node ast.Node, p *program.Program) (
expr, exprType, preStmts, postStmts, err = transpileCallExpr(n, p)

case *ast.CompoundAssignOperator:
return transpileCompoundAssignOperator(n, p)
return transpileCompoundAssignOperator(n, p, exprIsStmt)

case *ast.UnaryExprOrTypeTraitExpr:
return transpileUnaryExprOrTypeTraitExpr(n, p)
Expand Down Expand Up @@ -209,7 +209,7 @@ func transpileToStmt(node ast.Node, p *program.Program) (
}

// We do not care about the return type.
expr, _, preStmts, postStmts, err = transpileToExpr(node, p)
expr, _, preStmts, postStmts, err = transpileToExpr(node, p, false)
if err != nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions transpiler/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func transpileUnaryOperatorInc(n *ast.UnaryOperator, p *program.Program,
argLHS := util.NewCallExpr(getterName)
argOp := binaryOperator
argRHS := util.NewIntLit(1)
argValue := util.NewBinaryExpr(argLHS, argOp, argRHS, "interface{}")
argValue := util.NewBinaryExpr(argLHS, argOp, argRHS, "interface{}", false)

// Make Go expression
resExpr := util.NewCallExpr(setterName, argValue)
Expand All @@ -76,12 +76,12 @@ func transpileUnaryOperatorInc(n *ast.UnaryOperator, p *program.Program,
Children: []ast.Node{},
},
},
}, p)
}, p, false)
}

func transpileUnaryOperatorNot(n *ast.UnaryOperator, p *program.Program) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p)
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func transpileUnaryOperatorNot(n *ast.UnaryOperator, p *program.Program) (
// Dereferencing.
func transpileUnaryOperatorMul(n *ast.UnaryOperator, p *program.Program) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p)
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func transpileUnaryOperator(n *ast.UnaryOperator, p *program.Program) (
}

// Otherwise handle like a unary operator.
e, eType, newPre, newPost, err := transpileToExpr(n.Children[0], p)
e, eType, newPre, newPost, err := transpileToExpr(n.Children[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
Expand Down
Loading

0 comments on commit 87517d7

Please sign in to comment.