Добавлена полная поддержка цикла for

Этот коммит содержится в:
Softonik 2021-10-04 03:04:17 +03:00 коммит произвёл Nobody
родитель 7b79070a68
коммит 938bb8bba4
2 изменённых файлов: 42 добавлений и 6 удалений

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

@ -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 += "}"

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

@ -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))
}