Добавлена поддержка Goroutine
Этот коммит содержится в:
родитель
63b0dc2bc6
коммит
b69475e72c
2 изменённых файлов: 72 добавлений и 3 удалений
|
@ -8,6 +8,8 @@ import (
|
|||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// Service specifies the api logic of transforming a source code format into another target format.
|
||||
|
@ -20,11 +22,14 @@ const (
|
|||
ErrorWorkerReaderIsNil = "Reader should not be nil"
|
||||
// ErrorWorkerWriterIsNil ...
|
||||
ErrorWorkerWriterIsNil = "Writer should not be nil"
|
||||
|
||||
FunctionGoHelperPrefix = "__GoHelper_"
|
||||
)
|
||||
|
||||
var (
|
||||
funcDeclarations []string
|
||||
dlock sync.Mutex
|
||||
funcDeclarations []string
|
||||
helpersForDeclarations []string
|
||||
dlock sync.Mutex
|
||||
)
|
||||
|
||||
// defaultService specifies the api logic of transforming a source code format into another target format.
|
||||
|
@ -69,6 +74,7 @@ func (s *defaultService) Start() error {
|
|||
}
|
||||
|
||||
funcDeclarations = nil
|
||||
helpersForDeclarations = nil
|
||||
|
||||
// Start a transpile with an individual channel for each declaration in the source file.
|
||||
go func() {
|
||||
|
@ -84,6 +90,7 @@ func (s *defaultService) Start() error {
|
|||
}
|
||||
|
||||
printFunctionDeclarations(s.out)
|
||||
printGoHelperDeclarations(s.out)
|
||||
|
||||
// Print the ordered result.
|
||||
for i := 0; i < count; i++ {
|
||||
|
@ -115,6 +122,24 @@ func printFunctionDeclarations(out io.Writer) {
|
|||
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 {
|
||||
code := handleAssignStmtExpr(as.Lhs)
|
||||
code += as.Tok.String()
|
||||
|
@ -184,6 +209,18 @@ func handleDeclStmt(stmt *ast.DeclStmt) string {
|
|||
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 {
|
||||
code := ""
|
||||
switch e := expr.(type) {
|
||||
|
@ -445,6 +482,8 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
|||
code += handleCaseClause(s)
|
||||
case *ast.DeclStmt:
|
||||
code += handleDeclStmt(s)
|
||||
case *ast.GoStmt:
|
||||
code += handleGoStmt(s)
|
||||
case *ast.ExprStmt:
|
||||
code += handleExprStmt(s)
|
||||
code += ";"
|
||||
|
@ -456,6 +495,9 @@ func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
|||
code += handleSwitchStmt(s)
|
||||
case *ast.ReturnStmt:
|
||||
code += handleReturnStmt(s)
|
||||
default:
|
||||
spew.Dump(stmt)
|
||||
panic("handleStmt: unknown type")
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
func TestUtils(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
gomega_format.CharactersAroundMismatchToInclude = 20
|
||||
gomega_format.CharactersAroundMismatchToInclude = 50
|
||||
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() {
|
||||
It("Объявление void функции", func() {
|
||||
source := `package test
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче