From 31fd0251de502bfd80f9e2c46b6043a8aa8aed7f Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 22 Jan 2024 03:26:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B8=D1=81=D1=82=D0=BA=D0=B0:=20Expr=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/expr.go | 96 ++++++++++++++++++++++++++++++++++++++++++ pkg/service/service.go | 94 +---------------------------------------- pkg/service/spec.go | 2 +- pkg/service/value.go | 4 +- 4 files changed, 101 insertions(+), 95 deletions(-) create mode 100644 pkg/service/expr.go diff --git a/pkg/service/expr.go b/pkg/service/expr.go new file mode 100644 index 0000000..72f0bee --- /dev/null +++ b/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 +} diff --git a/pkg/service/service.go b/pkg/service/service.go index ff45a38..966ea90 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -161,36 +161,6 @@ func addFunctionDeclaration(f string) { 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) { code := "" switch d := decl.(type) { @@ -204,33 +174,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) { 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 { fd := decl.(*ast.FuncDecl) if shouldSkipFunction(fd.Type) { @@ -299,7 +242,7 @@ func handleFuncDeclParams(t *ast.FuncType) string { ftype := "" switch ft := field.Type.(type) { case *ast.Ident: - ftype = handleIdent(ft) + ftype = handleIdentExpr(ft) } for _, names := range field.Names { values = append(values, ftype+" "+names.Name) @@ -334,7 +277,7 @@ func handleFuncDeclType(t *ast.FuncType) string { switch ft := fl.List[0].Type.(type) { case *ast.Ident: - code += handleIdent(ft) + code += handleIdentExpr(ft) } return code @@ -352,36 +295,3 @@ func handleGenDecl(decl ast.Decl) string { code += handleSpecs(gd.Specs) 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 -} diff --git a/pkg/service/spec.go b/pkg/service/spec.go index c69edc3..c559d97 100644 --- a/pkg/service/spec.go +++ b/pkg/service/spec.go @@ -23,7 +23,7 @@ func handleImportSpec(spec ast.Spec) string { s := spec.(*ast.ImportSpec) code := "" if s.Name != nil { - name := handleIdent(s.Name) + name := handleIdentExpr(s.Name) if val, ok := mapping[name]; ok { name = val } diff --git a/pkg/service/value.go b/pkg/service/value.go index 33fc618..c5c87bf 100644 --- a/pkg/service/value.go +++ b/pkg/service/value.go @@ -20,7 +20,7 @@ func handleValueSpec(spec ast.Spec) string { func handleValueSpecNames(names []*ast.Ident) string { code := "" for _, name := range names { - code += handleIdent(name) + code += handleIdentExpr(name) } return code } @@ -31,7 +31,7 @@ func handleValueSpecType(expr ast.Expr) string { case *ast.SelectorExpr: code += handleSelectorExpr(t) case *ast.Ident: - code += handleIdent(t) + code += handleIdentExpr(t) } return code }