Python: добавлена поддержка == в if
Этот коммит содержится в:
родитель
686ef65fd7
коммит
80160b1001
2 изменённых файлов: 70 добавлений и 2 удалений
|
@ -183,6 +183,18 @@ func handleBinaryExpr(expr ast.Expr) string {
|
||||||
code += handleExpr(be.Y)
|
code += handleExpr(be.Y)
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
func handleBinaryExpr_FromIfStmt(expr ast.Expr) string {
|
||||||
|
be := expr.(*ast.BinaryExpr)
|
||||||
|
code := handleExpr(be.X)
|
||||||
|
switch be.Op {
|
||||||
|
case token.EQL:
|
||||||
|
code += "=="
|
||||||
|
default:
|
||||||
|
code += " " + be.Op.String() + " "
|
||||||
|
}
|
||||||
|
code += handleExpr(be.Y)
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
func handleCallExpr(expr *ast.CallExpr) string {
|
func handleCallExpr(expr *ast.CallExpr) string {
|
||||||
// spew.Dump(expr)
|
// spew.Dump(expr)
|
||||||
|
@ -262,6 +274,34 @@ func handleExpr(expr ast.Expr) string {
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
func handleExpr_FromIfStmt(expr ast.Expr) string {
|
||||||
|
code := ""
|
||||||
|
// spew.Dump(expr)
|
||||||
|
switch e := expr.(type) {
|
||||||
|
case *ast.BasicLit:
|
||||||
|
code += handleBasicLit(e)
|
||||||
|
case *ast.CompositeLit:
|
||||||
|
code += handleCompositeLit(e)
|
||||||
|
case *ast.UnaryExpr:
|
||||||
|
code += handleUnaryExpr(e)
|
||||||
|
case *ast.BinaryExpr:
|
||||||
|
code += handleBinaryExpr_FromIfStmt(e)
|
||||||
|
case *ast.CallExpr:
|
||||||
|
code += handleCallExpr(e)
|
||||||
|
case *ast.Ident:
|
||||||
|
code += handleIdent(e)
|
||||||
|
case *ast.ParenExpr:
|
||||||
|
code += handleParenExpr(e)
|
||||||
|
case *ast.SelectorExpr:
|
||||||
|
code += handleSelectorExpr(e)
|
||||||
|
case *ast.IndexExpr:
|
||||||
|
code += handleIndexExpr(e)
|
||||||
|
default:
|
||||||
|
spew.Dump(e)
|
||||||
|
code += "Unknown in handleExpr"
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
func handleParenExpr(stmt *ast.ParenExpr) string {
|
func handleParenExpr(stmt *ast.ParenExpr) string {
|
||||||
code := ""
|
code := ""
|
||||||
|
@ -449,7 +489,7 @@ func handleIdent(expr ast.Expr) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleIfStmt(stmt *ast.IfStmt) string {
|
func handleIfStmt(stmt *ast.IfStmt) string {
|
||||||
cond := handleExpr(stmt.Cond)
|
cond := handleExpr_FromIfStmt(stmt.Cond)
|
||||||
increaseInd()
|
increaseInd()
|
||||||
body := handleBlockStmt(stmt.Body)
|
body := handleBlockStmt(stmt.Body)
|
||||||
decreaseInd()
|
decreaseInd()
|
||||||
|
|
|
@ -503,7 +503,35 @@ main()
|
||||||
Compare(source, expected)
|
Compare(source, expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Condition", func() {
|
It("Condition ==", func() {
|
||||||
|
source := `package test
|
||||||
|
import . "skidl"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if a == 2 {
|
||||||
|
x = 1
|
||||||
|
y = 5
|
||||||
|
} else {
|
||||||
|
x = 6
|
||||||
|
y = 9
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expected := `from skidl import *
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if a==2:
|
||||||
|
x = 1
|
||||||
|
y = 5
|
||||||
|
else:
|
||||||
|
x = 6
|
||||||
|
y = 9
|
||||||
|
main()
|
||||||
|
`
|
||||||
|
Compare(source, expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
It("Condition >", func() {
|
||||||
source := `package test
|
source := `package test
|
||||||
import . "skidl"
|
import . "skidl"
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче