87 строки
1,9 КиБ
Go
87 строки
1,9 КиБ
Go
package service
|
|
|
|
import (
|
|
"go/ast"
|
|
"strings"
|
|
)
|
|
|
|
type abstractClass struct {
|
|
name string
|
|
Interface *ast.InterfaceType
|
|
}
|
|
|
|
func NewAbstractClass(name string) *abstractClass {
|
|
return &abstractClass{name: name}
|
|
}
|
|
|
|
func (c *abstractClass) Name() string {
|
|
return c.name
|
|
}
|
|
|
|
func (c *abstractClass) String() (code string) {
|
|
code += c.abstractClassDefinitionToString()
|
|
return
|
|
}
|
|
func (c *abstractClass) abstractClassDefinitionToString() (code string) {
|
|
code = "class " + c.name + " {"
|
|
code += "public:\n"
|
|
|
|
code += c.methodDeclarationsToString()
|
|
code += "\n"
|
|
|
|
code += "};\n"
|
|
return
|
|
}
|
|
func (c *abstractClass) methodDeclarationsToString() (code string) {
|
|
for _, f := range c.Interface.Methods.List {
|
|
code += c.fieldDeclarationToString(f)
|
|
}
|
|
code += "\n"
|
|
return
|
|
}
|
|
func (c *abstractClass) fieldDeclarationToString(f *ast.Field) (code string) {
|
|
code += " virtual "
|
|
switch s := f.Type.(type) {
|
|
case *ast.FuncType:
|
|
code += handleFuncDeclType(s)
|
|
code += " "
|
|
code += handleIdentExpr(f.Names[0])
|
|
code += "("
|
|
code += handleAbstractFuncDeclParams(s)
|
|
code += ") = 0;"
|
|
}
|
|
|
|
return
|
|
}
|
|
func handleAbstractFuncDeclParams(t *ast.FuncType) (code string) {
|
|
if t.Params == nil || t.Params.List == nil {
|
|
return
|
|
}
|
|
values := make([]string, 0)
|
|
for _, field := range t.Params.List {
|
|
switch ft := field.Type.(type) {
|
|
case *ast.Ident:
|
|
t := handleIdentExpr(ft)
|
|
values = append(values, t)
|
|
}
|
|
}
|
|
code += strings.Join(values, ",")
|
|
return
|
|
}
|
|
|
|
func (c *abstractClass) AddMethod(m *ast.FuncDecl) {}
|
|
|
|
func handleClassAbstract(name string, it *ast.InterfaceType) string {
|
|
addAbstractClassFromStructType(name, it)
|
|
return ""
|
|
}
|
|
func addAbstractClassFromStructType(name string, it *ast.InterfaceType) Class {
|
|
c := createAbstractClass(name)
|
|
c.Interface = it
|
|
return c
|
|
}
|
|
func createAbstractClass(className string) *abstractClass {
|
|
c := NewAbstractClass(className)
|
|
addClassDeclaration(c)
|
|
return c
|
|
}
|