From b8b3698514f3cfdaf8af5c0c9e0739a6bf2be0f3 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 22 Jan 2024 04:02:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B9:=20=D0=A1=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80?= =?UTF-8?q?=D0=B0=20=D1=81=20=D0=BF=D0=BE=D0=BB=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/expr.go | 2 ++ pkg/service/features/app.feature | 21 ++++++++++++++++- pkg/service/type.go | 39 +++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pkg/service/expr.go b/pkg/service/expr.go index 72f0bee..8445a25 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -22,6 +22,8 @@ func handleExpr(expr ast.Expr) string { code += handleParenExpr(e) case *ast.SelectorExpr: code += handleSelectorExpr(e) + case *ast.StructType: + code += handleStructType(e) } return code } diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 75b6122..53a8860 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -22,5 +22,24 @@ type device struct {} ``` * Результат: ``` -class device {} +class device { +public: +}; +``` + + Сценарий: Структура с полем + * Исходник: +``` +package test + +type device struct { + a int +} +``` + * Результат: +``` +class device { +public: + int a; +}; ``` diff --git a/pkg/service/type.go b/pkg/service/type.go index 8fea284..f9f551c 100644 --- a/pkg/service/type.go +++ b/pkg/service/type.go @@ -10,6 +10,43 @@ func handleTypeSpec(spec ast.Spec) string { code += " " code += s.Name.String() code += " " - code += "{}" + code += "{\n" + code += handleClass(s.Type) + // spew.Dump(s.TypeParams) + code += "};\n" + return code +} + +func handleClass(e ast.Expr) string { + code := "" + code += "public:\n" + code += handleExpr(e) + // spew.Dump(e) + return code +} + +func handleStructType(s *ast.StructType) string { + code := "" + code += handleFieldList(s.Fields) + return code +} + +func handleFieldList(l *ast.FieldList) string { + code := "" + for _, f := range l.List { + code += handleField(f) + } + return code +} + +func handleField(f *ast.Field) string { + code := "" + code += " " + code += handleIdentExpr(f.Type) + code += " " + for _, n := range f.Names { + code += handleIdentExpr(n) + } + code += ";\n" return code }