Добавлена поддержка присвоения из функции при объявлении переменной

Этот коммит содержится в:
Softonik 2021-10-04 02:25:50 +03:00 коммит произвёл Nobody
родитель 54fb10eb6a
коммит 84e65f8d32
2 изменённых файлов: 92 добавлений и 1 удалений

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

@ -372,6 +372,8 @@ func handleStmt(stmt ast.Stmt) string {
code += handleIfStmt(s) code += handleIfStmt(s)
case *ast.SwitchStmt: case *ast.SwitchStmt:
code += handleSwitchStmt(s) code += handleSwitchStmt(s)
case *ast.ReturnStmt:
code += handleReturnStmt(s)
} }
return code return code
} }
@ -401,6 +403,13 @@ func handleSwitchStmt(stmt *ast.SwitchStmt) string {
return code return code
} }
func handleReturnStmt(stmt *ast.ReturnStmt) string {
code := "return "
code += handleExpr(stmt.Results[0])
code += ";"
return code
}
func handleValueSpec(spec ast.Spec) string { func handleValueSpec(spec ast.Spec) string {
s := spec.(*ast.ValueSpec) s := spec.(*ast.ValueSpec)
code := "" code := ""
@ -441,6 +450,8 @@ func handleValueSpecValues(values []ast.Expr) string {
code += handleBasicLit(v) code += handleBasicLit(v)
case *ast.SelectorExpr: case *ast.SelectorExpr:
code += handleSelectorExpr(value) code += handleSelectorExpr(value)
case *ast.CallExpr:
code += handleCallExpr(v)
} }
} }
return code return code

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

@ -419,7 +419,6 @@ var _ = Describe("Go Translator", func() {
serial.Println("Connecting ...") serial.Println("Connecting ...")
} }
serial.Println("Connected!") serial.Println("Connected!")
return nil
} }
func Loop() {} func Loop() {}
` `
@ -486,6 +485,87 @@ var _ = Describe("Go Translator", func() {
Compare(source, expected) Compare(source, expected)
}) })
}) })
Describe("Функции", func() {
It("Объявление void функции", func() {
source := `package test
func Setup() {}
func Loop() {
}
func MyFunction() {
}
`
expected := `
void setup() {}
void loop() {
}
void MyFunction() {
}
`
Compare(source, expected)
})
It("Объявление int функции", func() {
source := `package test
func Setup() {}
func Loop() {
}
func MyFunction() int {
}
`
expected := `
void setup() {}
void loop() {
}
MyFunction() {
}
`
Compare(source, expected)
})
It("Объявление int функции с return 0", func() {
source := `package test
func Setup() {}
func Loop() {
}
func MyFunction() int {
return 0
}
`
expected := `
void setup() {}
void loop() {
}
MyFunction() {
return 0;
}
`
Compare(source, expected)
})
It("Объявляет и вызывает функцию", func() {
source := `package test
func Setup() {}
func Loop() {
var x int = MyFunction()
}
func MyFunction() int {
return 0
}
`
expected := `
void setup() {}
void loop() {
int x = MyFunction();
}
MyFunction() {
return 0;
}
`
Compare(source, expected)
})
})
}) })
func Compare(source, expected string) { func Compare(source, expected string) {