C/C++: добавлена генерация объявлений всех функций в начале
Этот коммит содержится в:
родитель
a06bea2b20
коммит
63b0dc2bc6
2 изменённых файлов: 159 добавлений и 8 удалений
|
@ -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 += "}"
|
||||
|
|
|
@ -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 <WiFi.h>
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
@ -463,7 +542,11 @@ var _ = Describe("Go Translator", func() {
|
|||
}
|
||||
}
|
||||
`
|
||||
expected := `#include <WiFi.h>
|
||||
expected := `
|
||||
void setup();
|
||||
void loop();
|
||||
|
||||
#include <WiFi.h>
|
||||
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;
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче