From ca4c4a0fd62f9d2b3402a1f2670830d3dd9bc0b2 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 12 Feb 2024 07:36:37 +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=9D=D0=B0=D1=81=D0=BB=D0=B5=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/class.go | 37 ++++++++++++++++++++++++++++---- pkg/service/expr.go | 5 ++--- pkg/service/features/app.feature | 23 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/pkg/service/class.go b/pkg/service/class.go index 112c12f..fe21824 100644 --- a/pkg/service/class.go +++ b/pkg/service/class.go @@ -19,9 +19,10 @@ var ( ) type class struct { - name string - Struct *ast.StructType - methods []*ast.FuncDecl + name string + baseInterface string + Struct *ast.StructType + methods []*ast.FuncDecl } func NewClass(name string) *class { @@ -33,6 +34,7 @@ func (c *class) Name() string { } func (c *class) String() (code string) { + c.checkForBaseInterface() code += c.classDefinitionToString() return } @@ -42,7 +44,13 @@ func (c *class) MethodsString() (code string) { } func (c *class) classDefinitionToString() (code string) { - code = "class " + c.name + " {" + code += "class " + c.name + + if len(c.baseInterface) > 0 { + code += ": public " + c.baseInterface + } + + code += " {" code += "public:\n" code += c.structToString() @@ -54,6 +62,27 @@ func (c *class) classDefinitionToString() (code string) { code += "};\n" return } +func (c *class) checkForBaseInterface() { + if c.Struct.Fields.NumFields() == 0 { + return + } + if len(c.Struct.Fields.List[0].Names) == 0 { + return + } + + n := c.Struct.Fields.List[0].Names[0] + if n.Name != "_baseInterface" { + return + } + + i, ok := c.Struct.Fields.List[0].Type.(*ast.Ident) + if !ok { + return + } + + c.baseInterface = i.Name + return +} func (c *class) structToString() (code string) { code += handleExpr(c.Struct) code += "\n" diff --git a/pkg/service/expr.go b/pkg/service/expr.go index e103084..f1a95dd 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -6,9 +6,8 @@ import ( "strings" ) -func handleExpr(expr ast.Expr) string { +func handleExpr(expr ast.Expr) (code string) { // spew.Dump(expr) - code := "" switch e := expr.(type) { case *ast.BasicLit: code += handleBasicLit(e) @@ -31,7 +30,7 @@ func handleExpr(expr ast.Expr) string { case *ast.ArrayType: code += handleArray(e) } - return code + return } func handleBasicLit(bl *ast.BasicLit) string { diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 6d997f5..7fce9c3 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -50,6 +50,29 @@ public: virtual void Add(int) = 0; virtual void Add2(int,double) = 0; }; +``` + + Сценарий: Наследование + * Исходник: +``` +package test + +type Device interface { +} + +type DeviceClass struct { + _baseInterface Device +} +``` + * Результат: +``` +class Device { +public: +}; +class DeviceClass: public Device { +public: + Device* _baseInterface; +}; ``` Сценарий: Класс: свойство типа интерфейс - указатель