Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 27 #31

Merged
merged 10 commits into from
Apr 16, 2017
18 changes: 18 additions & 0 deletions ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,24 @@ var nodes = map[string]interface{}{
Referenced: true,
Children: []interface{}{},
},
`0x7f9bc9083d00 <line:91:5, line:97:8> line:91:5 'unsigned short'`: &FieldDecl{
Address: "0x7f9bc9083d00",
Position: "line:91:5, line:97:8",
Position2: "line:91:5",
Name: "",
Type: "unsigned short",
Referenced: false,
Children: []interface{}{},
},
`0x30363a0 <col:18, col:29> __val 'int [2]'`: &FieldDecl{
Address: "0x30363a0",
Position: "col:18, col:29",
Position2: "",
Name: "__val",
Type: "int [2]",
Referenced: false,
Children: []interface{}{},
},

// FloatingLiteral
`0x7febe106f5e8 <col:24> 'double' 1.230000e+00`: &FloatingLiteral{
Expand Down
3 changes: 2 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ func printLine(out *bytes.Buffer, line string, indent int) {

func renderExpression(node interface{}) []string {
if node == nil {
return []string{""}
return []string{"", "unknown54"}
}

if n, ok := node.(ExpressionRenderer); ok {
return n.Render()
}
Expand Down
4 changes: 2 additions & 2 deletions darwin/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type _RuneLocale struct {
}

var _DefaultRuneLocale _RuneLocale = _RuneLocale{
__runetype: [256]uint32{0, 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, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, },
__runetype: [256]uint32{0, 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, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255},
}

func __maskrune(_c C__darwin_ct_rune_t, _f uint32) uint32 {
return _DefaultRuneLocale.__runetype[_c & 0xff] & _f;
return _DefaultRuneLocale.__runetype[_c&0xff] & _f
}

func __tolower(c C__darwin_ct_rune_t) C__darwin_ct_rune_t {
Expand Down
16 changes: 12 additions & 4 deletions field_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type FieldDecl struct {
func parseFieldDecl(line string) *FieldDecl {
groups := groupsFromRegex(
`<(?P<position>.*)>
(?P<position2> [^ ]+)?
(?P<position2> col:\d+| line:\d+:\d+)?
(?P<referenced> referenced)?
(?P<name>\w+?)
(?P<name> \w+?)?
'(?P<type>.+?)'`,
line,
)
Expand All @@ -29,7 +29,7 @@ func parseFieldDecl(line string) *FieldDecl {
Address: groups["address"],
Position: groups["position"],
Position2: strings.TrimSpace(groups["position2"]),
Name: groups["name"],
Name: strings.TrimSpace(groups["name"]),
Type: groups["type"],
Referenced: len(groups["referenced"]) > 0,
Children: []interface{}{},
Expand All @@ -38,19 +38,27 @@ func parseFieldDecl(line string) *FieldDecl {

func (n *FieldDecl) Render() []string {
fieldType := resolveType(n.Type)
name := strings.Replace(n.Name, "used", "", -1)
name := n.Name

//if name == "" {
// return []string{"", "unknown71"}
//}

// Go does not allow the name of a variable to be called "type". For the
// moment I will rename this to avoid the error.
if name == "type" {
name = "type_"
}

// It may have a default value.
suffix := ""
if len(n.Children) > 0 {
suffix = fmt.Sprintf(" = %s", renderExpression(n.Children[0])[0])
}

// NULL is a macro that one rendered looks like "(0)" we have to be
// sensitive to catch this as Go would complain that 0 (int) is not
// compatible with the type we are setting it to.
if suffix == " = (0)" {
suffix = " = nil"
}
Expand Down
34 changes: 27 additions & 7 deletions for_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,42 @@ func parseForStmt(line string) *ForStmt {
func (n *ForStmt) RenderLine(out *bytes.Buffer, functionName string, indent int, returnType string) {
children := n.Children

a := renderExpression(children[0])[0]
// There are always 5 children in a ForStmt, for example:
//
// for ( c = 0 ; c < n ; c++ ) {
// doSomething();
// }
//
// 1. initExpression = BinaryStmt: c = 0
// 2. Not sure what this is for, but it's always nil. There is a panic
// below in case we discover what it is used for (pun intended).
// 3. conditionalExpression = BinaryStmt: c < n
// 4. stepExpression = BinaryStmt: c++
// 5. body = CompoundStmt: { CallExpr }

if len(children) != 5 {
panic(fmt.Sprintf("Expected 5 children in ForStmt, got %#v", children))
}

// TODO: The second child of a ForStmt appears to always be null.
// Are there any cases where it is used?
if children[1] != nil {
panic("non-nil child 1 in ForStmt")
}
b := renderExpression(children[2])[0]
c := renderExpression(children[3])[0]

if a == "" && b == "" && c == "" {
printLine(out, fmt.Sprintf("for {"), indent)
init := renderExpression(children[0])[0]
conditional := renderExpression(children[2])[0]
step := renderExpression(children[3])[0]
body := children[4]

if init == "" && conditional == "" && step == "" {
printLine(out, "for {", indent)
} else {
printLine(out, fmt.Sprintf("for %s; %s; %s {", a, b, c), indent)
printLine(out, fmt.Sprintf("for %s; %s; %s {",
init, conditional, step), indent)
}

Render(out, children[4], functionName, indent+1, returnType)
Render(out, body, functionName, indent+1, returnType)

printLine(out, "}", indent)
}
55 changes: 47 additions & 8 deletions if_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,57 @@ func parseIfStmt(line string) *IfStmt {
}

func (n *IfStmt) RenderLine(out *bytes.Buffer, functionName string, indent int, returnType string) {
// TODO: The first two children of an IfStmt appear to always be null.
// Are there any cases where they are used?
children := n.Children[2:]
children := n.Children

e := renderExpression(children[0])
printLine(out, fmt.Sprintf("if %s {", cast(e[0], e[1], "bool")), indent)
// There is always 4 or 5 children in an IfStmt. For example:
//
// if (i == 0) {
// return 0;
// } else {
// return 1;
// }
//
// 1. Not sure what this is for. This gets removed.
// 2. Not sure what this is for.
// 3. conditional = BinaryOperator: i == 0
// 4. body = CompoundStmt: { return 0; }
// 5. elseBody = CompoundStmt: { return 1; }
//
// elseBody will be nil if there is no else clause.

Render(out, children[1], functionName, indent+1, returnType)
// On linux I have seen only 4 children for an IfStmt with the same
// definitions above, but missing the first argument. Since we don't
// know what the first argument is for anyway we will just remove it on
// Mac if necessary.
if len(children) == 5 && children[0] != nil {
panic("non-nil child 0 in ForStmt")
}
if len(children) == 5 {
children = children[1:]
}

// From here on there must be 4 children.
if len(children) != 4 {
panic(fmt.Sprintf("Expected 4 children in IfStmt, got %#v", children))
}

// Maybe we will discover what the nil value is?
if children[0] != nil {
panic("non-nil child 0 in ForStmt")
}

conditional := renderExpression(children[1])

// The condition in Go must always be a bool.
boolCondition := cast(conditional[0], conditional[1], "bool")

printLine(out, fmt.Sprintf("if %s {", boolCondition), indent)

Render(out, children[2], functionName, indent+1, returnType)

if len(children) > 2 {
if children[3] != nil {
printLine(out, "} else {", indent)
Render(out, children[2], functionName, indent+1, returnType)
Render(out, children[3], functionName, indent+1, returnType)
}

printLine(out, "}", indent)
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,6 @@ func main() {
flag.Usage()
os.Exit(1)
}

fmt.Print(Start(flag.Args()))
}
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func TestIntegrationScripts(t *testing.T) {
}

for _, file := range files {
Start([]string{"", file})
Start([]string{file})
}
}
42 changes: 21 additions & 21 deletions noarch/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,41 @@ package noarch
type __builtin_va_list int64

func BoolToInt(x bool) int {
if x {
return 1
}
if x {
return 1
}

return 0
return 0
}

func __bool_to_uint32(x bool) int {
if x {
return 1
}
if x {
return 1
}

return 0
return 0
}

func __not_uint32(x uint32) uint32 {
if x == 0 {
return 1
}
if x == 0 {
return 1
}

return 0
return 0
}

func NotInt(x int) int {
if x == 0 {
return 1
}
if x == 0 {
return 1
}

return 0
return 0
}

func Ternary(a bool, b, c func () interface{}) interface{} {
if a {
return b()
}
func Ternary(a bool, b, c func() interface{}) interface{} {
if a {
return b()
}

return c()
return c()
}
14 changes: 13 additions & 1 deletion tests/misc/for.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

int main()
{
int i;
int i = 0;

// Missing init
for (; i < 10; i++)
printf("%d\n", i);

// CompountStmt
for (i = 0; i < 10; i++) {
printf("%d\n", i);
}

// Not CompoundStmt
for (i = 0; i < 10; i++)
printf("%d\n", i);

// Infinite loop
int j = 0;
for (;;) {
printf("infinite loop\n");
Expand Down
9 changes: 8 additions & 1 deletion tests/misc/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
int main()
{
int x = 1;


// Without else
if ( x == 1 )
printf("x is equal to one.\n");

// With else
if ( x != 1 )
printf("x is not equal to one.\n");
else
printf("x is equal to one.\n");

return 0;
}