Skip to content

Commit

Permalink
Split into multiple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Apr 22, 2017
1 parent f92a6b3 commit d9f28f5
Show file tree
Hide file tree
Showing 65 changed files with 563 additions and 311 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/bin
/coverage.txt
/c2go
/.vscode
4 changes: 3 additions & 1 deletion ast/always_inline_attr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ast

import "github.com/elliotchance/c2go/program"

type AlwaysInlineAttr struct {
Address string
Position string
Expand All @@ -19,7 +21,7 @@ func parseAlwaysInlineAttr(line string) *AlwaysInlineAttr {
}
}

func (n *AlwaysInlineAttr) render(ast *Ast) (string, string) {
func (n *AlwaysInlineAttr) render(program *program.Program) (string, string) {
return "", ""
}

Expand Down
12 changes: 8 additions & 4 deletions ast/array_subscript_expr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ast

import "fmt"
import (
"fmt"

"github.com/elliotchance/c2go/program"
)

type ArraySubscriptExpr struct {
Address string
Expand All @@ -25,10 +29,10 @@ func parseArraySubscriptExpr(line string) *ArraySubscriptExpr {
}
}

func (n *ArraySubscriptExpr) render(ast *Ast) (string, string) {
func (n *ArraySubscriptExpr) render(program *program.Program) (string, string) {
children := n.Children
expression, _ := renderExpression(ast, children[0])
index, _ := renderExpression(ast, children[1])
expression, _ := renderExpression(program, children[0])
index, _ := renderExpression(program, children[1])
src := fmt.Sprintf("%s[%s]", expression, index)
return src, "unknown1"
}
Expand Down
6 changes: 5 additions & 1 deletion ast/asm_label_attr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type AsmLabelAttr struct {
Address string
Position string
Expand All @@ -21,7 +25,7 @@ func parseAsmLabelAttr(line string) *AsmLabelAttr {
}
}

func (n *AsmLabelAttr) render(ast *Ast) (string, string) {
func (n *AsmLabelAttr) render(program *program.Program) (string, string) {
return "", ""
}

Expand Down
90 changes: 47 additions & 43 deletions ast/ast.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,16 @@
package ast

import (
"bytes"
"fmt"
"regexp"
"strings"
)

type Ast struct {
imports []string

// for rendering go src
functionName string
indent int
returnType string
}

func NewAst() *Ast {
return &Ast{
imports: []string{"fmt"},
}
}

func (a *Ast) Imports() []string {
return a.imports
}

func (a *Ast) addImport(name string) {
for _, i := range a.imports {
if i == name {
// already imported
return
}
}

a.imports = append(a.imports, name)
}

func (a *Ast) importType(name string) string {
if strings.Index(name, ".") != -1 {
parts := strings.Split(name, ".")
a.addImport(strings.Join(parts[:len(parts)-1], "."))

parts2 := strings.Split(name, "/")
return parts2[len(parts2)-1]
}

return name
}
"github.com/elliotchance/c2go/program"
)

type Node interface {
render(ast *Ast) (string, string)
render(program *program.Program) (string, string)
AddChild(node Node)
}

Expand Down Expand Up @@ -195,3 +157,45 @@ func groupsFromRegex(rx, line string) map[string]string {

return result
}

func printLine(out *bytes.Buffer, line string, indent int) {
out.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat("\t", indent), line))
}

func Render(program *program.Program, node Node) string {
src, _ := node.render(program)
return src
}

func renderExpression(program *program.Program, node Node) (string, string) {
if node == nil {
return "", "unknown54"
}

return node.render(program)
}

func getFunctionParams(f *FunctionDecl) []*ParmVarDecl {
r := []*ParmVarDecl{}
for _, n := range f.Children {
if v, ok := n.(*ParmVarDecl); ok {
r = append(r, v)
}
}

return r
}

func getFunctionReturnType(f string) string {
// The type of the function will be the complete prototype, like:
//
// __inline_isfinitef(float) int
//
// will have a type of:
//
// int (float)
//
// The arguments will handle themselves, we only care about the
// return type ('int' in this case)
return strings.TrimSpace(strings.Split(f, "(")[0])
}
6 changes: 5 additions & 1 deletion ast/availability_attr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type AvailabilityAttr struct {
Address string
Position string
Expand Down Expand Up @@ -40,7 +44,7 @@ func parseAvailabilityAttr(line string) *AvailabilityAttr {
}
}

func (n *AvailabilityAttr) render(ast *Ast) (string, string) {
func (n *AvailabilityAttr) render(program *program.Program) (string, string) {
return "", ""
}

Expand Down
20 changes: 13 additions & 7 deletions ast/binary_operator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package ast

import "fmt"
import (
"fmt"

"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
"github.com/elliotchance/c2go/util"
)

type BinaryOperator struct {
Address string
Expand All @@ -25,21 +31,21 @@ func parseBinaryOperator(line string) *BinaryOperator {
}
}

func (n *BinaryOperator) render(ast *Ast) (string, string) {
func (n *BinaryOperator) render(program *program.Program) (string, string) {
operator := n.Operator

left, leftType := renderExpression(ast, n.Children[0])
right, rightType := renderExpression(ast, n.Children[1])
left, leftType := renderExpression(program, n.Children[0])
right, rightType := renderExpression(program, n.Children[1])

return_type := "bool"
if inStrings(operator, []string{"|", "&", "+", "-", "*", "/"}) {
if util.InStrings(operator, []string{"|", "&", "+", "-", "*", "/"}) {
// TODO: The left and right type might be different
return_type = leftType
}

if operator == "&&" {
left = cast(ast, left, leftType, return_type)
right = cast(ast, right, rightType, return_type)
left = types.Cast(program, left, leftType, return_type)
right = types.Cast(program, right, rightType, return_type)
}

if (operator == "!=" || operator == "==") && right == "(0)" {
Expand Down
6 changes: 5 additions & 1 deletion ast/break_stmt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type BreakStmt struct {
Address string
Position string
Expand All @@ -19,7 +23,7 @@ func parseBreakStmt(line string) *BreakStmt {
}
}

func (n *BreakStmt) render(ast *Ast) (string, string) {
func (n *BreakStmt) render(program *program.Program) (string, string) {
return "break", ""
}

Expand Down
6 changes: 5 additions & 1 deletion ast/builtin_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type BuiltinType struct {
Address string
Type string
Expand All @@ -19,7 +23,7 @@ func parseBuiltinType(line string) *BuiltinType {
}
}

func (n *BuiltinType) render(ast *Ast) (string, string) {
func (n *BuiltinType) render(program *program.Program) (string, string) {
return "", ""
}

Expand Down
8 changes: 6 additions & 2 deletions ast/c_style_cast_expr.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type CStyleCastExpr struct {
Address string
Position string
Expand All @@ -23,9 +27,9 @@ func parseCStyleCastExpr(line string) *CStyleCastExpr {
}
}

func (n *CStyleCastExpr) render(ast *Ast) (string, string) {
func (n *CStyleCastExpr) render(program *program.Program) (string, string) {
children := n.Children
return renderExpression(ast, children[0])
return renderExpression(program, children[0])
}

func (n *CStyleCastExpr) AddChild(node Node) {
Expand Down
13 changes: 8 additions & 5 deletions ast/call_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ast
import (
"fmt"
"strings"

"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
)

type CallExpr struct {
Expand All @@ -26,15 +29,15 @@ func parseCallExpr(line string) *CallExpr {
}
}

func (n *CallExpr) render(ast *Ast) (string, string) {
func (n *CallExpr) render(program *program.Program) (string, string) {
children := n.Children
func_name, _ := renderExpression(ast, children[0])
func_name, _ := renderExpression(program, children[0])

func_def := getFunctionDefinition(func_name)

if func_def.Substitution != "" {
parts := strings.Split(func_def.Substitution, ".")
ast.addImport(strings.Join(parts[:len(parts)-1], "."))
program.AddImport(strings.Join(parts[:len(parts)-1], "."))

parts2 := strings.Split(func_def.Substitution, "/")
func_name = parts2[len(parts2)-1]
Expand All @@ -43,15 +46,15 @@ func (n *CallExpr) render(ast *Ast) (string, string) {
args := []string{}
i := 0
for _, arg := range children[1:] {
e, eType := renderExpression(ast, arg)
e, eType := renderExpression(program, arg)

if i > len(func_def.ArgumentTypes)-1 {
// This means the argument is one of the varargs
// so we don't know what type it needs to be
// cast to.
args = append(args, e)
} else {
args = append(args, cast(ast, e, eType, func_def.ArgumentTypes[i]))
args = append(args, types.Cast(program, e, eType, func_def.ArgumentTypes[i]))
}

i++
Expand Down
32 changes: 0 additions & 32 deletions ast/cast.go

This file was deleted.

6 changes: 5 additions & 1 deletion ast/character_literal.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ast

import (
"github.com/elliotchance/c2go/program"
)

type CharacterLiteral struct {
Address string
Position string
Expand All @@ -23,7 +27,7 @@ func parseCharacterLiteral(line string) *CharacterLiteral {
}
}

func (n *CharacterLiteral) render(ast *Ast) (string, string) {
func (n *CharacterLiteral) render(program *program.Program) (string, string) {
return "", ""
}

Expand Down
Loading

0 comments on commit d9f28f5

Please sign in to comment.