From f772b9a78059253c7fd0ab5b0560c2061cead0ea Mon Sep 17 00:00:00 2001 From: koki-develop Date: Wed, 6 Mar 2024 16:52:30 +0900 Subject: [PATCH] Convert functions to methods --- internal/mingo/decl.go | 36 ++++----- internal/mingo/expr.go | 154 +++++++++++++++++++------------------- internal/mingo/file.go | 2 +- internal/mingo/func.go | 24 +++--- internal/mingo/mingo.go | 6 +- internal/mingo/stmt.go | 162 ++++++++++++++++++++-------------------- 6 files changed, 192 insertions(+), 192 deletions(-) diff --git a/internal/mingo/decl.go b/internal/mingo/decl.go index a534454..9eac5ef 100644 --- a/internal/mingo/decl.go +++ b/internal/mingo/decl.go @@ -7,40 +7,40 @@ import ( "strings" ) -func stringifyDecl(decl ast.Decl) string { +func (m *mingo) stringifyDecl(decl ast.Decl) string { switch x := decl.(type) { case *ast.GenDecl: - return stringifyGenDecl(x) + return m.stringifyGenDecl(x) case *ast.FuncDecl: - return stringifyFuncDecl(x) + return m.stringifyFuncDecl(x) } return "" } -func stringifyGenDecl(n *ast.GenDecl) string { +func (m *mingo) stringifyGenDecl(n *ast.GenDecl) string { switch n.Tok { case token.IMPORT: imports := []*ast.ImportSpec{} for _, spec := range n.Specs { imports = append(imports, spec.(*ast.ImportSpec)) } - return stringifyImportSpecs(imports) + return m.stringifyImportSpecs(imports) case token.CONST: consts := []*ast.ValueSpec{} for _, spec := range n.Specs { consts = append(consts, spec.(*ast.ValueSpec)) } - return stringifyConstSpecs(consts) + return m.stringifyConstSpecs(consts) case token.VAR: vars := []*ast.ValueSpec{} for _, spec := range n.Specs { vars = append(vars, spec.(*ast.ValueSpec)) } - return stringifyVarSpecs(vars) + return m.stringifyVarSpecs(vars) case token.TYPE: sb := new(strings.Builder) for _, spec := range n.Specs { - sb.WriteString(stringifyTypeSpec(spec.(*ast.TypeSpec))) + sb.WriteString(m.stringifyTypeSpec(spec.(*ast.TypeSpec))) sb.WriteString(";") } return sb.String() @@ -49,7 +49,7 @@ func stringifyGenDecl(n *ast.GenDecl) string { return "" } -func stringifyImportSpecs(specs []*ast.ImportSpec) string { +func (m *mingo) stringifyImportSpecs(specs []*ast.ImportSpec) string { sb := new(strings.Builder) sb.WriteString("import") @@ -77,7 +77,7 @@ func stringifyImportSpecs(specs []*ast.ImportSpec) string { return sb.String() } -func stringifyConstSpecs(specs []*ast.ValueSpec) string { +func (m *mingo) stringifyConstSpecs(specs []*ast.ValueSpec) string { sb := new(strings.Builder) sb.WriteString("const") @@ -100,7 +100,7 @@ func stringifyConstSpecs(specs []*ast.ValueSpec) string { if spec.Type != nil { sb.WriteString(" ") - sb.WriteString(stringifyExpr(spec.Type)) + sb.WriteString(m.stringifyExpr(spec.Type)) } if spec.Values != nil { @@ -109,7 +109,7 @@ func stringifyConstSpecs(specs []*ast.ValueSpec) string { if k > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(value)) + sb.WriteString(m.stringifyExpr(value)) } } } @@ -121,7 +121,7 @@ func stringifyConstSpecs(specs []*ast.ValueSpec) string { return sb.String() } -func stringifyVarSpecs(specs []*ast.ValueSpec) string { +func (m *mingo) stringifyVarSpecs(specs []*ast.ValueSpec) string { sb := new(strings.Builder) sb.WriteString("var") @@ -146,7 +146,7 @@ func stringifyVarSpecs(specs []*ast.ValueSpec) string { if spec.Type != nil { sb.WriteString(" ") - sb.WriteString(stringifyExpr(spec.Type)) + sb.WriteString(m.stringifyExpr(spec.Type)) } if spec.Values != nil { @@ -155,7 +155,7 @@ func stringifyVarSpecs(specs []*ast.ValueSpec) string { if k > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(value)) + sb.WriteString(m.stringifyExpr(value)) } } } @@ -167,19 +167,19 @@ func stringifyVarSpecs(specs []*ast.ValueSpec) string { return sb.String() } -func stringifyTypeSpec(n *ast.TypeSpec) string { +func (m *mingo) stringifyTypeSpec(n *ast.TypeSpec) string { sb := new(strings.Builder) sb.WriteString(fmt.Sprintf("type %s", n.Name.Name)) if n.TypeParams != nil { - sb.WriteString(stringifyFuncTypeParams(n.TypeParams)) + sb.WriteString(m.stringifyFuncTypeParams(n.TypeParams)) } if n.Assign != 0 { sb.WriteString("=") } else { sb.WriteString(" ") } - sb.WriteString(stringifyExpr(n.Type)) + sb.WriteString(m.stringifyExpr(n.Type)) return sb.String() } diff --git a/internal/mingo/expr.go b/internal/mingo/expr.go index 42cef69..e3eadf5 100644 --- a/internal/mingo/expr.go +++ b/internal/mingo/expr.go @@ -6,50 +6,50 @@ import ( "strings" ) -func stringifyExpr(expr ast.Expr) string { +func (m *mingo) stringifyExpr(expr ast.Expr) string { switch x := expr.(type) { case *ast.BasicLit: - return stringifyBasicLit(x) + return m.stringifyBasicLit(x) case *ast.CallExpr: - return stringifyCallExpr(x) + return m.stringifyCallExpr(x) case *ast.SelectorExpr: - return stringifySelectExpr(x) + return m.stringifySelectExpr(x) case *ast.StarExpr: - return stringifyStarExpr(x) + return m.stringifyStarExpr(x) case *ast.ArrayType: - return stringifyArrayType(x) + return m.stringifyArrayType(x) case *ast.Ellipsis: - return stringifyEllipsis(x) + return m.stringifyEllipsis(x) case *ast.FuncLit: - return stringifyFuncLit(x) + return m.stringifyFuncLit(x) case *ast.BinaryExpr: - return stringifyBinaryExpr(x) + return m.stringifyBinaryExpr(x) case *ast.SliceExpr: - return stringifySliceExpr(x) + return m.stringifySliceExpr(x) case *ast.UnaryExpr: - return stringifyUnaryExpr(x) + return m.stringifyUnaryExpr(x) case *ast.CompositeLit: - return stringifyCompositeLit(x) + return m.stringifyCompositeLit(x) case *ast.ParenExpr: - return stringifyParenExpr(x) + return m.stringifyParenExpr(x) case *ast.IndexExpr: - return stringifyIndexExpr(x) + return m.stringifyIndexExpr(x) case *ast.KeyValueExpr: - return stringifyKeyValueExpr(x) + return m.stringifyKeyValueExpr(x) case *ast.TypeAssertExpr: - return stringifyTypeAssertExpr(x) + return m.stringifyTypeAssertExpr(x) case *ast.ChanType: - return stringifyChanType(x) + return m.stringifyChanType(x) case *ast.MapType: - return stringifyMapType(x) + return m.stringifyMapType(x) case *ast.InterfaceType: - return stringifyInterfaceType(x) + return m.stringifyInterfaceType(x) case *ast.StructType: - return stringifyStructType(x) + return m.stringifyStructType(x) case *ast.FuncType: - return stringifyFuncType(x) + return m.stringifyFuncType(x) case *ast.IndexListExpr: - return stringifyIndexListExpr(x) + return m.stringifyIndexListExpr(x) case nil: return "" } @@ -57,45 +57,45 @@ func stringifyExpr(expr ast.Expr) string { return expr.(*ast.Ident).Name } -func stringifyIndexListExpr(expr *ast.IndexListExpr) string { +func (m *mingo) stringifyIndexListExpr(expr *ast.IndexListExpr) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(expr.X)) + sb.WriteString(m.stringifyExpr(expr.X)) sb.WriteString("[") for i, index := range expr.Indices { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(index)) + sb.WriteString(m.stringifyExpr(index)) } sb.WriteString("]") return sb.String() } -func stringifySelectExpr(expr *ast.SelectorExpr) string { +func (m *mingo) stringifySelectExpr(expr *ast.SelectorExpr) string { switch x := expr.X.(type) { case *ast.SelectorExpr: - return fmt.Sprintf("%s.%s", stringifySelectExpr(x), expr.Sel.Name) + return fmt.Sprintf("%s.%s", m.stringifySelectExpr(x), expr.Sel.Name) default: - return fmt.Sprintf("%s.%s", stringifyExpr(expr.X), expr.Sel.Name) + return fmt.Sprintf("%s.%s", m.stringifyExpr(expr.X), expr.Sel.Name) } } -func stringifyBasicLit(lit *ast.BasicLit) string { +func (m *mingo) stringifyBasicLit(lit *ast.BasicLit) string { return lit.Value } -func stringifyCallExpr(expr *ast.CallExpr) string { +func (m *mingo) stringifyCallExpr(expr *ast.CallExpr) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(expr.Fun)) + sb.WriteString(m.stringifyExpr(expr.Fun)) sb.WriteString("(") for i, arg := range expr.Args { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(arg)) + sb.WriteString(m.stringifyExpr(arg)) } if expr.Ellipsis.IsValid() { @@ -107,108 +107,108 @@ func stringifyCallExpr(expr *ast.CallExpr) string { return sb.String() } -func stringifyStarExpr(expr *ast.StarExpr) string { - return fmt.Sprintf("*%s", stringifyExpr(expr.X)) +func (m *mingo) stringifyStarExpr(expr *ast.StarExpr) string { + return fmt.Sprintf("*%s", m.stringifyExpr(expr.X)) } -func stringifyArrayType(expr *ast.ArrayType) string { - return fmt.Sprintf("[]%s", stringifyExpr(expr.Elt)) +func (m *mingo) stringifyArrayType(expr *ast.ArrayType) string { + return fmt.Sprintf("[]%s", m.stringifyExpr(expr.Elt)) } -func stringifyEllipsis(expr *ast.Ellipsis) string { - return fmt.Sprintf("...%s", stringifyExpr(expr.Elt)) +func (m *mingo) stringifyEllipsis(expr *ast.Ellipsis) string { + return fmt.Sprintf("...%s", m.stringifyExpr(expr.Elt)) } -func stringifyFuncLit(expr *ast.FuncLit) string { +func (m *mingo) stringifyFuncLit(expr *ast.FuncLit) string { sb := new(strings.Builder) - sb.WriteString(stringifyFuncType(expr.Type)) - sb.WriteString(stringifyBlockStmt(expr.Body)) + sb.WriteString(m.stringifyFuncType(expr.Type)) + sb.WriteString(m.stringifyBlockStmt(expr.Body)) return sb.String() } -func stringifyBinaryExpr(expr *ast.BinaryExpr) string { - return fmt.Sprintf("%s%s%s", stringifyExpr(expr.X), expr.Op.String(), stringifyExpr(expr.Y)) +func (m *mingo) stringifyBinaryExpr(expr *ast.BinaryExpr) string { + return fmt.Sprintf("%s%s%s", m.stringifyExpr(expr.X), expr.Op.String(), m.stringifyExpr(expr.Y)) } -func stringifySliceExpr(expr *ast.SliceExpr) string { +func (m *mingo) stringifySliceExpr(expr *ast.SliceExpr) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(expr.X)) + sb.WriteString(m.stringifyExpr(expr.X)) sb.WriteString("[") if expr.Low != nil { - sb.WriteString(stringifyExpr(expr.Low)) + sb.WriteString(m.stringifyExpr(expr.Low)) } sb.WriteString(":") if expr.High != nil { - sb.WriteString(stringifyExpr(expr.High)) + sb.WriteString(m.stringifyExpr(expr.High)) } if expr.Max != nil { sb.WriteString(":") - sb.WriteString(stringifyExpr(expr.Max)) + sb.WriteString(m.stringifyExpr(expr.Max)) } sb.WriteString("]") return sb.String() } -func stringifyUnaryExpr(expr *ast.UnaryExpr) string { - return fmt.Sprintf("%s%s", expr.Op.String(), stringifyExpr(expr.X)) +func (m *mingo) stringifyUnaryExpr(expr *ast.UnaryExpr) string { + return fmt.Sprintf("%s%s", expr.Op.String(), m.stringifyExpr(expr.X)) } -func stringifyCompositeLit(expr *ast.CompositeLit) string { +func (m *mingo) stringifyCompositeLit(expr *ast.CompositeLit) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(expr.Type)) + sb.WriteString(m.stringifyExpr(expr.Type)) sb.WriteString("{") for i, elt := range expr.Elts { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(elt)) + sb.WriteString(m.stringifyExpr(elt)) } sb.WriteString("}") return sb.String() } -func stringifyParenExpr(expr *ast.ParenExpr) string { - return fmt.Sprintf("(%s)", stringifyExpr(expr.X)) +func (m *mingo) stringifyParenExpr(expr *ast.ParenExpr) string { + return fmt.Sprintf("(%s)", m.stringifyExpr(expr.X)) } -func stringifyIndexExpr(expr *ast.IndexExpr) string { - return fmt.Sprintf("%s[%s]", stringifyExpr(expr.X), stringifyExpr(expr.Index)) +func (m *mingo) stringifyIndexExpr(expr *ast.IndexExpr) string { + return fmt.Sprintf("%s[%s]", m.stringifyExpr(expr.X), m.stringifyExpr(expr.Index)) } -func stringifyKeyValueExpr(expr *ast.KeyValueExpr) string { - return fmt.Sprintf("%s:%s", stringifyExpr(expr.Key), stringifyExpr(expr.Value)) +func (m *mingo) stringifyKeyValueExpr(expr *ast.KeyValueExpr) string { + return fmt.Sprintf("%s:%s", m.stringifyExpr(expr.Key), m.stringifyExpr(expr.Value)) } -func stringifyTypeAssertExpr(expr *ast.TypeAssertExpr) string { +func (m *mingo) stringifyTypeAssertExpr(expr *ast.TypeAssertExpr) string { if expr.Type == nil { - return fmt.Sprintf("%s.(type)", stringifyExpr(expr.X)) + return fmt.Sprintf("%s.(type)", m.stringifyExpr(expr.X)) } else { - return fmt.Sprintf("%s.(%s)", stringifyExpr(expr.X), stringifyExpr(expr.Type)) + return fmt.Sprintf("%s.(%s)", m.stringifyExpr(expr.X), m.stringifyExpr(expr.Type)) } } -func stringifyChanType(expr *ast.ChanType) string { +func (m *mingo) stringifyChanType(expr *ast.ChanType) string { sb := new(strings.Builder) if expr.Dir == ast.RECV { sb.WriteString("<-") } sb.WriteString("chan ") - sb.WriteString(stringifyExpr(expr.Value)) + sb.WriteString(m.stringifyExpr(expr.Value)) return sb.String() } -func stringifyMapType(expr *ast.MapType) string { - return fmt.Sprintf("map[%s]%s", stringifyExpr(expr.Key), stringifyExpr(expr.Value)) +func (m *mingo) stringifyMapType(expr *ast.MapType) string { + return fmt.Sprintf("map[%s]%s", m.stringifyExpr(expr.Key), m.stringifyExpr(expr.Value)) } -func stringifyInterfaceType(expr *ast.InterfaceType) string { +func (m *mingo) stringifyInterfaceType(expr *ast.InterfaceType) string { sb := new(strings.Builder) sb.WriteString("interface{") @@ -220,11 +220,11 @@ func stringifyInterfaceType(expr *ast.InterfaceType) string { sb.WriteString(name.Name) } if f, ok := field.Type.(*ast.FuncType); ok { - sb.WriteString(stringifyFuncTypeParams(f.TypeParams)) - sb.WriteString(stringifyFuncParams(f.Params)) - sb.WriteString(stringifyFuncResults(f.Results)) + sb.WriteString(m.stringifyFuncTypeParams(f.TypeParams)) + sb.WriteString(m.stringifyFuncParams(f.Params)) + sb.WriteString(m.stringifyFuncResults(f.Results)) } else { - sb.WriteString(stringifyExpr(field.Type)) + sb.WriteString(m.stringifyExpr(field.Type)) } } sb.WriteString("}") @@ -232,7 +232,7 @@ func stringifyInterfaceType(expr *ast.InterfaceType) string { return sb.String() } -func stringifyStructType(expr *ast.StructType) string { +func (m *mingo) stringifyStructType(expr *ast.StructType) string { sb := new(strings.Builder) sb.WriteString("struct{") @@ -249,7 +249,7 @@ func stringifyStructType(expr *ast.StructType) string { if len(field.Names) > 0 { sb.WriteString(" ") } - sb.WriteString(stringifyExpr(field.Type)) + sb.WriteString(m.stringifyExpr(field.Type)) if field.Tag != nil { sb.WriteString(" ") @@ -261,13 +261,13 @@ func stringifyStructType(expr *ast.StructType) string { return sb.String() } -func stringifyFuncType(expr *ast.FuncType) string { +func (m *mingo) stringifyFuncType(expr *ast.FuncType) string { sb := new(strings.Builder) sb.WriteString("func") - sb.WriteString(stringifyFuncTypeParams(expr.TypeParams)) - sb.WriteString(stringifyFuncParams(expr.Params)) - sb.WriteString(stringifyFuncResults(expr.Results)) + sb.WriteString(m.stringifyFuncTypeParams(expr.TypeParams)) + sb.WriteString(m.stringifyFuncParams(expr.Params)) + sb.WriteString(m.stringifyFuncResults(expr.Results)) return sb.String() } diff --git a/internal/mingo/file.go b/internal/mingo/file.go index 47692e1..795c41b 100644 --- a/internal/mingo/file.go +++ b/internal/mingo/file.go @@ -5,6 +5,6 @@ import ( "go/ast" ) -func stringifyFile(n *ast.File) string { +func (m *mingo) stringifyFile(n *ast.File) string { return fmt.Sprintf("package %s;", n.Name.Name) } diff --git a/internal/mingo/func.go b/internal/mingo/func.go index f023487..73c2235 100644 --- a/internal/mingo/func.go +++ b/internal/mingo/func.go @@ -6,29 +6,29 @@ import ( "strings" ) -func stringifyFuncDecl(n *ast.FuncDecl) string { +func (m *mingo) stringifyFuncDecl(n *ast.FuncDecl) string { sb := new(strings.Builder) sb.WriteString("func") if n.Recv != nil { - sb.WriteString(stringifyFuncParams(n.Recv)) + sb.WriteString(m.stringifyFuncParams(n.Recv)) } else { sb.WriteString(" ") } fmt.Fprintf(sb, "%s", n.Name.Name) - sb.WriteString(stringifyFuncTypeParams(n.Type.TypeParams)) - sb.WriteString(stringifyFuncParams(n.Type.Params)) - sb.WriteString(stringifyFuncResults(n.Type.Results)) - sb.WriteString(stringifyBlockStmt(n.Body)) + sb.WriteString(m.stringifyFuncTypeParams(n.Type.TypeParams)) + sb.WriteString(m.stringifyFuncParams(n.Type.Params)) + sb.WriteString(m.stringifyFuncResults(n.Type.Results)) + sb.WriteString(m.stringifyBlockStmt(n.Body)) sb.WriteString(";") return sb.String() } -func stringifyFuncTypeParams(params *ast.FieldList) string { +func (m *mingo) stringifyFuncTypeParams(params *ast.FieldList) string { if params == nil { return "" } @@ -49,14 +49,14 @@ func stringifyFuncTypeParams(params *ast.FieldList) string { if len(param.Names) > 0 { sb.WriteString(" ") } - sb.WriteString(stringifyExpr(param.Type)) + sb.WriteString(m.stringifyExpr(param.Type)) } sb.WriteString("]") return sb.String() } -func stringifyFuncParams(params *ast.FieldList) string { +func (m *mingo) stringifyFuncParams(params *ast.FieldList) string { sb := new(strings.Builder) sb.WriteString("(") @@ -76,14 +76,14 @@ func stringifyFuncParams(params *ast.FieldList) string { sb.WriteString(" ") } - sb.WriteString(stringifyExpr(arg.Type)) + sb.WriteString(m.stringifyExpr(arg.Type)) } sb.WriteString(")") return sb.String() } -func stringifyFuncResults(results *ast.FieldList) string { +func (m *mingo) stringifyFuncResults(results *ast.FieldList) string { if results == nil { return "" } @@ -110,7 +110,7 @@ func stringifyFuncResults(results *ast.FieldList) string { rb.WriteString(" ") } - rb.WriteString(stringifyExpr(rslt.Type)) + rb.WriteString(m.stringifyExpr(rslt.Type)) } if needParens { diff --git a/internal/mingo/mingo.go b/internal/mingo/mingo.go index a322054..3217c77 100644 --- a/internal/mingo/mingo.go +++ b/internal/mingo/mingo.go @@ -35,13 +35,13 @@ func (m *mingo) Minify(filename string, src []byte) (string, error) { ast.Inspect(file, func(n ast.Node) bool { switch x := n.(type) { case *ast.File: - fmt.Fprint(sb, stringifyFile(x)) + fmt.Fprint(sb, m.stringifyFile(x)) return true case *ast.GenDecl: - fmt.Fprint(sb, stringifyGenDecl(x)) + fmt.Fprint(sb, m.stringifyGenDecl(x)) return true case *ast.FuncDecl: - fmt.Fprint(sb, stringifyFuncDecl(x)) + fmt.Fprint(sb, m.stringifyFuncDecl(x)) return true } return false diff --git a/internal/mingo/stmt.go b/internal/mingo/stmt.go index edfb8c8..9e175a0 100644 --- a/internal/mingo/stmt.go +++ b/internal/mingo/stmt.go @@ -7,59 +7,59 @@ import ( "strings" ) -func stringifyStmt(stmt ast.Stmt) string { +func (m *mingo) stringifyStmt(stmt ast.Stmt) string { switch x := stmt.(type) { case *ast.ReturnStmt: - return stringifyReturnStmt(x) + return m.stringifyReturnStmt(x) case *ast.AssignStmt: - return stringifyAssignStmt(x) + return m.stringifyAssignStmt(x) case *ast.IfStmt: - return stringifyIfStmt(x) + return m.stringifyIfStmt(x) case *ast.BlockStmt: - return stringifyBlockStmt(x) + return m.stringifyBlockStmt(x) case *ast.ExprStmt: - return stringifyExprStmt(x) + return m.stringifyExprStmt(x) case *ast.DeclStmt: - return stringifyDeclStmt(x) + return m.stringifyDeclStmt(x) case *ast.DeferStmt: - return stringifyDeferStmt(x) + return m.stringifyDeferStmt(x) case *ast.GoStmt: - return stringifyGoStmt(x) + return m.stringifyGoStmt(x) case *ast.LabeledStmt: - return stringifyLabeledStmt(x) + return m.stringifyLabeledStmt(x) case *ast.SwitchStmt: - return stringifySwitchStmt(x) + return m.stringifySwitchStmt(x) case *ast.SelectStmt: - return stringifySelectStmt(x) + return m.stringifySelectStmt(x) case *ast.ForStmt: - return stringifyForStmt(x) + return m.stringifyForStmt(x) case *ast.RangeStmt: - return stringifyRangeStmt(x) + return m.stringifyRangeStmt(x) case *ast.BranchStmt: - return stringifyBranchStmt(x) + return m.stringifyBranchStmt(x) case *ast.EmptyStmt: - return stringifyEmptyStmt(x) + return m.stringifyEmptyStmt(x) case *ast.IncDecStmt: - return stringifyIncDecStmt(x) + return m.stringifyIncDecStmt(x) case *ast.SendStmt: - return stringifySendStmt(x) + return m.stringifySendStmt(x) case *ast.CaseClause: - return stringifyCaseCaluse(x) + return m.stringifyCaseCaluse(x) case *ast.CommClause: - return stringifyCommClause(x) + return m.stringifyCommClause(x) case *ast.TypeSwitchStmt: - return stringifyTypeSwitchStmt(x) + return m.stringifyTypeSwitchStmt(x) default: panic(fmt.Sprintf("unhandled stmt: %#v", x)) } } -func stringifyBlockStmt(stmt *ast.BlockStmt) string { +func (m *mingo) stringifyBlockStmt(stmt *ast.BlockStmt) string { sb := new(strings.Builder) sb.WriteString("{") for i, child := range stmt.List { - sb.WriteString(stringifyStmt(child)) + sb.WriteString(m.stringifyStmt(child)) if _, ok := child.(*ast.DeclStmt); !ok { if i < len(stmt.List)-1 { @@ -71,7 +71,7 @@ func stringifyBlockStmt(stmt *ast.BlockStmt) string { return sb.String() } -func stringifyReturnStmt(stmt *ast.ReturnStmt) string { +func (m *mingo) stringifyReturnStmt(stmt *ast.ReturnStmt) string { sb := new(strings.Builder) sb.WriteString("return") @@ -83,20 +83,20 @@ func stringifyReturnStmt(stmt *ast.ReturnStmt) string { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(expr)) + sb.WriteString(m.stringifyExpr(expr)) } return sb.String() } -func stringifyAssignStmt(stmt *ast.AssignStmt) string { +func (m *mingo) stringifyAssignStmt(stmt *ast.AssignStmt) string { sb := new(strings.Builder) for i, expr := range stmt.Lhs { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(expr)) + sb.WriteString(m.stringifyExpr(expr)) } if stmt.Tok == token.DEFINE { sb.WriteString(":=") @@ -107,155 +107,155 @@ func stringifyAssignStmt(stmt *ast.AssignStmt) string { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(expr)) + sb.WriteString(m.stringifyExpr(expr)) } return sb.String() } -func stringifyIfStmt(stmt *ast.IfStmt) string { +func (m *mingo) stringifyIfStmt(stmt *ast.IfStmt) string { sb := new(strings.Builder) sb.WriteString("if ") - sb.WriteString(stringifyIfStmtBody(stmt)) + sb.WriteString(m.stringifyIfStmtBody(stmt)) return sb.String() } -func stringifyElseIfStmt(stmt *ast.IfStmt) string { +func (m *mingo) stringifyElseIfStmt(stmt *ast.IfStmt) string { sb := new(strings.Builder) sb.WriteString("else if ") - sb.WriteString(stringifyIfStmtBody(stmt)) + sb.WriteString(m.stringifyIfStmtBody(stmt)) return sb.String() } -func stringifyIfStmtBody(stmt *ast.IfStmt) string { +func (m *mingo) stringifyIfStmtBody(stmt *ast.IfStmt) string { sb := new(strings.Builder) if stmt.Init != nil { - sb.WriteString(stringifyStmt(stmt.Init)) + sb.WriteString(m.stringifyStmt(stmt.Init)) sb.WriteString(";") } if stmt.Cond != nil { - sb.WriteString(stringifyExpr(stmt.Cond)) + sb.WriteString(m.stringifyExpr(stmt.Cond)) } if stmt.Body != nil { - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) } if stmt.Else != nil { if ifstmt, ok := stmt.Else.(*ast.IfStmt); ok { - sb.WriteString(stringifyElseIfStmt(ifstmt)) + sb.WriteString(m.stringifyElseIfStmt(ifstmt)) } else { sb.WriteString("else") - sb.WriteString(stringifyStmt(stmt.Else)) + sb.WriteString(m.stringifyStmt(stmt.Else)) } } return sb.String() } -func stringifyExprStmt(stmt *ast.ExprStmt) string { +func (m *mingo) stringifyExprStmt(stmt *ast.ExprStmt) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(stmt.X)) + sb.WriteString(m.stringifyExpr(stmt.X)) return sb.String() } -func stringifyDeclStmt(stmt *ast.DeclStmt) string { +func (m *mingo) stringifyDeclStmt(stmt *ast.DeclStmt) string { sb := new(strings.Builder) - sb.WriteString(stringifyDecl(stmt.Decl)) + sb.WriteString(m.stringifyDecl(stmt.Decl)) return sb.String() } -func stringifyDeferStmt(stmt *ast.DeferStmt) string { +func (m *mingo) stringifyDeferStmt(stmt *ast.DeferStmt) string { sb := new(strings.Builder) sb.WriteString("defer ") - sb.WriteString(stringifyExpr(stmt.Call)) + sb.WriteString(m.stringifyExpr(stmt.Call)) return sb.String() } -func stringifyGoStmt(stmt *ast.GoStmt) string { +func (m *mingo) stringifyGoStmt(stmt *ast.GoStmt) string { sb := new(strings.Builder) sb.WriteString("go ") - sb.WriteString(stringifyExpr(stmt.Call)) + sb.WriteString(m.stringifyExpr(stmt.Call)) return sb.String() } -func stringifyLabeledStmt(stmt *ast.LabeledStmt) string { +func (m *mingo) stringifyLabeledStmt(stmt *ast.LabeledStmt) string { sb := new(strings.Builder) sb.WriteString(fmt.Sprintf("%s:", stmt.Label.Name)) - sb.WriteString(stringifyStmt(stmt.Stmt)) + sb.WriteString(m.stringifyStmt(stmt.Stmt)) return sb.String() } -func stringifySwitchStmt(stmt *ast.SwitchStmt) string { +func (m *mingo) stringifySwitchStmt(stmt *ast.SwitchStmt) string { sb := new(strings.Builder) sb.WriteString("switch ") if stmt.Init != nil { - sb.WriteString(stringifyStmt(stmt.Init)) + sb.WriteString(m.stringifyStmt(stmt.Init)) sb.WriteString(";") } if stmt.Tag != nil { - sb.WriteString(stringifyExpr(stmt.Tag)) + sb.WriteString(m.stringifyExpr(stmt.Tag)) } if stmt.Body != nil { - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) } return sb.String() } -func stringifySelectStmt(stmt *ast.SelectStmt) string { +func (m *mingo) stringifySelectStmt(stmt *ast.SelectStmt) string { sb := new(strings.Builder) sb.WriteString("select ") - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) return sb.String() } -func stringifyForStmt(stmt *ast.ForStmt) string { +func (m *mingo) stringifyForStmt(stmt *ast.ForStmt) string { sb := new(strings.Builder) sb.WriteString("for ") if stmt.Init != nil { - sb.WriteString(stringifyStmt(stmt.Init)) + sb.WriteString(m.stringifyStmt(stmt.Init)) } if stmt.Init != nil || stmt.Post != nil { sb.WriteString(";") } if stmt.Cond != nil { - sb.WriteString(stringifyExpr(stmt.Cond)) + sb.WriteString(m.stringifyExpr(stmt.Cond)) } if stmt.Init != nil || stmt.Post != nil { sb.WriteString(";") } if stmt.Post != nil { - sb.WriteString(stringifyStmt(stmt.Post)) + sb.WriteString(m.stringifyStmt(stmt.Post)) } - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) return sb.String() } -func stringifyRangeStmt(stmt *ast.RangeStmt) string { +func (m *mingo) stringifyRangeStmt(stmt *ast.RangeStmt) string { sb := new(strings.Builder) sb.WriteString("for ") needAssign := false if stmt.Key != nil { needAssign = true - sb.WriteString(stringifyExpr(stmt.Key)) + sb.WriteString(m.stringifyExpr(stmt.Key)) } if stmt.Value != nil { needAssign = true sb.WriteString(",") - sb.WriteString(stringifyExpr(stmt.Value)) + sb.WriteString(m.stringifyExpr(stmt.Value)) } if needAssign { @@ -263,13 +263,13 @@ func stringifyRangeStmt(stmt *ast.RangeStmt) string { } sb.WriteString("range ") - sb.WriteString(stringifyExpr(stmt.X)) - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyExpr(stmt.X)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) return sb.String() } -func stringifyBranchStmt(stmt *ast.BranchStmt) string { +func (m *mingo) stringifyBranchStmt(stmt *ast.BranchStmt) string { sb := new(strings.Builder) sb.WriteString(stmt.Tok.String()) if stmt.Label != nil { @@ -279,28 +279,28 @@ func stringifyBranchStmt(stmt *ast.BranchStmt) string { return sb.String() } -func stringifyEmptyStmt(_ *ast.EmptyStmt) string { +func (m *mingo) stringifyEmptyStmt(_ *ast.EmptyStmt) string { return "" } -func stringifyIncDecStmt(stmt *ast.IncDecStmt) string { +func (m *mingo) stringifyIncDecStmt(stmt *ast.IncDecStmt) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(stmt.X)) + sb.WriteString(m.stringifyExpr(stmt.X)) sb.WriteString(stmt.Tok.String()) return sb.String() } -func stringifySendStmt(stmt *ast.SendStmt) string { +func (m *mingo) stringifySendStmt(stmt *ast.SendStmt) string { sb := new(strings.Builder) - sb.WriteString(stringifyExpr(stmt.Chan)) + sb.WriteString(m.stringifyExpr(stmt.Chan)) sb.WriteString("<-") - sb.WriteString(stringifyExpr(stmt.Value)) + sb.WriteString(m.stringifyExpr(stmt.Value)) return sb.String() } -func stringifyCaseCaluse(stmt *ast.CaseClause) string { +func (m *mingo) stringifyCaseCaluse(stmt *ast.CaseClause) string { sb := new(strings.Builder) if len(stmt.List) > 0 { @@ -309,14 +309,14 @@ func stringifyCaseCaluse(stmt *ast.CaseClause) string { if i > 0 { sb.WriteString(",") } - sb.WriteString(stringifyExpr(expr)) + sb.WriteString(m.stringifyExpr(expr)) } sb.WriteString(":") } else { sb.WriteString("default:") } for i, child := range stmt.Body { - sb.WriteString(stringifyStmt(child)) + sb.WriteString(m.stringifyStmt(child)) if i < len(stmt.Body)-1 { sb.WriteString(";") } @@ -325,36 +325,36 @@ func stringifyCaseCaluse(stmt *ast.CaseClause) string { return sb.String() } -func stringifyCommClause(stmt *ast.CommClause) string { +func (m *mingo) stringifyCommClause(stmt *ast.CommClause) string { sb := new(strings.Builder) if stmt.Comm != nil { sb.WriteString("case ") - sb.WriteString(stringifyStmt(stmt.Comm)) + sb.WriteString(m.stringifyStmt(stmt.Comm)) sb.WriteString(":") } else { sb.WriteString("default:") } for _, stmt := range stmt.Body { - sb.WriteString(stringifyStmt(stmt)) + sb.WriteString(m.stringifyStmt(stmt)) sb.WriteString(";") } return sb.String() } -func stringifyTypeSwitchStmt(stmt *ast.TypeSwitchStmt) string { +func (m *mingo) stringifyTypeSwitchStmt(stmt *ast.TypeSwitchStmt) string { sb := new(strings.Builder) sb.WriteString("switch ") if stmt.Init != nil { - sb.WriteString(stringifyStmt(stmt.Init)) + sb.WriteString(m.stringifyStmt(stmt.Init)) sb.WriteString(";") } if stmt.Assign != nil { - sb.WriteString(stringifyStmt(stmt.Assign)) + sb.WriteString(m.stringifyStmt(stmt.Assign)) } if stmt.Body != nil { - sb.WriteString(stringifyBlockStmt(stmt.Body)) + sb.WriteString(m.stringifyBlockStmt(stmt.Body)) } return sb.String()