Добавлена поддержка Goroutine
Этот коммит содержится в:
родитель
63b0dc2bc6
коммит
b69475e72c
2 изменённых файлов: 72 добавлений и 3 удалений
|
@ -8,6 +8,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
|
@ -20,10 +22,13 @@ const (
|
||||||
ErrorWorkerReaderIsNil = "Reader should not be nil"
|
ErrorWorkerReaderIsNil = "Reader should not be nil"
|
||||||
// ErrorWorkerWriterIsNil ...
|
// ErrorWorkerWriterIsNil ...
|
||||||
ErrorWorkerWriterIsNil = "Writer should not be nil"
|
ErrorWorkerWriterIsNil = "Writer should not be nil"
|
||||||
|
|
||||||
|
FunctionGoHelperPrefix = "__GoHelper_"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
funcDeclarations []string
|
funcDeclarations []string
|
||||||
|
helpersForDeclarations []string
|
||||||
dlock sync.Mutex
|
dlock sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -69,6 +74,7 @@ func (s *defaultService) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
funcDeclarations = nil
|
funcDeclarations = nil
|
||||||
|
helpersForDeclarations = 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.
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -84,6 +90,7 @@ func (s *defaultService) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
printFunctionDeclarations(s.out)
|
printFunctionDeclarations(s.out)
|
||||||
|
printGoHelperDeclarations(s.out)
|
||||||
|
|
||||||
// Print the ordered result.
|
// Print the ordered result.
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
@ -115,6 +122,24 @@ func printFunctionDeclarations(out io.Writer) {
|
||||||
out.Write([]byte("\n"))
|
out.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addGoHelperDeclaration(f string) {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
helpersForDeclarations = append(helpersForDeclarations, f)
|
||||||
|
}
|
||||||
|
func printGoHelperDeclarations(out io.Writer) {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
for _, f := range helpersForDeclarations {
|
||||||
|
helper := fmt.Sprintf(`void %v%v(void*) { %v(); vTaskDelete(NULL); }`,
|
||||||
|
FunctionGoHelperPrefix, f, f)
|
||||||
|
out.Write([]byte(helper + "\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()
|
||||||
|
@ -184,6 +209,18 @@ func handleDeclStmt(stmt *ast.DeclStmt) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGoStmt(stmt *ast.GoStmt) string {
|
||||||
|
code := ""
|
||||||
|
switch f := stmt.Call.Fun.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
go_helper := FunctionGoHelperPrefix + f.Name
|
||||||
|
addGoHelperDeclaration(f.Name)
|
||||||
|
code += `xTaskCreate(` + go_helper + `,"",1024,NULL,1,NULL);` + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
func handleExpr(expr ast.Expr) string {
|
func handleExpr(expr ast.Expr) string {
|
||||||
code := ""
|
code := ""
|
||||||
switch e := expr.(type) {
|
switch e := expr.(type) {
|
||||||
|
@ -445,6 +482,8 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
||||||
code += handleCaseClause(s)
|
code += handleCaseClause(s)
|
||||||
case *ast.DeclStmt:
|
case *ast.DeclStmt:
|
||||||
code += handleDeclStmt(s)
|
code += handleDeclStmt(s)
|
||||||
|
case *ast.GoStmt:
|
||||||
|
code += handleGoStmt(s)
|
||||||
case *ast.ExprStmt:
|
case *ast.ExprStmt:
|
||||||
code += handleExprStmt(s)
|
code += handleExprStmt(s)
|
||||||
code += ";"
|
code += ";"
|
||||||
|
@ -456,6 +495,9 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
||||||
code += handleSwitchStmt(s)
|
code += handleSwitchStmt(s)
|
||||||
case *ast.ReturnStmt:
|
case *ast.ReturnStmt:
|
||||||
code += handleReturnStmt(s)
|
code += handleReturnStmt(s)
|
||||||
|
default:
|
||||||
|
spew.Dump(stmt)
|
||||||
|
panic("handleStmt: unknown type")
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
func TestUtils(t *testing.T) {
|
func TestUtils(t *testing.T) {
|
||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
gomega_format.CharactersAroundMismatchToInclude = 20
|
gomega_format.CharactersAroundMismatchToInclude = 50
|
||||||
RunSpecs(t, "Go Translator")
|
RunSpecs(t, "Go Translator")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -582,6 +582,33 @@ var _ = Describe("Go Translator", func() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("Goroutines", func() {
|
||||||
|
It("Go создаёт поток", func() {
|
||||||
|
source := `package test
|
||||||
|
func main() {
|
||||||
|
go myfunction()
|
||||||
|
}
|
||||||
|
func myfunction() {
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expected := `
|
||||||
|
void main();
|
||||||
|
void myfunction();
|
||||||
|
void __GoHelper_myfunction(void*) {
|
||||||
|
myfunction();
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
xTaskCreate(__GoHelper_myfunction,"",1024,NULL,1,NULL);
|
||||||
|
}
|
||||||
|
void myfunction() {
|
||||||
|
}
|
||||||
|
`
|
||||||
|
Compare(source, expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Describe("Функции", func() {
|
Describe("Функции", func() {
|
||||||
It("Объявление void функции", func() {
|
It("Объявление void функции", func() {
|
||||||
source := `package test
|
source := `package test
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче