diff --git a/transpile/service.go b/transpile/service.go index f195af9..50950d4 100644 --- a/transpile/service.go +++ b/transpile/service.go @@ -372,6 +372,8 @@ func handleStmt(stmt ast.Stmt) string { code += handleIfStmt(s) case *ast.SwitchStmt: code += handleSwitchStmt(s) + case *ast.ReturnStmt: + code += handleReturnStmt(s) } return code } @@ -401,6 +403,13 @@ func handleSwitchStmt(stmt *ast.SwitchStmt) string { return code } +func handleReturnStmt(stmt *ast.ReturnStmt) string { + code := "return " + code += handleExpr(stmt.Results[0]) + code += ";" + return code +} + func handleValueSpec(spec ast.Spec) string { s := spec.(*ast.ValueSpec) code := "" @@ -441,6 +450,8 @@ func handleValueSpecValues(values []ast.Expr) string { code += handleBasicLit(v) case *ast.SelectorExpr: code += handleSelectorExpr(value) + case *ast.CallExpr: + code += handleCallExpr(v) } } return code diff --git a/transpile/service_test.go b/transpile/service_test.go index aabac3e..b9bf363 100644 --- a/transpile/service_test.go +++ b/transpile/service_test.go @@ -419,7 +419,6 @@ var _ = Describe("Go Translator", func() { serial.Println("Connecting ...") } serial.Println("Connected!") - return nil } func Loop() {} ` @@ -486,6 +485,87 @@ var _ = Describe("Go Translator", func() { 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) {