Сценарий: Последовательность блоков: абстрактные и обычные классы - в своей последовательности

Реализация методов - отдельно
Этот коммит содержится в:
Softonik 2024-02-12 04:54:21 +03:00 коммит произвёл Nobody
родитель 88229f2bd5
коммит 6113b6be28
4 изменённых файлов: 61 добавлений и 45 удалений

Просмотреть файл

@ -7,6 +7,7 @@ import (
type Class interface { type Class interface {
Name() string Name() string
String() string String() string
MethodsString() string
AddMethod(*ast.FuncDecl) AddMethod(*ast.FuncDecl)
} }
@ -33,9 +34,13 @@ func (c *class) Name() string {
func (c *class) String() (code string) { func (c *class) String() (code string) {
code += c.classDefinitionToString() code += c.classDefinitionToString()
return
}
func (c *class) MethodsString() (code string) {
code += c.methodImplementationsToString() code += c.methodImplementationsToString()
return return
} }
func (c *class) classDefinitionToString() (code string) { func (c *class) classDefinitionToString() (code string) {
code = "class " + c.name + " {" code = "class " + c.name + " {"
code += "public:\n" code += "public:\n"

Просмотреть файл

@ -5,10 +5,6 @@ import (
"strings" "strings"
) )
var (
abstractClassDeclarations []Class
)
type abstractClass struct { type abstractClass struct {
name string name string
Interface *ast.InterfaceType Interface *ast.InterfaceType
@ -26,6 +22,10 @@ func (c *abstractClass) String() (code string) {
code += c.abstractClassDefinitionToString() code += c.abstractClassDefinitionToString()
return return
} }
func (c *abstractClass) MethodsString() (code string) {
return
}
func (c *abstractClass) abstractClassDefinitionToString() (code string) { func (c *abstractClass) abstractClassDefinitionToString() (code string) {
code = "class " + c.name + " {" code = "class " + c.name + " {"
code += "public:\n" code += "public:\n"
@ -93,5 +93,5 @@ func addAbstractClassDeclaration(c Class) {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()
abstractClassDeclarations = append(abstractClassDeclarations, c) classDeclarations = append(classDeclarations, c)
} }

Просмотреть файл

@ -302,11 +302,14 @@ func main() {
``` ```
* Результат: * Результат:
``` ```
void main();
class device { class device {
public: public:
}; };
void main();
device *dev1,*dev2; device *dev1,*dev2;
void main() { void main() {
dev1=new device(); dev1=new device();
} }
@ -333,12 +336,15 @@ func main() {
``` ```
* Результат: * Результат:
``` ```
Device* NewDevice();
void main();
class Device { class Device {
public: public:
}; };
Device* NewDevice();
void main();
Device *dev1,*dev2; Device *dev1,*dev2;
Device* NewDevice() { Device* NewDevice() {
return new Device(); return new Device();
} }
@ -347,7 +353,12 @@ dev1=NewDevice();
} }
``` ```
Сценарий: Последовательность блоков: инклюды, константы, типы, абстрактные классы, классы, переменные, определения функций, функции Сценарий: Последовательность блоков:
инклюды, константы, типы,
абстрактные классы/классы,
определения функций,
переменные,
методы классов, функции
* Исходник: * Исходник:
``` ```
package test package test
@ -378,23 +389,24 @@ const int c1 = 4;
typedef int Mera; typedef int Mera;
void someFunc(); class device {
public:
void doSomething();
};
class Device { class Device {
public: public:
virtual std::string Name() = 0; virtual std::string Name() = 0;
}; };
class device {
public: void someFunc();
void doSomething();
};
void device::doSomething() {
}
int a = 1; int a = 1;
int b = 1; int b = 1;
void device::doSomething() {
}
void someFunc() { void someFunc() {
} }
``` ```

Просмотреть файл

@ -89,7 +89,6 @@ func (s *defaultService) Start() error {
includeDeclarations = nil includeDeclarations = nil
constDeclarations = nil constDeclarations = nil
typeDeclarations = nil typeDeclarations = nil
abstractClassDeclarations = nil
classDeclarations = nil classDeclarations = nil
variableDeclarations = nil variableDeclarations = nil
funcDeclarations = nil funcDeclarations = nil
@ -112,10 +111,10 @@ func (s *defaultService) Start() error {
s.printIncludes() s.printIncludes()
s.printConstDeclarations() s.printConstDeclarations()
s.printTypeDeclarations() s.printTypeDeclarations()
s.printFunctionDeclarations()
s.printAbstractClassDeclarations()
s.printClassDeclarations() s.printClassDeclarations()
s.printFunctionDeclarations()
s.printVariableDeclarations() s.printVariableDeclarations()
s.printMethodImplementations()
s.printGoHelperDeclarations() s.printGoHelperDeclarations()
// Print the ordered result. // Print the ordered result.
@ -179,31 +178,6 @@ func (s *defaultService) printTypeDeclarations() {
} }
s.out.Write([]byte("\n")) s.out.Write([]byte("\n"))
} }
func (s *defaultService) printFunctionDeclarations() {
dlock.Lock()
defer dlock.Unlock()
for _, f := range funcDeclarations {
s.out.Write([]byte(f + "\n"))
if s.header != nil {
s.header.Write([]byte(f + "\n"))
}
}
s.out.Write([]byte("\n"))
}
func (s *defaultService) printAbstractClassDeclarations() {
dlock.Lock()
defer dlock.Unlock()
for _, c := range abstractClassDeclarations {
d := c.String()
s.out.Write([]byte(d + "\n"))
if s.header != nil {
s.header.Write([]byte(d + "\n"))
}
}
s.out.Write([]byte("\n"))
}
func (s *defaultService) printClassDeclarations() { func (s *defaultService) printClassDeclarations() {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()
@ -217,6 +191,18 @@ func (s *defaultService) printClassDeclarations() {
} }
s.out.Write([]byte("\n")) s.out.Write([]byte("\n"))
} }
func (s *defaultService) printFunctionDeclarations() {
dlock.Lock()
defer dlock.Unlock()
for _, f := range funcDeclarations {
s.out.Write([]byte(f + "\n"))
if s.header != nil {
s.header.Write([]byte(f + "\n"))
}
}
s.out.Write([]byte("\n"))
}
func (s *defaultService) printVariableDeclarations() { func (s *defaultService) printVariableDeclarations() {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()
@ -230,6 +216,19 @@ func (s *defaultService) printVariableDeclarations() {
} }
s.out.Write([]byte("\n")) s.out.Write([]byte("\n"))
} }
func (s *defaultService) printMethodImplementations() {
dlock.Lock()
defer dlock.Unlock()
for _, c := range classDeclarations {
d := c.MethodsString()
s.out.Write([]byte(d + "\n"))
if s.header != nil {
s.header.Write([]byte(d + "\n"))
}
}
s.out.Write([]byte("\n"))
}
func (s *defaultService) printGoHelperDeclarations() { func (s *defaultService) printGoHelperDeclarations() {
dlock.Lock() dlock.Lock()
defer dlock.Unlock() defer dlock.Unlock()