From 317a63357d48184d04c0bc54c2fbada4d94f8fd4 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Mon, 1 Apr 2019 11:52:40 +0300 Subject: [PATCH 1/4] preparing --- transpiler/gofmt.go | 46 ++++++++++++++++++++++++++++++++++++++++ transpiler/transpiler.go | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 transpiler/gofmt.go diff --git a/transpiler/gofmt.go b/transpiler/gofmt.go new file mode 100644 index 00000000..7a23ee7d --- /dev/null +++ b/transpiler/gofmt.go @@ -0,0 +1,46 @@ +package transpiler + +import ( + "fmt" + "os" + "strings" +) + +// Examples: +// from: +// var di [][][]byte = [][][]byte{[][]byte{[]byte("cq\x00"), []byte(";\x00")}, [][]byte{[]byte("pl\x00"), []byte("+\x00")}, [][]byte{[]byte("hy\x00"), []byte("-\x00")}, [][]byte{[]byte("sl\x00"), []byte("/\x00")}} +// to: +// var di [][][]byte = [][][]byte{ +// [][]byte{[]byte("cq\x00"), []byte(";\x00")}, +// [][]byte{[]byte("pl\x00"), []byte("+\x00")}, +// [][]byte{[]byte("hy\x00"), []byte("-\x00")}, +// [][]byte{[]byte("sl\x00"), []byte("/\x00")}, +// } +// +// from: +// noarch.Printf([]byte("%d not ok - %s:%d: \x00"), current_test, []byte("/home/konstantin/go/src/github.com/Konstantin8105/c4go/tests/init.c\x00"), 13) +// to: +// noarch.Printf( +// []byte("%d not ok - %s:%d: \x00"), +// current_test, +// []byte("/home/konstantin/go/src/github.com/Konstantin8105/c4go/tests/init.c\x00"), +// 13, +// ) + +func addBreaklines(src string) string { + // parens + // * () + // * [] + // * {} + // * "" + + // separate long lines by comma + // lines := strings.Split(src, "\n") + // for i := range lines { + // if len(lines[i]) > 100 { + // fmt.Fprintf(os.Stdout, "%s\n", lines[i]) + // } + // } + + return src +} diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go index d13c7fb1..91d2e3c1 100644 --- a/transpiler/transpiler.go +++ b/transpiler/transpiler.go @@ -158,6 +158,8 @@ func TranspileAST(fileName, packageName string, withOutsideStructs bool, source = src } + source = addBreaklines(source) + return } From 2bc03a6ca6c9b04f9a6718b699b92b9a351f2fa4 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Tue, 2 Apr 2019 16:24:53 +0300 Subject: [PATCH 2/4] prepare --- transpiler/gofmt.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/transpiler/gofmt.go b/transpiler/gofmt.go index 7a23ee7d..f231fc6c 100644 --- a/transpiler/gofmt.go +++ b/transpiler/gofmt.go @@ -27,20 +27,44 @@ import ( // 13, // ) +const maxLineSymbol int = 100 + func addBreaklines(src string) string { + // separate long lines by comma + lines := strings.Split(src, "\n") + for i := range lines { + line := lines[i] + if len(line) > maxLineSymbol { + line = formatting(line) + } + } + + return strings.Join(lines, "\n") +} + +func formatting(line string) string { // parens // * () // * [] // * {} // * "" + parens := []struct { + name string + from, to byte + }{ + {"p1", '(', ')'}, + {"p2", '[', ']'}, + {"p3", '{', '}'}, + {"p4", '"', '"'}, + } - // separate long lines by comma - // lines := strings.Split(src, "\n") - // for i := range lines { - // if len(lines[i]) > 100 { - // fmt.Fprintf(os.Stdout, "%s\n", lines[i]) - // } + // check last byte + if line[len(line)-1] != '}' || line[len(line)-1] != ')' { + return line + } + + // for i := len(line) - 1; i >= 0; i-- { + // // } - return src } From c2debb8d89f1bcd66891ab263ae9644c5c923d1d Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 3 Apr 2019 11:54:03 +0300 Subject: [PATCH 3/4] step --- transpiler/gofmt.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/transpiler/gofmt.go b/transpiler/gofmt.go index f231fc6c..d241947d 100644 --- a/transpiler/gofmt.go +++ b/transpiler/gofmt.go @@ -1,8 +1,6 @@ package transpiler import ( - "fmt" - "os" "strings" ) @@ -63,8 +61,13 @@ func formatting(line string) string { return line } - // for i := len(line) - 1; i >= 0; i-- { - // - // } + level := 0 + for i := len(line) - 1; i >= 0; i-- { + symbol := line[i] + for j := range parens { + if symbol == parens[j].to { + } + } + } } From d124df1bbba22a4a3ad97d6e48a34367dda8b881 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Wed, 3 Apr 2019 17:02:43 +0300 Subject: [PATCH 4/4] step --- transpiler/gofmt.go | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/transpiler/gofmt.go b/transpiler/gofmt.go index d241947d..c6af71b1 100644 --- a/transpiler/gofmt.go +++ b/transpiler/gofmt.go @@ -46,28 +46,34 @@ func formatting(line string) string { // * [] // * {} // * "" - parens := []struct { - name string - from, to byte - }{ - {"p1", '(', ')'}, - {"p2", '[', ']'}, - {"p3", '{', '}'}, - {"p4", '"', '"'}, - } + // separator : + // * , // check last byte - if line[len(line)-1] != '}' || line[len(line)-1] != ')' { + if line[len(line)-1] != '}' && line[len(line)-1] != ')' { + return line + } + if !strings.Contains(line, "=") { return line } - level := 0 - for i := len(line) - 1; i >= 0; i-- { - symbol := line[i] - for j := range parens { - if symbol == parens[j].to { - } + // initialization levels + levels := make([]int, len(line)) + for i := 0; i < len(line); i++ { + levels[i] = -1 + } + + // add levels for parens - " + for i, isInside := 0, false; i < len(line); i++ { + if isInside { + levels[i] = 0 + } + if line[i] == '"' { + isInside = !isInside } } + _ = levels + + return line }