Чистка: Stmt обработчики вынесены в отдельный файл
Этот коммит содержится в:
родитель
b09e560a4d
коммит
5bd52e1421
2 изменённых файлов: 194 добавлений и 189 удалений
|
@ -8,8 +8,6 @@ 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.
|
||||||
|
@ -162,35 +160,6 @@ func addFunctionDeclaration(f string) {
|
||||||
|
|
||||||
funcDeclarations = append(funcDeclarations, f)
|
funcDeclarations = append(funcDeclarations, f)
|
||||||
}
|
}
|
||||||
func addGoHelperDeclaration(f string) {
|
|
||||||
dlock.Lock()
|
|
||||||
defer dlock.Unlock()
|
|
||||||
|
|
||||||
helpersForDeclarations = append(helpersForDeclarations, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleAssignStmt(as *ast.AssignStmt) string {
|
|
||||||
code := handleAssignStmtExpr(as.Lhs)
|
|
||||||
code += as.Tok.String()
|
|
||||||
code += handleAssignStmtExpr(as.Rhs)
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleIncDecStmt(as *ast.IncDecStmt) string {
|
|
||||||
code := handleExpr(as.X)
|
|
||||||
code += as.Tok.String()
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleAssignStmtExpr(e []ast.Expr) string {
|
|
||||||
ops := make([]string, 0)
|
|
||||||
code := ""
|
|
||||||
for _, op := range e {
|
|
||||||
ops = append(ops, handleExpr(op))
|
|
||||||
}
|
|
||||||
code += strings.Join(ops, ",")
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleBasicLit(bl *ast.BasicLit) string {
|
func handleBasicLit(bl *ast.BasicLit) string {
|
||||||
return bl.Value
|
return bl.Value
|
||||||
|
@ -235,27 +204,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
|
||||||
done <- true
|
done <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDeclStmt(stmt *ast.DeclStmt) string {
|
|
||||||
code := ""
|
|
||||||
switch decl := stmt.Decl.(type) {
|
|
||||||
case *ast.GenDecl:
|
|
||||||
code += handleGenDecl(decl)
|
|
||||||
}
|
|
||||||
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) {
|
||||||
|
@ -283,15 +231,6 @@ func handleParenExpr(stmt *ast.ParenExpr) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleExprStmt(stmt *ast.ExprStmt) string {
|
|
||||||
code := ""
|
|
||||||
switch x := stmt.X.(type) {
|
|
||||||
case *ast.CallExpr:
|
|
||||||
code += handleCallExpr(x)
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleFuncDecl(decl ast.Decl) string {
|
func handleFuncDecl(decl ast.Decl) string {
|
||||||
fd := decl.(*ast.FuncDecl)
|
fd := decl.(*ast.FuncDecl)
|
||||||
if shouldSkipFunction(fd.Type) {
|
if shouldSkipFunction(fd.Type) {
|
||||||
|
@ -370,35 +309,6 @@ func handleFuncDeclParams(t *ast.FuncType) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBlockStmt(body *ast.BlockStmt) string {
|
|
||||||
code := ""
|
|
||||||
if body == nil {
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
for _, stmt := range body.List {
|
|
||||||
code += handleStmt(stmt, false)
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleBranchStmt(stmt *ast.BranchStmt) string {
|
|
||||||
return stmt.Tok.String() + ";"
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleCaseClause(cc *ast.CaseClause) string {
|
|
||||||
code := "case "
|
|
||||||
clauses := make([]string, 0)
|
|
||||||
for _, clause := range cc.List {
|
|
||||||
clauses = append(clauses, handleExpr(clause))
|
|
||||||
}
|
|
||||||
code += strings.Join(clauses, ",")
|
|
||||||
code += ":"
|
|
||||||
for _, body := range cc.Body {
|
|
||||||
code += handleStmt(body, false)
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleFuncDeclName(ident *ast.Ident) string {
|
func handleFuncDeclName(ident *ast.Ident) string {
|
||||||
code := ""
|
code := ""
|
||||||
if ident == nil {
|
if ident == nil {
|
||||||
|
@ -461,17 +371,6 @@ func handleIdent(expr ast.Expr) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleIfStmt(stmt *ast.IfStmt) string {
|
|
||||||
cond := handleExpr(stmt.Cond)
|
|
||||||
body := handleBlockStmt(stmt.Body)
|
|
||||||
code := fmt.Sprintf(`if (%s) { %s }`, cond, body)
|
|
||||||
if stmt.Else != nil {
|
|
||||||
tail := handleBlockStmt(stmt.Else.(*ast.BlockStmt))
|
|
||||||
code += fmt.Sprintf(" else { %s }", tail)
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSelectorExpr(expr ast.Expr) string {
|
func handleSelectorExpr(expr ast.Expr) string {
|
||||||
s := expr.(*ast.SelectorExpr)
|
s := expr.(*ast.SelectorExpr)
|
||||||
code := ""
|
code := ""
|
||||||
|
@ -486,91 +385,3 @@ func handleSelectorExpr(expr ast.Expr) string {
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
|
||||||
code := ""
|
|
||||||
switch s := stmt.(type) {
|
|
||||||
case *ast.AssignStmt:
|
|
||||||
code += handleAssignStmt(s)
|
|
||||||
if !standaloneAssignment {
|
|
||||||
code += ";"
|
|
||||||
}
|
|
||||||
case *ast.IncDecStmt:
|
|
||||||
code += handleIncDecStmt(s)
|
|
||||||
if !standaloneAssignment {
|
|
||||||
code += ";"
|
|
||||||
}
|
|
||||||
case *ast.BranchStmt:
|
|
||||||
code += handleBranchStmt(s)
|
|
||||||
case *ast.CaseClause:
|
|
||||||
code += handleCaseClause(s)
|
|
||||||
case *ast.DeclStmt:
|
|
||||||
code += handleDeclStmt(s)
|
|
||||||
case *ast.GoStmt:
|
|
||||||
code += handleGoStmt(s)
|
|
||||||
case *ast.ExprStmt:
|
|
||||||
code += handleExprStmt(s)
|
|
||||||
code += ";"
|
|
||||||
case *ast.ForStmt:
|
|
||||||
code += handleForStmt(s)
|
|
||||||
case *ast.IfStmt:
|
|
||||||
code += handleIfStmt(s)
|
|
||||||
case *ast.SwitchStmt:
|
|
||||||
code += handleSwitchStmt(s)
|
|
||||||
case *ast.ReturnStmt:
|
|
||||||
code += handleReturnStmt(s)
|
|
||||||
default:
|
|
||||||
spew.Dump(stmt)
|
|
||||||
panic("handleStmt: unknown type")
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleForStmt(stmt *ast.ForStmt) string {
|
|
||||||
code := ""
|
|
||||||
is_while := false
|
|
||||||
if stmt.Init == nil && stmt.Post == nil {
|
|
||||||
is_while = true
|
|
||||||
code += "while"
|
|
||||||
} else {
|
|
||||||
code += "for"
|
|
||||||
}
|
|
||||||
code += "("
|
|
||||||
if !is_while {
|
|
||||||
code += handleStmt(stmt.Init, true)
|
|
||||||
code += ";"
|
|
||||||
}
|
|
||||||
|
|
||||||
if stmt.Cond != nil {
|
|
||||||
code += handleBinaryExpr(stmt.Cond)
|
|
||||||
} else {
|
|
||||||
code += "1"
|
|
||||||
}
|
|
||||||
|
|
||||||
if !is_while {
|
|
||||||
code += ";"
|
|
||||||
code += handleStmt(stmt.Post, true)
|
|
||||||
}
|
|
||||||
code += ") {"
|
|
||||||
code += handleBlockStmt(stmt.Body)
|
|
||||||
code += "}"
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSwitchStmt(stmt *ast.SwitchStmt) string {
|
|
||||||
code := "switch ("
|
|
||||||
code += handleExpr(stmt.Tag)
|
|
||||||
code += "){"
|
|
||||||
code += handleBlockStmt(stmt.Body)
|
|
||||||
code += "}"
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleReturnStmt(stmt *ast.ReturnStmt) string {
|
|
||||||
code := "return "
|
|
||||||
if len(stmt.Results) > 0 {
|
|
||||||
code += handleExpr(stmt.Results[0])
|
|
||||||
}
|
|
||||||
code += ";"
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
194
pkg/service/stmt.go
Обычный файл
194
pkg/service/stmt.go
Обычный файл
|
@ -0,0 +1,194 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go/ast"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleBlockStmt(body *ast.BlockStmt) string {
|
||||||
|
code := ""
|
||||||
|
if body == nil {
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
for _, stmt := range body.List {
|
||||||
|
code += handleStmt(stmt, false)
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
|
||||||
|
code := ""
|
||||||
|
switch s := stmt.(type) {
|
||||||
|
case *ast.AssignStmt:
|
||||||
|
code += handleAssignStmt(s)
|
||||||
|
if !standaloneAssignment {
|
||||||
|
code += ";"
|
||||||
|
}
|
||||||
|
case *ast.IncDecStmt:
|
||||||
|
code += handleIncDecStmt(s)
|
||||||
|
if !standaloneAssignment {
|
||||||
|
code += ";"
|
||||||
|
}
|
||||||
|
case *ast.BranchStmt:
|
||||||
|
code += handleBranchStmt(s)
|
||||||
|
case *ast.CaseClause:
|
||||||
|
code += handleCaseClause(s)
|
||||||
|
case *ast.DeclStmt:
|
||||||
|
code += handleDeclStmt(s)
|
||||||
|
case *ast.GoStmt:
|
||||||
|
code += handleGoStmt(s)
|
||||||
|
case *ast.ExprStmt:
|
||||||
|
code += handleExprStmt(s)
|
||||||
|
code += ";"
|
||||||
|
case *ast.ForStmt:
|
||||||
|
code += handleForStmt(s)
|
||||||
|
case *ast.IfStmt:
|
||||||
|
code += handleIfStmt(s)
|
||||||
|
case *ast.SwitchStmt:
|
||||||
|
code += handleSwitchStmt(s)
|
||||||
|
case *ast.ReturnStmt:
|
||||||
|
code += handleReturnStmt(s)
|
||||||
|
default:
|
||||||
|
spew.Dump(stmt)
|
||||||
|
panic("handleStmt: unknown type")
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleAssignStmt(as *ast.AssignStmt) string {
|
||||||
|
code := handleAssignStmtExpr(as.Lhs)
|
||||||
|
code += as.Tok.String()
|
||||||
|
code += handleAssignStmtExpr(as.Rhs)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
func handleAssignStmtExpr(e []ast.Expr) string {
|
||||||
|
ops := make([]string, 0)
|
||||||
|
code := ""
|
||||||
|
for _, op := range e {
|
||||||
|
ops = append(ops, handleExpr(op))
|
||||||
|
}
|
||||||
|
code += strings.Join(ops, ",")
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleIncDecStmt(as *ast.IncDecStmt) string {
|
||||||
|
code := handleExpr(as.X)
|
||||||
|
code += as.Tok.String()
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBranchStmt(stmt *ast.BranchStmt) string {
|
||||||
|
return stmt.Tok.String() + ";"
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCaseClause(cc *ast.CaseClause) string {
|
||||||
|
code := "case "
|
||||||
|
clauses := make([]string, 0)
|
||||||
|
for _, clause := range cc.List {
|
||||||
|
clauses = append(clauses, handleExpr(clause))
|
||||||
|
}
|
||||||
|
code += strings.Join(clauses, ",")
|
||||||
|
code += ":"
|
||||||
|
for _, body := range cc.Body {
|
||||||
|
code += handleStmt(body, false)
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDeclStmt(stmt *ast.DeclStmt) string {
|
||||||
|
code := ""
|
||||||
|
switch decl := stmt.Decl.(type) {
|
||||||
|
case *ast.GenDecl:
|
||||||
|
code += handleGenDecl(decl)
|
||||||
|
}
|
||||||
|
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 addGoHelperDeclaration(f string) {
|
||||||
|
dlock.Lock()
|
||||||
|
defer dlock.Unlock()
|
||||||
|
|
||||||
|
helpersForDeclarations = append(helpersForDeclarations, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleExprStmt(stmt *ast.ExprStmt) string {
|
||||||
|
code := ""
|
||||||
|
switch x := stmt.X.(type) {
|
||||||
|
case *ast.CallExpr:
|
||||||
|
code += handleCallExpr(x)
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleForStmt(stmt *ast.ForStmt) string {
|
||||||
|
code := ""
|
||||||
|
is_while := false
|
||||||
|
if stmt.Init == nil && stmt.Post == nil {
|
||||||
|
is_while = true
|
||||||
|
code += "while"
|
||||||
|
} else {
|
||||||
|
code += "for"
|
||||||
|
}
|
||||||
|
code += "("
|
||||||
|
if !is_while {
|
||||||
|
code += handleStmt(stmt.Init, true)
|
||||||
|
code += ";"
|
||||||
|
}
|
||||||
|
|
||||||
|
if stmt.Cond != nil {
|
||||||
|
code += handleBinaryExpr(stmt.Cond)
|
||||||
|
} else {
|
||||||
|
code += "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if !is_while {
|
||||||
|
code += ";"
|
||||||
|
code += handleStmt(stmt.Post, true)
|
||||||
|
}
|
||||||
|
code += ") {"
|
||||||
|
code += handleBlockStmt(stmt.Body)
|
||||||
|
code += "}"
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleIfStmt(stmt *ast.IfStmt) string {
|
||||||
|
cond := handleExpr(stmt.Cond)
|
||||||
|
body := handleBlockStmt(stmt.Body)
|
||||||
|
code := fmt.Sprintf(`if (%s) { %s }`, cond, body)
|
||||||
|
if stmt.Else != nil {
|
||||||
|
tail := handleBlockStmt(stmt.Else.(*ast.BlockStmt))
|
||||||
|
code += fmt.Sprintf(" else { %s }", tail)
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSwitchStmt(stmt *ast.SwitchStmt) string {
|
||||||
|
code := "switch ("
|
||||||
|
code += handleExpr(stmt.Tag)
|
||||||
|
code += "){"
|
||||||
|
code += handleBlockStmt(stmt.Body)
|
||||||
|
code += "}"
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleReturnStmt(stmt *ast.ReturnStmt) string {
|
||||||
|
code := "return "
|
||||||
|
if len(stmt.Results) > 0 {
|
||||||
|
code += handleExpr(stmt.Results[0])
|
||||||
|
}
|
||||||
|
code += ";"
|
||||||
|
return code
|
||||||
|
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче