From ee76ec40a8ff8fb8d4622c01ce8991cd46dbb4df Mon Sep 17 00:00:00 2001 From: Softonik Date: Fri, 9 Dec 2022 01:04:27 +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=20=D0=B1=D0=B5=D0=B7=D1=8B=D0=BC=D1=8F=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20import=20=D0=B8=20=D0=BE=D0=B1=D0=B0=20=D0=B2?= =?UTF-8?q?=D0=B8=D0=B4=D0=B0=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=B2=20=D1=81=D0=BE=D0=BE=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=82=D0=B2=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile_python/service.go | 15 ++----- transpile_python/service_test.go | 75 +++++++++++++++++++------------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/transpile_python/service.go b/transpile_python/service.go index fa24b80..a08e9dd 100644 --- a/transpile_python/service.go +++ b/transpile_python/service.go @@ -457,18 +457,11 @@ func handleIfStmt(stmt *ast.IfStmt) string { func handleImportSpec(spec ast.Spec) string { s := spec.(*ast.ImportSpec) code := "" - if s.Name != nil { - name := handleIdent(s.Name) - if val, ok := mapping[name]; ok { - name = val - } - if name != "" { - if name != "controller" { - code = "#include <" + name + ".h>\n" - } - } + libName := strings.Trim(s.Path.Value, "\"") + if s.Name == nil { + code = "import " + libName + "\n" } else { - code = "from " + strings.Trim(s.Path.Value, "\"") + " import *\n" + code = "from " + libName + " import *\n" } return code } diff --git a/transpile_python/service_test.go b/transpile_python/service_test.go index 7f7ed33..67d4ea8 100644 --- a/transpile_python/service_test.go +++ b/transpile_python/service_test.go @@ -27,7 +27,7 @@ var _ = Describe("Go Translator to Python/Skidl", func() { It("Importing skidl", func() { source := `package test import ( - "skidl" + . "skidl" ) ` expected := `from skidl import * @@ -40,13 +40,28 @@ main() It("Importing skidl and FreeCAD", func() { source := `package test import ( - "skidl" - "FreeCAD" + . "skidl" + . "FreeCAD" ) ` expected := `from skidl import * from FreeCAD import * +main() +` + Compare(source, expected) + }) + + It("Importing inline", func() { + source := `package test + import ( + "skidl" + "FreeCAD" + ) + ` + expected := `import skidl +import FreeCAD + main() ` Compare(source, expected) @@ -54,7 +69,7 @@ main() It("Variables declaration", func() { source := `package test - import "skidl" + import . "skidl" var ( a = 1 @@ -81,7 +96,7 @@ main() It("Simple bool assignment", func() { source := `package test - import "skidl" + import . "skidl" func main() { t := true @@ -100,7 +115,7 @@ main() It("Empty Array assignment", func() { source := `package test - import "skidl" + import . "skidl" func main() { a := []any{} @@ -119,7 +134,7 @@ main() It("Inited array assignment", func() { source := `package test - import "skidl" + import . "skidl" func main() { a := []any{1, 2, x} @@ -136,7 +151,7 @@ main() It("Array append", func() { source := `package test - import "skidl" + import . "skidl" func main() { append(a, 1) @@ -153,7 +168,7 @@ main() It("Deep property assign", func() { source := `package test - import "skidl" + import . "skidl" func main() { a.b.c = 1 @@ -170,7 +185,7 @@ main() It("Function Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() {} ` @@ -185,7 +200,7 @@ main() It("Func call", func() { source := `package test - import "skidl" + import . "skidl" func main() { generate_netlist() @@ -202,7 +217,7 @@ main() It("Func return", func() { source := `package test - import "skidl" + import . "skidl" func myfunc(p1, p2 any) any { return 5 @@ -224,7 +239,7 @@ main() It("Net Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { vin := Net("3.3v") @@ -241,7 +256,7 @@ main() It("Nets Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { vin := Net("3.3v") @@ -260,7 +275,7 @@ main() It("Part Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") @@ -277,7 +292,7 @@ main() It("Parts Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") @@ -296,7 +311,7 @@ main() It("Values Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { esp32 := ESP32(value == "ESP32") @@ -315,7 +330,7 @@ main() It("Values with params Declaration", func() { source := `package test - import "skidl" + import . "skidl" func main() { tr_tok_verh := tr_npn(value == "tr Tok verh NPN") @@ -332,7 +347,7 @@ main() It("Assignments += by name ", func() { source := `package test - import "skidl" + import . "skidl" func main() { esp32["GND"] += gnd @@ -351,7 +366,7 @@ main() It("Assignments += by index", func() { source := `package test - import "skidl" + import . "skidl" func main() { esp32[1] += gnd @@ -372,7 +387,7 @@ main() XIt("Assignments += by index of function call", func() { source := `package test - import "skidl" + import . "skidl" func main() { vkl_iface = vkl_iface_part(1)[0] @@ -389,7 +404,7 @@ main() It("Assignments &", func() { source := `package test - import "skidl" + import . "skidl" func main() { tr_vkl["G"] & vkl @@ -410,7 +425,7 @@ main() It("Value Assignment", func() { source := `package test - import "skidl" + import . "skidl" func main() { vkl_iface.value = "k vkl" @@ -433,7 +448,7 @@ main() It("Condition", func() { source := `package test - import "skidl" + import . "skidl" func main() { if a > 2 { @@ -461,7 +476,7 @@ main() It("Condition complex", func() { source := `package test - import "skidl" + import . "skidl" func main() { if a > 2 { @@ -498,7 +513,7 @@ main() It("For with range", func() { source := `package test - import "skidl" + import . "skidl" func main() { for v := range list { @@ -518,7 +533,7 @@ main() It("Function decl/call", func() { source := `package test - import "skidl" + import . "skidl" func vdiv(inp, outp, param string) { r1 := r(value == 1000) @@ -551,7 +566,7 @@ main() It("Subcircuit function decorator", func() { source := `package test - import "skidl" + import . "skidl" //@subcircuit func vdiv(inp, outp, param string) { @@ -586,7 +601,7 @@ main() It("Package function decorator", func() { source := `package test - import "skidl" + import . "skidl" //@package func vdiv(inp, outp, param string) { @@ -621,7 +636,7 @@ main() It("Interfaces", func() { source := `package test - import "skidl" + import . "skidl" //@subcircuit func mem_module(intfc any) {