From f92c01d1a7c34de93d8498ef75d769fc5ec68a2c Mon Sep 17 00:00:00 2001 From: Softonik Date: Tue, 22 Nov 2022 02:29:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B8=D0=BF=D1=8B=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B9=20=D0=B2=20=D0=BE=D0=B1=D1=8A=D1=8F?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=D1=85=20=D0=B8=20=D0=BE?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile/service.go | 16 ++++++++++++---- transpile/service_test.go | 25 ++++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/transpile/service.go b/transpile/service.go index 20bc8ab..0252634 100644 --- a/transpile/service.go +++ b/transpile/service.go @@ -311,9 +311,6 @@ func handleFuncDecl(decl ast.Decl) string { fp := handleFuncDeclParams(fd.Type) code += fp - if ft == "" { - ft = "int" - } addFunctionDeclaration(ft + " " + name + "(" + fp + ");") code += ") {" @@ -417,8 +414,19 @@ func handleFuncDeclName(ident *ast.Ident) string { func handleFuncDeclType(t *ast.FuncType) string { code := "" if t.Results == nil { - code = "void" + return "void" } + + fl := t.Results + if fl.NumFields() == 0 { + panic("handleFuncDeclType: fl.NumFields() == 0") + } + + switch ft := fl.List[0].Type.(type) { + case *ast.Ident: + code += handleIdent(ft) + } + return code } diff --git a/transpile/service_test.go b/transpile/service_test.go index 235a70d..1fc8adb 100644 --- a/transpile/service_test.go +++ b/transpile/service_test.go @@ -283,7 +283,7 @@ var _ = Describe("Go Translator", func() { void foo() { foo2(); } - foo2() { + int foo2() { return 0; } ` @@ -708,7 +708,7 @@ var _ = Describe("Go Translator", func() { void setup() {} void loop() { } - MyFunction() { + int MyFunction() { } ` Compare(source, expected) @@ -731,7 +731,7 @@ var _ = Describe("Go Translator", func() { void setup() {} void loop() { } - MyFunction() { + int MyFunction() { return 0; } ` @@ -755,7 +755,7 @@ var _ = Describe("Go Translator", func() { void setup() {} void loop() { } - MyFunction() { + int MyFunction() { return -1; } ` @@ -781,12 +781,27 @@ var _ = Describe("Go Translator", func() { void loop() { int x = MyFunction(); } - MyFunction() { + int MyFunction() { return 0; } ` Compare(source, expected) }) + It("Объявление bool функции с return false", func() { + source := `package test + func MyFunction() bool { + return false + } + ` + expected := ` + bool MyFunction(); + + bool MyFunction() { + return false; + } + ` + Compare(source, expected) + }) }) Describe("Циклы", func() {