Сценарий: Структура с обращением к полям

Этот коммит содержится в:
Softonik 2024-02-12 19:31:56 +03:00 коммит произвёл Nobody
родитель ba57ec17b1
коммит bcfa576806
3 изменённых файлов: 74 добавлений и 0 удалений

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

@ -7,7 +7,9 @@ import (
)
func handleExpr(expr ast.Expr) (code string) {
// println()
// spew.Dump(expr)
switch e := expr.(type) {
case *ast.BasicLit:
code += handleBasicLit(e)
@ -23,6 +25,8 @@ func handleExpr(expr ast.Expr) (code string) {
code += handleParenExpr(e)
case *ast.SelectorExpr:
code += handleSelectorExpr(e)
case *ast.IndexExpr:
code += handleIndexExpr(e)
case *ast.StructType:
code += handleStructType(e)
case *ast.InterfaceType:
@ -137,6 +141,27 @@ func handleSelectorExpr(s *ast.SelectorExpr) (code string) {
return
}
func handleIndexExpr(ind *ast.IndexExpr) (code string) {
// println()
// spew.Dump(ind)
switch x := ind.X.(type) {
case *ast.SelectorExpr:
if isInMethod {
code += "this"
} else {
code += handleExpr(x.X)
}
code += "->"
code += handleIdentExpr(x.Sel)
code += "["
code += handleExpr(ind.Index)
code += "]"
}
return
}
func handleStarExpr(s *ast.StarExpr) (code string) {
switch x := s.X.(type) {
case *ast.Ident:

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

@ -307,6 +307,50 @@ void device::doSomething() {
this->x=1;
this->x=this->y;
}
```
Сценарий: Структура с обращением к полям
* Исходник:
```
package test
type device struct {
g [8]bool
}
func (dev *device) doSomething(a *int) {
if dev.g[0] {
a:=1
}
}
func some(gs *GPIOS) {
if gs.g[0] {
a:=1
}
}
```
* Результат:
```
class device {
public:
bool g[8];
void doSomething(int* a);
};
void some(GPIOS* gs);
void device::doSomething(int* a) {
if (this->g[0]) {
auto a=1;
}
}
void some(GPIOS* gs) {
if (gs->g[0]) {
auto a=1;
}
}
```
Сценарий: Структура с вызовом метода и свойства другого объекта

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

@ -21,6 +21,8 @@ func handleBlockStmt(body *ast.BlockStmt) string {
return code
}
func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
// println()
// spew.Dump(stmt)
code := ""
switch s := stmt.(type) {
case *ast.AssignStmt:
@ -182,6 +184,9 @@ func handleForStmt(stmt *ast.ForStmt) string {
}
func handleIfStmt(stmt *ast.IfStmt) string {
// println()
// spew.Dump(stmt)
cond := handleExpr(stmt.Cond)
body := handleBlockStmt(stmt.Body)
code := fmt.Sprintf("if (%s) {\n %s }\n", cond, body)