C/C++: добавлена генерация объявлений всех функций в начале
Этот коммит содержится в:
родитель
a06bea2b20
коммит
63b0dc2bc6
2 изменённых файлов: 159 добавлений и 8 удалений
|
@ -7,6 +7,7 @@ import (
|
||||||
"go/token"
|
"go/token"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service specifies the api logic of transforming a source code format into another target format.
|
// 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"
|
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.
|
// defaultService specifies the api logic of transforming a source code format into another target format.
|
||||||
type defaultService struct {
|
type defaultService struct {
|
||||||
in io.Reader
|
in io.Reader
|
||||||
|
@ -61,16 +67,24 @@ func (s *defaultService) Start() error {
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
dst[i] = make(chan string, 1)
|
dst[i] = make(chan string, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
funcDeclarations = nil
|
||||||
|
|
||||||
// Start a transpile with an individual channel for each declaration in the source file.
|
// Start a transpile with an individual channel for each declaration in the source file.
|
||||||
for i, decl := range file.Decls {
|
go func() {
|
||||||
go handleDecl(i, decl, dst[i], done)
|
for i, decl := range file.Decls {
|
||||||
}
|
handleDecl(i, decl, dst[i], done)
|
||||||
|
}
|
||||||
|
}()
|
||||||
// Wait for all workers are done.
|
// Wait for all workers are done.
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printFunctionDeclarations(s.out)
|
||||||
|
|
||||||
// Print the ordered result.
|
// Print the ordered result.
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
for content := range dst[i] {
|
for content := range dst[i] {
|
||||||
|
@ -85,6 +99,22 @@ func (s *defaultService) Start() error {
|
||||||
return nil
|
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 {
|
func handleAssignStmt(as *ast.AssignStmt) string {
|
||||||
code := handleAssignStmtExpr(as.Lhs)
|
code := handleAssignStmtExpr(as.Lhs)
|
||||||
code += as.Tok.String()
|
code += as.Tok.String()
|
||||||
|
@ -197,7 +227,8 @@ func handleFuncDecl(decl ast.Decl) string {
|
||||||
}
|
}
|
||||||
code := ""
|
code := ""
|
||||||
name := ""
|
name := ""
|
||||||
code += handleFuncDeclType(fd.Type)
|
ft := handleFuncDeclType(fd.Type)
|
||||||
|
code += ft
|
||||||
code += " "
|
code += " "
|
||||||
name = handleFuncDeclName(fd.Name)
|
name = handleFuncDeclName(fd.Name)
|
||||||
if name == "NewController" {
|
if name == "NewController" {
|
||||||
|
@ -205,7 +236,14 @@ func handleFuncDecl(decl ast.Decl) string {
|
||||||
}
|
}
|
||||||
code += name
|
code += name
|
||||||
code += "("
|
code += "("
|
||||||
code += handleFuncDeclParams(fd.Type)
|
fp := handleFuncDeclParams(fd.Type)
|
||||||
|
code += fp
|
||||||
|
|
||||||
|
if ft == "" {
|
||||||
|
ft = "int"
|
||||||
|
}
|
||||||
|
addFunctionDeclaration(ft + " " + name + "(" + fp + ");")
|
||||||
|
|
||||||
code += ") {"
|
code += ") {"
|
||||||
code += handleBlockStmt(fd.Body)
|
code += handleBlockStmt(fd.Body)
|
||||||
code += "}"
|
code += "}"
|
||||||
|
|
|
@ -7,10 +7,12 @@ import (
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
gomega_format "github.com/onsi/gomega/format"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUtils(t *testing.T) {
|
func TestUtils(t *testing.T) {
|
||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
|
gomega_format.CharactersAroundMismatchToInclude = 20
|
||||||
RunSpecs(t, "Go Translator")
|
RunSpecs(t, "Go Translator")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +30,11 @@ var _ = Describe("Go Translator", func() {
|
||||||
func foo() {}
|
func foo() {}
|
||||||
func bar() {}
|
func bar() {}
|
||||||
`
|
`
|
||||||
expected := `void foo(){}
|
expected := `
|
||||||
|
void foo();
|
||||||
|
void bar();
|
||||||
|
|
||||||
|
void foo(){}
|
||||||
void bar() {}`
|
void bar() {}`
|
||||||
Compare(source, expected)
|
Compare(source, expected)
|
||||||
})
|
})
|
||||||
|
@ -38,7 +44,11 @@ var _ = Describe("Go Translator", func() {
|
||||||
func foo(x int) {}
|
func foo(x int) {}
|
||||||
func bar(y 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) {}`
|
void bar(int y) {}`
|
||||||
Compare(source, expected)
|
Compare(source, expected)
|
||||||
})
|
})
|
||||||
|
@ -70,6 +80,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
const char* foo = "bar";
|
const char* foo = "bar";
|
||||||
}
|
}
|
||||||
|
@ -84,6 +96,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
char* foo = "bar";
|
char* foo = "bar";
|
||||||
}
|
}
|
||||||
|
@ -98,6 +112,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
bar();
|
bar();
|
||||||
}
|
}
|
||||||
|
@ -112,6 +128,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
bar(1,2,3);
|
bar(1,2,3);
|
||||||
}
|
}
|
||||||
|
@ -126,6 +144,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
bar("foo");
|
bar("foo");
|
||||||
}
|
}
|
||||||
|
@ -140,6 +160,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
foo.Bar(1,"2");
|
foo.Bar(1,"2");
|
||||||
}
|
}
|
||||||
|
@ -156,6 +178,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
x = 1;
|
x = 1;
|
||||||
y = 2;
|
y = 2;
|
||||||
|
@ -174,6 +198,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
x = bar();
|
x = bar();
|
||||||
y = pkg.Bar();
|
y = pkg.Bar();
|
||||||
|
@ -190,6 +216,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
Serial.begin();
|
Serial.begin();
|
||||||
}
|
}
|
||||||
|
@ -204,6 +232,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
foo.Bar(1,"2",LOW);
|
foo.Bar(1,"2",LOW);
|
||||||
}
|
}
|
||||||
|
@ -218,6 +248,8 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void foo();
|
||||||
|
|
||||||
void foo() {
|
void foo() {
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
|
@ -235,6 +267,29 @@ var _ = Describe("Go Translator", func() {
|
||||||
Compare(source, expected)
|
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() {
|
It("Package_Import", func() {
|
||||||
source := `package test
|
source := `package test
|
||||||
import "github.com/andygeiss/esp32-mqtt/api/controller"
|
import "github.com/andygeiss/esp32-mqtt/api/controller"
|
||||||
|
@ -271,6 +326,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (1 == 1) {
|
if (1 == 1) {
|
||||||
|
@ -291,6 +349,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x == 1) {
|
if (x == 1) {
|
||||||
|
@ -311,6 +372,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
if (x() == 1) {
|
if (x() == 1) {
|
||||||
|
@ -332,6 +396,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
const maxX = 1;
|
const maxX = 1;
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -356,6 +423,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
const maxX = 1;
|
const maxX = 1;
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -380,6 +450,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
switch (x) {
|
switch (x) {
|
||||||
|
@ -405,6 +478,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
switch (x) {
|
switch (x) {
|
||||||
|
@ -433,6 +509,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
func Loop() {}
|
func Loop() {}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
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;
|
WiFiClient client;
|
||||||
voidsetup(){}
|
voidsetup(){}
|
||||||
voidloop(){
|
voidloop(){
|
||||||
|
@ -488,6 +571,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
func Loop() {}
|
func Loop() {}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
int analogInPin = A0;
|
int analogInPin = A0;
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {}
|
void loop() {}
|
||||||
|
@ -507,6 +593,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
void MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
@ -526,6 +616,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
void MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
@ -545,6 +639,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
int MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
@ -564,6 +662,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
int MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
@ -584,6 +686,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
int MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
}
|
}
|
||||||
|
@ -605,6 +711,10 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
int MyFunction();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
int x = MyFunction();
|
int x = MyFunction();
|
||||||
|
@ -629,6 +739,9 @@ var _ = Describe("Go Translator", func() {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
expected := `
|
expected := `
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
|
||||||
void setup() {}
|
void setup() {}
|
||||||
void loop() {
|
void loop() {
|
||||||
int i;
|
int i;
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче