Skip to content

Commit

Permalink
Allow struct inside a function. Fixes #344 and #346 (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 authored and elliotchance committed Nov 30, 2017
1 parent 646311f commit d4b0bbf
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 179 deletions.
9 changes: 8 additions & 1 deletion ast/non_null_attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ type NonNullAttr struct {
A int
B int
C int
D int
ChildNodes []Node
}

func parseNonNullAttr(line string) *NonNullAttr {
groups := groupsFromRegex(
`<(?P<position>.*)>(?P<a> \d+)(?P<b> \d+)?(?P<c> \d+)?`,
`<(?P<position>.*)>(?P<a> \d+)(?P<b> \d+)?(?P<c> \d+)?(?P<d> \d+)?`,
line,
)

Expand All @@ -31,12 +32,18 @@ func parseNonNullAttr(line string) *NonNullAttr {
c = util.Atoi(strings.TrimSpace(groups["c"]))
}

d := 0
if groups["d"] != "" {
d = util.Atoi(strings.TrimSpace(groups["d"]))
}

return &NonNullAttr{
Addr: ParseAddress(groups["address"]),
Pos: NewPositionFromString(groups["position"]),
A: util.Atoi(strings.TrimSpace(groups["a"])),
B: b,
C: c,
D: d,
ChildNodes: []Node{},
}
}
Expand Down
14 changes: 14 additions & 0 deletions ast/non_null_attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestNonNullAttr(t *testing.T) {
A: 1,
B: 0,
C: 0,
D: 0,
ChildNodes: []Node{},
},
`0x2cce280 </sys/cdefs.h:286:44, /bits/mathcalls.h:115:69> 1`: &NonNullAttr{
Expand All @@ -20,6 +21,7 @@ func TestNonNullAttr(t *testing.T) {
A: 1,
B: 0,
C: 0,
D: 0,
ChildNodes: []Node{},
},
`0x201ede0 <line:145:79, col:93> 0`: &NonNullAttr{
Expand All @@ -28,6 +30,7 @@ func TestNonNullAttr(t *testing.T) {
A: 0,
B: 0,
C: 0,
D: 0,
ChildNodes: []Node{},
},
`0x1b89b20 <col:76, col:93> 2 3`: &NonNullAttr{
Expand All @@ -36,6 +39,7 @@ func TestNonNullAttr(t *testing.T) {
A: 2,
B: 3,
C: 0,
D: 0,
ChildNodes: []Node{},
},
`0x55f0219e20d0 <line:717:22, col:42> 0 1 4`: &NonNullAttr{
Expand All @@ -44,6 +48,16 @@ func TestNonNullAttr(t *testing.T) {
A: 0,
B: 1,
C: 4,
D: 0,
ChildNodes: []Node{},
},
`0x248ea60 <line:155:26, col:49> 0 1 2 4`: &NonNullAttr{
Addr: 0x248ea60,
Pos: NewPositionFromString("line:155:26, col:49"),
A: 0,
B: 1,
C: 2,
D: 4,
ChildNodes: []Node{},
},
}
Expand Down
9 changes: 8 additions & 1 deletion tests/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct xx {

int main()
{
plan(14);
plan(15);

struct programming variable;
char *s = "Programming in Software Development.";
Expand Down Expand Up @@ -109,5 +109,12 @@ int main()
x.inner.deep.k = 56;
is_eq(x.i + x.inner.j + x.inner.deep.k, 102);

struct u{
int y;
};
struct u yy;
yy.y = 42;
is_eq(yy.y,42);

done_testing();
}
5 changes: 5 additions & 0 deletions transpiler/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package transpiler

import (
"fmt"
goast "go/ast"
"go/token"
"reflect"
Expand Down Expand Up @@ -34,6 +35,10 @@ func transpileBinaryOperatorComma(n *ast.BinaryOperator, p *program.Program) (
return nil, nil, err
}

if left == nil || right == nil {
return nil, nil, fmt.Errorf("Cannot transpile binary operator comma: right = %v , left = %v", right, left)
}

preStmts = append(preStmts, left...)

return right[0], preStmts, nil
Expand Down
37 changes: 30 additions & 7 deletions transpiler/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (

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

if body == nil {
return nil, nil, nil, fmt.Errorf("Body of If cannot by nil")
}
if boolCondition == nil {
return nil, nil, nil, fmt.Errorf("Bool Condition in If cannot by nil")
}
r := &goast.IfStmt{
Cond: boolCondition,
Body: body,
Expand All @@ -95,16 +101,32 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (

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

r.Else = elseBody
if elseBody != nil {
r.Else = elseBody
} else {
return nil, nil, nil, fmt.Errorf("Body of Else in If cannot be nil")
}
}

return r, newPre, newPost, nil
}

func transpileForStmt(n *ast.ForStmt, p *program.Program) (
*goast.ForStmt, []goast.Stmt, []goast.Stmt, error) {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
f *goast.ForStmt, preStmts []goast.Stmt, postStmts []goast.Stmt, err error) {

// This `defer` is workaround
// Please remove after solving all problems
defer func() {
if f == (*goast.ForStmt)(nil) {
p.AddMessage(p.GenerateWarningMessage(fmt.Errorf("ForStmt cannot be nil"), n))
f = &goast.ForStmt{ // This is workaround
Body: &goast.BlockStmt{
Lbrace: 1,
List: []goast.Stmt{&goast.BranchStmt{Tok: token.BREAK}},
},
}
}
}()

children := n.Children()

Expand Down Expand Up @@ -145,9 +167,7 @@ func transpileForStmt(n *ast.ForStmt, p *program.Program) (
if err != nil {
return nil, nil, nil, err
}
preStmts = append(preStmts, newPre...)
preStmts = append(preStmts, before)
preStmts = append(preStmts, newPost...)
preStmts = append(preStmts, combineStmts(before, newPre, newPost)...)
children[0] = c.Children()[1]
}
case *ast.DeclStmt:
Expand Down Expand Up @@ -283,6 +303,9 @@ func transpileForStmt(n *ast.ForStmt, p *program.Program) (
if err != nil {
return nil, nil, nil, err
}
if body == nil {
return nil, nil, nil, fmt.Errorf("Body of For cannot be nil")
}

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

Expand Down
Loading

0 comments on commit d4b0bbf

Please sign in to comment.