From 63b0dc2bc677a339cd1a513901168cd39627ef3e Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 14 Nov 2022 06:15:32 +0300 Subject: [PATCH] =?UTF-8?q?C/C++:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B9=20=D0=B2=20=D0=BD=D0=B0=D1=87=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile/service.go | 48 +++++++++++++-- transpile/service_test.go | 119 +++++++++++++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 8 deletions(-) diff --git a/transpile/service.go b/transpile/service.go index 7b247bb..67563f8 100644 --- a/transpile/service.go +++ b/transpile/service.go @@ -7,6 +7,7 @@ import ( "go/token" "io" "strings" + "sync" ) // Service specifies the api logic of transforming a source code format into another target format. @@ -21,6 +22,11 @@ const ( ErrorWorkerWriterIsNil = "Writer should not be nil" ) +var ( + funcDeclarations []string + dlock sync.Mutex +) + // defaultService specifies the api logic of transforming a source code format into another target format. type defaultService struct { in io.Reader @@ -61,16 +67,24 @@ func (s *defaultService) Start() error { for i := 0; i < count; i++ { dst[i] = make(chan string, 1) } + + funcDeclarations = nil + // Start a transpile with an individual channel for each declaration in the source file. - for i, decl := range file.Decls { - go handleDecl(i, decl, dst[i], done) - } + go func() { + for i, decl := range file.Decls { + handleDecl(i, decl, dst[i], done) + } + }() // Wait for all workers are done. for i := 0; i < count; i++ { select { case <-done: } } + + printFunctionDeclarations(s.out) + // Print the ordered result. for i := 0; i < count; i++ { for content := range dst[i] { @@ -85,6 +99,22 @@ func (s *defaultService) Start() error { return nil } +func addFunctionDeclaration(f string) { + dlock.Lock() + defer dlock.Unlock() + + funcDeclarations = append(funcDeclarations, f) +} +func printFunctionDeclarations(out io.Writer) { + dlock.Lock() + defer dlock.Unlock() + + for _, f := range funcDeclarations { + out.Write([]byte(f + "\n")) + } + out.Write([]byte("\n")) +} + func handleAssignStmt(as *ast.AssignStmt) string { code := handleAssignStmtExpr(as.Lhs) code += as.Tok.String() @@ -197,7 +227,8 @@ func handleFuncDecl(decl ast.Decl) string { } code := "" name := "" - code += handleFuncDeclType(fd.Type) + ft := handleFuncDeclType(fd.Type) + code += ft code += " " name = handleFuncDeclName(fd.Name) if name == "NewController" { @@ -205,7 +236,14 @@ func handleFuncDecl(decl ast.Decl) string { } code += name code += "(" - code += handleFuncDeclParams(fd.Type) + fp := handleFuncDeclParams(fd.Type) + code += fp + + if ft == "" { + ft = "int" + } + addFunctionDeclaration(ft + " " + name + "(" + fp + ");") + code += ") {" code += handleBlockStmt(fd.Body) code += "}" diff --git a/transpile/service_test.go b/transpile/service_test.go index e40cad3..7d5cf87 100644 --- a/transpile/service_test.go +++ b/transpile/service_test.go @@ -7,10 +7,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + gomega_format "github.com/onsi/gomega/format" ) func TestUtils(t *testing.T) { RegisterFailHandler(Fail) + gomega_format.CharactersAroundMismatchToInclude = 20 RunSpecs(t, "Go Translator") } @@ -28,7 +30,11 @@ var _ = Describe("Go Translator", func() { func foo() {} func bar() {} ` - expected := `void foo(){} + expected := ` + void foo(); + void bar(); + + void foo(){} void bar() {}` Compare(source, expected) }) @@ -38,7 +44,11 @@ var _ = Describe("Go Translator", func() { func foo(x int) {} func bar(y int) {} ` - expected := `void foo(int x){} + expected := ` + void foo(int x); + void bar(int y); + + void foo(int x){} void bar(int y) {}` Compare(source, expected) }) @@ -70,6 +80,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { const char* foo = "bar"; } @@ -84,6 +96,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { char* foo = "bar"; } @@ -98,6 +112,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { bar(); } @@ -112,6 +128,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { bar(1,2,3); } @@ -126,6 +144,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { bar("foo"); } @@ -140,6 +160,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { foo.Bar(1,"2"); } @@ -156,6 +178,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { x = 1; y = 2; @@ -174,6 +198,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { x = bar(); y = pkg.Bar(); @@ -190,6 +216,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { Serial.begin(); } @@ -204,6 +232,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { foo.Bar(1,"2",LOW); } @@ -218,6 +248,8 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void foo(); + void foo() { Serial.println(WiFi.localIP()); } @@ -235,6 +267,29 @@ var _ = Describe("Go Translator", func() { Compare(source, expected) }) + It("Functions get declarations first", func() { + source := `package test + func foo() { + foo2() + } + func foo2() int { + return 0 + } + ` + expected := ` + void foo(); + int foo2(); + + void foo() { + foo2(); + } + foo2() { + return 0; + } + ` + Compare(source, expected) + }) + It("Package_Import", func() { source := `package test import "github.com/andygeiss/esp32-mqtt/api/controller" @@ -271,6 +326,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { if (1 == 1) { @@ -291,6 +349,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { if (x == 1) { @@ -311,6 +372,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { if (x() == 1) { @@ -332,6 +396,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + const maxX = 1; void setup() {} void loop() { @@ -356,6 +423,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + const maxX = 1; void setup() {} void loop() { @@ -380,6 +450,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { switch (x) { @@ -405,6 +478,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { switch (x) { @@ -433,6 +509,9 @@ var _ = Describe("Go Translator", func() { func Loop() {} ` expected := ` + void setup(); + void loop(); + #include void setup() { Serial.begin(115200); @@ -463,7 +542,11 @@ var _ = Describe("Go Translator", func() { } } ` - expected := `#include + expected := ` + void setup(); + void loop(); + + #include WiFiClient client; voidsetup(){} voidloop(){ @@ -488,6 +571,9 @@ var _ = Describe("Go Translator", func() { func Loop() {} ` expected := ` + void setup(); + void loop(); + int analogInPin = A0; void setup() {} void loop() {} @@ -507,6 +593,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void MyFunction(); + void setup() {} void loop() { } @@ -526,6 +616,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void MyFunction(); + void setup() {} void loop() { } @@ -545,6 +639,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + int MyFunction(); + void setup() {} void loop() { } @@ -564,6 +662,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + int MyFunction(); + void setup() {} void loop() { } @@ -584,6 +686,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + int MyFunction(); + void setup() {} void loop() { } @@ -605,6 +711,10 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + int MyFunction(); + void setup() {} void loop() { int x = MyFunction(); @@ -629,6 +739,9 @@ var _ = Describe("Go Translator", func() { } ` expected := ` + void setup(); + void loop(); + void setup() {} void loop() { int i;