diff --git a/pkg/service/expr.go b/pkg/service/expr.go index f1a95dd..465f1dc 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -7,7 +7,9 @@ import ( ) func handleExpr(expr ast.Expr) (code string) { + // println() // spew.Dump(expr) + switch e := expr.(type) { case *ast.BasicLit: code += handleBasicLit(e) @@ -23,6 +25,8 @@ func handleExpr(expr ast.Expr) (code string) { code += handleParenExpr(e) case *ast.SelectorExpr: code += handleSelectorExpr(e) + case *ast.IndexExpr: + code += handleIndexExpr(e) case *ast.StructType: code += handleStructType(e) case *ast.InterfaceType: @@ -137,6 +141,27 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) { return } +func handleIndexExpr(ind *ast.IndexExpr) (code string) { + // println() + // spew.Dump(ind) + + switch x := ind.X.(type) { + case *ast.SelectorExpr: + if isInMethod { + code += "this" + } else { + code += handleExpr(x.X) + } + + code += "->" + code += handleIdentExpr(x.Sel) + code += "[" + code += handleExpr(ind.Index) + code += "]" + } + return +} + func handleStarExpr(s *ast.StarExpr) (code string) { switch x := s.X.(type) { case *ast.Ident: diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 7fce9c3..3eeb1b9 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -307,6 +307,50 @@ void device::doSomething() { this->x=1; this->x=this->y; } +``` + + Сценарий: Структура с обращением к полям + * Исходник: +``` +package test + +type device struct { + g [8]bool +} + +func (dev *device) doSomething(a *int) { + if dev.g[0] { + a:=1 + } +} + +func some(gs *GPIOS) { + if gs.g[0] { + a:=1 + } +} +``` + * Результат: +``` +class device { +public: + bool g[8]; + void doSomething(int* a); +}; + +void some(GPIOS* gs); + +void device::doSomething(int* a) { +if (this->g[0]) { + auto a=1; + } +} + +void some(GPIOS* gs) { +if (gs->g[0]) { + auto a=1; + } +} ``` Сценарий: Структура с вызовом метода и свойства другого объекта diff --git a/pkg/service/stmt.go b/pkg/service/stmt.go index 4dc2bbd..e8f9ea7 100644 --- a/pkg/service/stmt.go +++ b/pkg/service/stmt.go @@ -21,6 +21,8 @@ func handleBlockStmt(body *ast.BlockStmt) string { return code } func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { + // println() + // spew.Dump(stmt) code := "" switch s := stmt.(type) { case *ast.AssignStmt: @@ -182,6 +184,9 @@ func handleForStmt(stmt *ast.ForStmt) string { } func handleIfStmt(stmt *ast.IfStmt) string { + // println() + // spew.Dump(stmt) + cond := handleExpr(stmt.Cond) body := handleBlockStmt(stmt.Body) code := fmt.Sprintf("if (%s) {\n %s }\n", cond, body)