From 5bd52e1421562ac6e350981e0fa819b3d2f38c6b Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 22 Jan 2024 03:21:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B8=D1=81=D1=82=D0=BA=D0=B0:=20Stmt=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/service.go | 189 --------------------------------------- pkg/service/stmt.go | 194 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+), 189 deletions(-) create mode 100644 pkg/service/stmt.go diff --git a/pkg/service/service.go b/pkg/service/service.go index fc715a4..ff45a38 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -8,8 +8,6 @@ import ( "io" "strings" "sync" - - "github.com/davecgh/go-spew/spew" ) // Service specifies the api logic of transforming a source code format into another target format. @@ -162,35 +160,6 @@ func addFunctionDeclaration(f string) { funcDeclarations = append(funcDeclarations, f) } -func addGoHelperDeclaration(f string) { - dlock.Lock() - defer dlock.Unlock() - - helpersForDeclarations = append(helpersForDeclarations, f) -} - -func handleAssignStmt(as *ast.AssignStmt) string { - code := handleAssignStmtExpr(as.Lhs) - code += as.Tok.String() - code += handleAssignStmtExpr(as.Rhs) - return code -} - -func handleIncDecStmt(as *ast.IncDecStmt) string { - code := handleExpr(as.X) - code += as.Tok.String() - return code -} - -func handleAssignStmtExpr(e []ast.Expr) string { - ops := make([]string, 0) - code := "" - for _, op := range e { - ops = append(ops, handleExpr(op)) - } - code += strings.Join(ops, ",") - return code -} func handleBasicLit(bl *ast.BasicLit) string { return bl.Value @@ -235,27 +204,6 @@ func handleDecl(id int, decl ast.Decl, dst chan<- string, done chan<- bool) { done <- true } -func handleDeclStmt(stmt *ast.DeclStmt) string { - code := "" - switch decl := stmt.Decl.(type) { - case *ast.GenDecl: - code += handleGenDecl(decl) - } - return code -} - -func handleGoStmt(stmt *ast.GoStmt) string { - code := "" - switch f := stmt.Call.Fun.(type) { - case *ast.Ident: - go_helper := FunctionGoHelperPrefix + f.Name - addGoHelperDeclaration(f.Name) - code += `xTaskCreate(` + go_helper + `,"",1024,NULL,1,NULL);` + "\n" - } - - return code -} - func handleExpr(expr ast.Expr) string { code := "" switch e := expr.(type) { @@ -283,15 +231,6 @@ func handleParenExpr(stmt *ast.ParenExpr) string { return code } -func handleExprStmt(stmt *ast.ExprStmt) string { - code := "" - switch x := stmt.X.(type) { - case *ast.CallExpr: - code += handleCallExpr(x) - } - return code -} - func handleFuncDecl(decl ast.Decl) string { fd := decl.(*ast.FuncDecl) if shouldSkipFunction(fd.Type) { @@ -370,35 +309,6 @@ func handleFuncDeclParams(t *ast.FuncType) string { return code } -func handleBlockStmt(body *ast.BlockStmt) string { - code := "" - if body == nil { - return code - } - for _, stmt := range body.List { - code += handleStmt(stmt, false) - } - return code -} - -func handleBranchStmt(stmt *ast.BranchStmt) string { - return stmt.Tok.String() + ";" -} - -func handleCaseClause(cc *ast.CaseClause) string { - code := "case " - clauses := make([]string, 0) - for _, clause := range cc.List { - clauses = append(clauses, handleExpr(clause)) - } - code += strings.Join(clauses, ",") - code += ":" - for _, body := range cc.Body { - code += handleStmt(body, false) - } - return code -} - func handleFuncDeclName(ident *ast.Ident) string { code := "" if ident == nil { @@ -461,17 +371,6 @@ func handleIdent(expr ast.Expr) string { return code } -func handleIfStmt(stmt *ast.IfStmt) string { - cond := handleExpr(stmt.Cond) - body := handleBlockStmt(stmt.Body) - code := fmt.Sprintf(`if (%s) { %s }`, cond, body) - if stmt.Else != nil { - tail := handleBlockStmt(stmt.Else.(*ast.BlockStmt)) - code += fmt.Sprintf(" else { %s }", tail) - } - return code -} - func handleSelectorExpr(expr ast.Expr) string { s := expr.(*ast.SelectorExpr) code := "" @@ -486,91 +385,3 @@ func handleSelectorExpr(expr ast.Expr) string { } return code } - -func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { - code := "" - switch s := stmt.(type) { - case *ast.AssignStmt: - code += handleAssignStmt(s) - if !standaloneAssignment { - code += ";" - } - case *ast.IncDecStmt: - code += handleIncDecStmt(s) - if !standaloneAssignment { - code += ";" - } - case *ast.BranchStmt: - code += handleBranchStmt(s) - case *ast.CaseClause: - code += handleCaseClause(s) - case *ast.DeclStmt: - code += handleDeclStmt(s) - case *ast.GoStmt: - code += handleGoStmt(s) - case *ast.ExprStmt: - code += handleExprStmt(s) - code += ";" - case *ast.ForStmt: - code += handleForStmt(s) - case *ast.IfStmt: - code += handleIfStmt(s) - case *ast.SwitchStmt: - code += handleSwitchStmt(s) - case *ast.ReturnStmt: - code += handleReturnStmt(s) - default: - spew.Dump(stmt) - panic("handleStmt: unknown type") - } - return code -} - -func handleForStmt(stmt *ast.ForStmt) string { - code := "" - is_while := false - if stmt.Init == nil && stmt.Post == nil { - is_while = true - code += "while" - } else { - code += "for" - } - code += "(" - if !is_while { - code += handleStmt(stmt.Init, true) - code += ";" - } - - if stmt.Cond != nil { - code += handleBinaryExpr(stmt.Cond) - } else { - code += "1" - } - - if !is_while { - code += ";" - code += handleStmt(stmt.Post, true) - } - code += ") {" - code += handleBlockStmt(stmt.Body) - code += "}" - return code -} - -func handleSwitchStmt(stmt *ast.SwitchStmt) string { - code := "switch (" - code += handleExpr(stmt.Tag) - code += "){" - code += handleBlockStmt(stmt.Body) - code += "}" - return code -} - -func handleReturnStmt(stmt *ast.ReturnStmt) string { - code := "return " - if len(stmt.Results) > 0 { - code += handleExpr(stmt.Results[0]) - } - code += ";" - return code -} diff --git a/pkg/service/stmt.go b/pkg/service/stmt.go new file mode 100644 index 0000000..59b2d69 --- /dev/null +++ b/pkg/service/stmt.go @@ -0,0 +1,194 @@ +package service + +import ( + "fmt" + "go/ast" + "strings" + + "github.com/davecgh/go-spew/spew" +) + +func handleBlockStmt(body *ast.BlockStmt) string { + code := "" + if body == nil { + return code + } + for _, stmt := range body.List { + code += handleStmt(stmt, false) + } + return code +} +func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { + code := "" + switch s := stmt.(type) { + case *ast.AssignStmt: + code += handleAssignStmt(s) + if !standaloneAssignment { + code += ";" + } + case *ast.IncDecStmt: + code += handleIncDecStmt(s) + if !standaloneAssignment { + code += ";" + } + case *ast.BranchStmt: + code += handleBranchStmt(s) + case *ast.CaseClause: + code += handleCaseClause(s) + case *ast.DeclStmt: + code += handleDeclStmt(s) + case *ast.GoStmt: + code += handleGoStmt(s) + case *ast.ExprStmt: + code += handleExprStmt(s) + code += ";" + case *ast.ForStmt: + code += handleForStmt(s) + case *ast.IfStmt: + code += handleIfStmt(s) + case *ast.SwitchStmt: + code += handleSwitchStmt(s) + case *ast.ReturnStmt: + code += handleReturnStmt(s) + default: + spew.Dump(stmt) + panic("handleStmt: unknown type") + } + return code +} + +func handleAssignStmt(as *ast.AssignStmt) string { + code := handleAssignStmtExpr(as.Lhs) + code += as.Tok.String() + code += handleAssignStmtExpr(as.Rhs) + return code +} +func handleAssignStmtExpr(e []ast.Expr) string { + ops := make([]string, 0) + code := "" + for _, op := range e { + ops = append(ops, handleExpr(op)) + } + code += strings.Join(ops, ",") + return code +} + +func handleIncDecStmt(as *ast.IncDecStmt) string { + code := handleExpr(as.X) + code += as.Tok.String() + return code +} + +func handleBranchStmt(stmt *ast.BranchStmt) string { + return stmt.Tok.String() + ";" +} + +func handleCaseClause(cc *ast.CaseClause) string { + code := "case " + clauses := make([]string, 0) + for _, clause := range cc.List { + clauses = append(clauses, handleExpr(clause)) + } + code += strings.Join(clauses, ",") + code += ":" + for _, body := range cc.Body { + code += handleStmt(body, false) + } + return code +} + +func handleDeclStmt(stmt *ast.DeclStmt) string { + code := "" + switch decl := stmt.Decl.(type) { + case *ast.GenDecl: + code += handleGenDecl(decl) + } + return code +} + +func handleGoStmt(stmt *ast.GoStmt) string { + code := "" + switch f := stmt.Call.Fun.(type) { + case *ast.Ident: + go_helper := FunctionGoHelperPrefix + f.Name + addGoHelperDeclaration(f.Name) + code += `xTaskCreate(` + go_helper + `,"",1024,NULL,1,NULL);` + "\n" + } + + return code +} +func addGoHelperDeclaration(f string) { + dlock.Lock() + defer dlock.Unlock() + + helpersForDeclarations = append(helpersForDeclarations, f) +} + +func handleExprStmt(stmt *ast.ExprStmt) string { + code := "" + switch x := stmt.X.(type) { + case *ast.CallExpr: + code += handleCallExpr(x) + } + return code +} + +func handleForStmt(stmt *ast.ForStmt) string { + code := "" + is_while := false + if stmt.Init == nil && stmt.Post == nil { + is_while = true + code += "while" + } else { + code += "for" + } + code += "(" + if !is_while { + code += handleStmt(stmt.Init, true) + code += ";" + } + + if stmt.Cond != nil { + code += handleBinaryExpr(stmt.Cond) + } else { + code += "1" + } + + if !is_while { + code += ";" + code += handleStmt(stmt.Post, true) + } + code += ") {" + code += handleBlockStmt(stmt.Body) + code += "}" + return code +} + +func handleIfStmt(stmt *ast.IfStmt) string { + cond := handleExpr(stmt.Cond) + body := handleBlockStmt(stmt.Body) + code := fmt.Sprintf(`if (%s) { %s }`, cond, body) + if stmt.Else != nil { + tail := handleBlockStmt(stmt.Else.(*ast.BlockStmt)) + code += fmt.Sprintf(" else { %s }", tail) + } + return code +} + +func handleSwitchStmt(stmt *ast.SwitchStmt) string { + code := "switch (" + code += handleExpr(stmt.Tag) + code += "){" + code += handleBlockStmt(stmt.Body) + code += "}" + return code +} + +func handleReturnStmt(stmt *ast.ReturnStmt) string { + code := "return " + if len(stmt.Results) > 0 { + code += handleExpr(stmt.Results[0]) + } + code += ";" + return code +}