forked from elliotchance/c2go
/
scope.go
56 lines (45 loc) · 1.18 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// This file contains functions for transpiling scopes. A scope is zero or more
// statements between a set of curly brackets.
package transpiler
import (
"fmt"
goast "go/ast"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
)
func transpileCompoundStmt(n *ast.CompoundStmt, p *program.Program) (
*goast.BlockStmt, []goast.Stmt, []goast.Stmt, error) {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
stmts := []goast.Stmt{}
for _, x := range n.Children() {
result, err := transpileToStmts(x, p)
if err != nil {
return nil, nil, nil, err
}
if result != nil {
stmts = append(stmts, result...)
}
}
return &goast.BlockStmt{
List: stmts,
}, preStmts, postStmts, nil
}
func transpileToBlockStmt(node ast.Node, p *program.Program) (
*goast.BlockStmt, []goast.Stmt, []goast.Stmt, error) {
stmts, err := transpileToStmts(node, p)
if err != nil {
return nil, nil, nil, err
}
if len(stmts) == 1 {
if block, ok := stmts[0].(*goast.BlockStmt); ok {
return block, nil, nil, nil
}
}
if stmts == nil {
return nil, nil, nil, fmt.Errorf("Stmts inside Block cannot be nil")
}
return &goast.BlockStmt{
List: stmts,
}, nil, nil, nil
}