Добавлена полная поддержка цикла 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 return code
} }
for _, stmt := range body.List { for _, stmt := range body.List {
code += handleStmt(stmt) code += handleStmt(stmt, false)
} }
return code return code
} }
@ -245,7 +245,7 @@ func handleCaseClause(cc *ast.CaseClause) string {
code += strings.Join(clauses, ",") code += strings.Join(clauses, ",")
code += ":" code += ":"
for _, body := range cc.Body { for _, body := range cc.Body {
code += handleStmt(body) code += handleStmt(body, false)
} }
return code return code
} }
@ -351,12 +351,14 @@ func handleSpecs(specs []ast.Spec) string {
return code return code
} }
func handleStmt(stmt ast.Stmt) string { func handleStmt(stmt ast.Stmt, standaloneAssignment bool) string {
code := "" code := ""
switch s := stmt.(type) { switch s := stmt.(type) {
case *ast.AssignStmt: case *ast.AssignStmt:
code += handleAssignStmt(s) code += handleAssignStmt(s)
if !standaloneAssignment {
code += ";" code += ";"
}
case *ast.BranchStmt: case *ast.BranchStmt:
code += handleBranchStmt(s) code += handleBranchStmt(s)
case *ast.CaseClause: case *ast.CaseClause:
@ -380,14 +382,23 @@ func handleStmt(stmt ast.Stmt) string {
func handleForStmt(stmt *ast.ForStmt) string { func handleForStmt(stmt *ast.ForStmt) string {
code := "" code := ""
is_while := false
if stmt.Init == nil && stmt.Post == nil { if stmt.Init == nil && stmt.Post == nil {
is_while = true
code += "while" code += "while"
} else { } else {
code += "for" code += "for"
} }
code += "(" // stmt.Init code += "("
if !is_while {
code += handleStmt(stmt.Init, true)
code += ";"
}
code += handleBinaryExpr(stmt.Cond) // stmt.Cond code += handleBinaryExpr(stmt.Cond) // stmt.Cond
code += "" // stmt.Post if !is_while {
code += ";"
code += handleStmt(stmt.Post, true)
}
code += ") {" code += ") {"
code += handleBlockStmt(stmt.Body) // stmt.Body code += handleBlockStmt(stmt.Body) // stmt.Body
code += "}" code += "}"

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

@ -586,6 +586,30 @@ var _ = Describe("Go Translator", func() {
Compare(source, expected) 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) { func Compare(source, expected string) {
@ -595,6 +619,7 @@ func Compare(source, expected string) {
err := service.Start() err := service.Start()
got := out.String() got := out.String()
tgot, texpected := Trim(got), Trim(expected) tgot, texpected := Trim(got), Trim(expected)
// spew.Dump(tgot)
ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, tgot).To(Be(texpected)) ExpectWithOffset(1, tgot).To(Be(texpected))
} }