From 80160b100184e17e70fa50cba17d33cb304f1b81 Mon Sep 17 00:00:00 2001 From: Softonik Date: Thu, 15 Dec 2022 06:58:47 +0300 Subject: [PATCH] =?UTF-8?q?Python:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=3D=3D=20=D0=B2=20if?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile_python/service.go | 42 +++++++++++++++++++++++++++++++- transpile_python/service_test.go | 30 ++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/transpile_python/service.go b/transpile_python/service.go index 1f3d828..301f941 100644 --- a/transpile_python/service.go +++ b/transpile_python/service.go @@ -183,6 +183,18 @@ func handleBinaryExpr(expr ast.Expr) string { code += handleExpr(be.Y) 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 { // spew.Dump(expr) @@ -262,6 +274,34 @@ func handleExpr(expr ast.Expr) string { } 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 { code := "" @@ -449,7 +489,7 @@ func handleIdent(expr ast.Expr) string { } func handleIfStmt(stmt *ast.IfStmt) string { - cond := handleExpr(stmt.Cond) + cond := handleExpr_FromIfStmt(stmt.Cond) increaseInd() body := handleBlockStmt(stmt.Body) decreaseInd() diff --git a/transpile_python/service_test.go b/transpile_python/service_test.go index d1f82d4..d27e569 100644 --- a/transpile_python/service_test.go +++ b/transpile_python/service_test.go @@ -503,7 +503,35 @@ main() 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 import . "skidl"