From 84e65f8d3229c7e3f93b38f384e2ea033fea305f Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 4 Oct 2021 02:25:50 +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=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=BE=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=D0=B7=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D1=8A=D1=8F=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile/service.go | 11 ++++++ transpile/service_test.go | 82 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) 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) {