Чистка: Expr обработчики вынесены в отдельный файл
Этот коммит содержится в:
родитель
5bd52e1421
коммит
31fd0251de
4 изменённых файлов: 101 добавлений и 95 удалений
96
pkg/service/expr.go
Обычный файл
96
pkg/service/expr.go
Обычный файл
|
@ -0,0 +1,96 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/ast"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handleExpr(expr ast.Expr) string {
|
||||||
|
code := ""
|
||||||
|
switch e := expr.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
code += handleBasicLit(e)
|
||||||
|
case *ast.UnaryExpr:
|
||||||
|
code += handleUnaryExpr(e)
|
||||||
|
case *ast.BinaryExpr:
|
||||||
|
code += handleBinaryExpr(e)
|
||||||
|
case *ast.CallExpr:
|
||||||
|
code += handleCallExpr(e)
|
||||||
|
case *ast.Ident:
|
||||||
|
code += handleIdentExpr(e)
|
||||||
|
case *ast.ParenExpr:
|
||||||
|
code += handleParenExpr(e)
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
code += handleSelectorExpr(e)
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBasicLit(bl *ast.BasicLit) string {
|
||||||
|
return bl.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleUnaryExpr(expr *ast.UnaryExpr) string {
|
||||||
|
code := expr.Op.String()
|
||||||
|
code += handleExpr(expr.X)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleBinaryExpr(expr ast.Expr) string {
|
||||||
|
be := expr.(*ast.BinaryExpr)
|
||||||
|
code := handleExpr(be.X)
|
||||||
|
code += be.Op.String()
|
||||||
|
code += handleExpr(be.Y)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleCallExpr(expr *ast.CallExpr) string {
|
||||||
|
code := handleExpr(expr.Fun)
|
||||||
|
code += "("
|
||||||
|
args := make([]string, 0)
|
||||||
|
for _, arg := range expr.Args {
|
||||||
|
args = append(args, handleExpr(arg))
|
||||||
|
}
|
||||||
|
code += strings.Join(args, ",")
|
||||||
|
code += ")"
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleIdentExpr(expr ast.Expr) string {
|
||||||
|
ident := expr.(*ast.Ident)
|
||||||
|
code := ""
|
||||||
|
switch ident.Name {
|
||||||
|
case "nil":
|
||||||
|
code += "NULL"
|
||||||
|
case "uint32":
|
||||||
|
code += "unsigned long"
|
||||||
|
case "uint64":
|
||||||
|
code += "unsigned long long"
|
||||||
|
case "string":
|
||||||
|
code += "char*"
|
||||||
|
default:
|
||||||
|
code += ident.Name
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleParenExpr(stmt *ast.ParenExpr) string {
|
||||||
|
code := ""
|
||||||
|
code += handleExpr(stmt.X)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSelectorExpr(expr ast.Expr) string {
|
||||||
|
s := expr.(*ast.SelectorExpr)
|
||||||
|
code := ""
|
||||||
|
switch x := s.X.(type) {
|
||||||
|
case *ast.Ident:
|
||||||
|
code += handleIdentExpr(x)
|
||||||
|
}
|
||||||
|
code += "."
|
||||||
|
code += handleIdentExpr(s.Sel)
|
||||||
|
if val, ok := mapping[code]; ok {
|
||||||
|
code = val
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
|
@ -161,36 +161,6 @@ func addFunctionDeclaration(f string) {
|
||||||
funcDeclarations = append(funcDeclarations, f)
|
funcDeclarations = append(funcDeclarations, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBasicLit(bl *ast.BasicLit) string {
|
|
||||||
return bl.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleUnaryExpr(expr *ast.UnaryExpr) string {
|
|
||||||
code := expr.Op.String()
|
|
||||||
code += handleExpr(expr.X)
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleBinaryExpr(expr ast.Expr) string {
|
|
||||||
be := expr.(*ast.BinaryExpr)
|
|
||||||
code := handleExpr(be.X)
|
|
||||||
code += be.Op.String()
|
|
||||||
code += handleExpr(be.Y)
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleCallExpr(expr *ast.CallExpr) string {
|
|
||||||
code := handleExpr(expr.Fun)
|
|
||||||
code += "("
|
|
||||||
args := make([]string, 0)
|
|
||||||
for _, arg := range expr.Args {
|
|
||||||
args = append(args, handleExpr(arg))
|
|
||||||
}
|
|
||||||
code += strings.Join(args, ",")
|
|
||||||
code += ")"
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
|
func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
|
||||||
code := ""
|
code := ""
|
||||||
switch d := decl.(type) {
|
switch d := decl.(type) {
|
||||||
|
@ -204,33 +174,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) {
|
||||||
done <- true
|
done <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleExpr(expr ast.Expr) string {
|
|
||||||
code := ""
|
|
||||||
switch e := expr.(type) {
|
|
||||||
case *ast.BasicLit:
|
|
||||||
code += handleBasicLit(e)
|
|
||||||
case *ast.UnaryExpr:
|
|
||||||
code += handleUnaryExpr(e)
|
|
||||||
case *ast.BinaryExpr:
|
|
||||||
code += handleBinaryExpr(e)
|
|
||||||
case *ast.CallExpr:
|
|
||||||
code += handleCallExpr(e)
|
|
||||||
case *ast.Ident:
|
|
||||||
code += handleIdent(e)
|
|
||||||
case *ast.ParenExpr:
|
|
||||||
code += handleParenExpr(e)
|
|
||||||
case *ast.SelectorExpr:
|
|
||||||
code += handleSelectorExpr(e)
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleParenExpr(stmt *ast.ParenExpr) string {
|
|
||||||
code := ""
|
|
||||||
code += handleExpr(stmt.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) {
|
||||||
|
@ -299,7 +242,7 @@ func handleFuncDeclParams(t *ast.FuncType) string {
|
||||||
ftype := ""
|
ftype := ""
|
||||||
switch ft := field.Type.(type) {
|
switch ft := field.Type.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
ftype = handleIdent(ft)
|
ftype = handleIdentExpr(ft)
|
||||||
}
|
}
|
||||||
for _, names := range field.Names {
|
for _, names := range field.Names {
|
||||||
values = append(values, ftype+" "+names.Name)
|
values = append(values, ftype+" "+names.Name)
|
||||||
|
@ -334,7 +277,7 @@ func handleFuncDeclType(t *ast.FuncType) string {
|
||||||
|
|
||||||
switch ft := fl.List[0].Type.(type) {
|
switch ft := fl.List[0].Type.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
code += handleIdent(ft)
|
code += handleIdentExpr(ft)
|
||||||
}
|
}
|
||||||
|
|
||||||
return code
|
return code
|
||||||
|
@ -352,36 +295,3 @@ func handleGenDecl(decl ast.Decl) string {
|
||||||
code += handleSpecs(gd.Specs)
|
code += handleSpecs(gd.Specs)
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleIdent(expr ast.Expr) string {
|
|
||||||
ident := expr.(*ast.Ident)
|
|
||||||
code := ""
|
|
||||||
switch ident.Name {
|
|
||||||
case "nil":
|
|
||||||
code += "NULL"
|
|
||||||
case "uint32":
|
|
||||||
code += "unsigned long"
|
|
||||||
case "uint64":
|
|
||||||
code += "unsigned long long"
|
|
||||||
case "string":
|
|
||||||
code += "char*"
|
|
||||||
default:
|
|
||||||
code += ident.Name
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSelectorExpr(expr ast.Expr) string {
|
|
||||||
s := expr.(*ast.SelectorExpr)
|
|
||||||
code := ""
|
|
||||||
switch x := s.X.(type) {
|
|
||||||
case *ast.Ident:
|
|
||||||
code += handleIdent(x)
|
|
||||||
}
|
|
||||||
code += "."
|
|
||||||
code += handleIdent(s.Sel)
|
|
||||||
if val, ok := mapping[code]; ok {
|
|
||||||
code = val
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ func handleImportSpec(spec ast.Spec) string {
|
||||||
s := spec.(*ast.ImportSpec)
|
s := spec.(*ast.ImportSpec)
|
||||||
code := ""
|
code := ""
|
||||||
if s.Name != nil {
|
if s.Name != nil {
|
||||||
name := handleIdent(s.Name)
|
name := handleIdentExpr(s.Name)
|
||||||
if val, ok := mapping[name]; ok {
|
if val, ok := mapping[name]; ok {
|
||||||
name = val
|
name = val
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ func handleValueSpec(spec ast.Spec) string {
|
||||||
func handleValueSpecNames(names []*ast.Ident) string {
|
func handleValueSpecNames(names []*ast.Ident) string {
|
||||||
code := ""
|
code := ""
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
code += handleIdent(name)
|
code += handleIdentExpr(name)
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ func handleValueSpecType(expr ast.Expr) string {
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
code += handleSelectorExpr(t)
|
code += handleSelectorExpr(t)
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
code += handleIdent(t)
|
code += handleIdentExpr(t)
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче