Функциональность: Обращения к полям - всегда через указатель ->

Этот коммит содержится в:
Softonik 2024-02-14 04:22:08 +03:00 коммит произвёл Nobody
родитель f4fcbd57c4
коммит bbb46751ee
7 изменённых файлов: 137 добавлений и 66 удалений

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

@ -115,6 +115,9 @@ func handleParenExpr(stmt *ast.ParenExpr) string {
}
func handleSelectorExpr(s *ast.SelectorExpr) (code string) {
// println()
// spew.Dump(s)
switch x := s.X.(type) {
case *ast.Ident:
if isInMethod {
@ -126,7 +129,7 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) {
code += "->"
} else {
code += handleIdentExpr(x)
code += "."
code += "->"
}
case *ast.SelectorExpr:
code += "this"
@ -135,9 +138,7 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) {
code += "->"
}
code += handleIdentExpr(s.Sel)
if val, ok := mapping[code]; ok {
code = val
}
code = RemapCode(code)
return
}

116
pkg/service/features/selecting.feature Обычный файл
Просмотреть файл

@ -0,0 +1,116 @@
# Во имя Бога Милостивого, Милосердного!!!
# language: ru
Функциональность: Обращения к полям
Сценарий: Вызов метода
* Исходник:
```
package test
func foo() {
foo.Bar(1,"2")
}
```
* Результат:
```
void foo();
void foo() {
foo->Bar(1,"2");
}
```
Сценарий: Вызов метода из пакета
* Исходник:
```
package test
func foo() {
x = bar()
y = pkg.Bar()
z = x + y
}
```
* Результат:
```
void foo();
void foo() {
x=bar();
y=pkg->Bar();
z=x+y;
}
```
Сценарий: Вызов метода из пакета ещё
* Исходник:
```
package test
func foo() {
foo.Bar(1,"2",digital.Low)
}
```
* Результат:
```
void foo();
void foo() {
foo->Bar(1,"2",LOW);
}
```
Сценарий: Обращение к полям динамически созданного класса
* Исходник:
```
package test
type Device struct {
v int
}
func NewDevice() *Device {
d := &Device{}
d.v = 1
return d
}
```
* Результат:
```
class Device {
public: int v;
};
Device* NewDevice();
Device* NewDevice() {
auto d=new Device();
d->v=1;
return d;
}
```
Сценарий:
* Исходник:
```
package test
type Device struct {
v int
}
func NewDevice() *Device {
d := &Device{}
d.v = 1
return d
}
```
* Результат:
```
class Device {
public: int v;
};
Device* NewDevice();
Device* NewDevice() {
auto d=new Device();
d->v=1;
return d;
}
```

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

@ -117,9 +117,7 @@ func handleFuncDeclName(ident *ast.Ident) (code string) {
return
}
code += ident.Name
if val, ok := mapping[code]; ok {
code = val
}
code = RemapCode(code)
return
}

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

@ -24,9 +24,7 @@ func (i *Include) String() (code string) {
func (i *Include) genString(s *ast.ImportSpec) (code string) {
if s.Name != nil {
name := handleIdentExpr(s.Name)
if val, ok := mapping[name]; ok {
name = val
}
name = RemapCode(name)
if name != "" {
if name != "controller" {
code = "#include <" + name + ".h>\n"

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

@ -1,8 +1,9 @@
package service
import "strings"
var mapping = map[string]string{
"fmt.Sprint": "std::to_string",
"fmt->Sprint": "std::to_string",
"digital.Low": "LOW",
"digital.High": "HIGH",
"digital.ModeInput": "INPUT",
@ -111,3 +112,12 @@ var mapping = map[string]string{
"semaphore.SemaphoreGive": "xSemaphoreGive",
"log.Log_e": "log_e",
}
func RemapCode(code string) string {
check := strings.ReplaceAll(code, "->", ".")
if val, ok := mapping[check]; ok {
return val
}
return code
}

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

@ -138,22 +138,6 @@ var _ = Describe("Go Translator", func() {
Compare(source, expected)
})
It("Function_With_Package_Function_Call", func() {
source := `package test
func foo() {
foo.Bar(1,"2")
}
`
expected := `
void foo();
void foo() {
foo.Bar(1,"2");
}
`
Compare(source, expected)
})
It("Function_With_Assignments", func() {
source := `package test
func foo() {
@ -174,26 +158,6 @@ var _ = Describe("Go Translator", func() {
Compare(source, expected)
})
It("Function_With_Package_Selector_Assignments", func() {
source := `package test
func foo() {
x = bar()
y = pkg.Bar()
z = x + y
}
`
expected := `
void foo();
void foo() {
x = bar();
y = pkg.Bar();
z = x + y;
}
`
Compare(source, expected)
})
It("Function_Ident_Mapping", func() {
source := `package test
func foo() {
@ -210,22 +174,6 @@ var _ = Describe("Go Translator", func() {
Compare(source, expected)
})
It("Function_With_Ident_Param", func() {
source := `package test
func foo() {
foo.Bar(1,"2",digital.Low)
}
`
expected := `
void foo();
void foo() {
foo.Bar(1,"2",LOW);
}
`
Compare(source, expected)
})
It("Function_With_Function_Param", func() {
source := `package test
func foo() {

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

@ -23,6 +23,7 @@ func handleBlockStmt(body *ast.BlockStmt) string {
func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
// println()
// spew.Dump(stmt)
code := ""
switch s := stmt.(type) {
case *ast.AssignStmt:
@ -96,14 +97,13 @@ func isExpr0String(ee []ast.Expr) bool {
return false
}
func handleAssignStmtExpr(e []ast.Expr) string {
func handleAssignStmtExpr(e []ast.Expr) (code string) {
ops := make([]string, 0)
code := ""
for _, op := range e {
ops = append(ops, handleExpr(op))
}
code += strings.Join(ops, ",")
return code
return
}
func handleToken(t token.Token) (code string, new bool) {