From 938bb8bba487ccef42b011904f02926e205e4b11 Mon Sep 17 00:00:00 2001 From: Softonik Date: Mon, 4 Oct 2021 03:04:17 +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=BB=D0=BD=D0=B0=D1=8F=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B0=20=D1=86=D0=B8?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=20for?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile/service.go | 23 +++++++++++++++++------ transpile/service_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/transpile/service.go b/transpile/service.go index 157833a..ccbe9fc 100644 --- a/transpile/service.go +++ b/transpile/service.go @@ -227,7 +227,7 @@ func handleBlockStmt(body *ast.BlockStmt) string { return code } for _, stmt := range body.List { - code += handleStmt(stmt) + code += handleStmt(stmt, false) } return code } @@ -245,7 +245,7 @@ func handleCaseClause(cc *ast.CaseClause) string { code += strings.Join(clauses, ",") code += ":" for _, body := range cc.Body { - code += handleStmt(body) + code += handleStmt(body, false) } return code } @@ -351,12 +351,14 @@ func handleSpecs(specs []ast.Spec) string { return code } -func handleStmt(stmt ast.Stmt) string { +func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string { code := "" switch s := stmt.(type) { case *ast.AssignStmt: code += handleAssignStmt(s) - code += ";" + if !standaloneAssignment { + code += ";" + } case *ast.BranchStmt: code += handleBranchStmt(s) case *ast.CaseClause: @@ -380,14 +382,23 @@ func handleStmt(stmt ast.Stmt) string { func handleForStmt(stmt *ast.ForStmt) string { code := "" + is_while := false if stmt.Init == nil && stmt.Post == nil { + is_while = true code += "while" } else { code += "for" } - code += "(" // stmt.Init + code += "(" + if !is_while { + code += handleStmt(stmt.Init, true) + code += ";" + } code += handleBinaryExpr(stmt.Cond) // stmt.Cond - code += "" // stmt.Post + if !is_while { + code += ";" + code += handleStmt(stmt.Post, true) + } code += ") {" code += handleBlockStmt(stmt.Body) // stmt.Body code += "}" diff --git a/transpile/service_test.go b/transpile/service_test.go index dd07a04..f4bc71b 100644 --- a/transpile/service_test.go +++ b/transpile/service_test.go @@ -586,6 +586,30 @@ var _ = Describe("Go Translator", func() { Compare(source, expected) }) }) + + Describe("Циклы", func() { + It("for i=0; i<10; i+=1", func() { + source := `package test + func Setup() {} + func Loop() { + var i int + for i=0; i<10; i+=1 { + i = i + } + } + ` + expected := ` + void setup() {} + void loop() { + int i; + for (i=0; i<10; i+=1) { + i = i; + } + } + ` + Compare(source, expected) + }) + }) }) func Compare(source, expected string) { @@ -595,6 +619,7 @@ func Compare(source, expected string) { err := service.Start() got := out.String() tgot, texpected := Trim(got), Trim(expected) + // spew.Dump(tgot) ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, tgot).To(Be(texpected)) }