diff --git a/transpile_python/service.go b/transpile_python/service.go index 15c1db3..491b585 100644 --- a/transpile_python/service.go +++ b/transpile_python/service.go @@ -56,7 +56,7 @@ func (s *defaultService) Start() error { } // If source has no declarations then main it to an empty for loop. if file.Decls == nil { - fmt.Fprint(s.out, "from skidl import *\n") + fmt.Fprint(s.out, "") return nil } // Use Goroutines to work concurrently. @@ -79,7 +79,7 @@ func (s *defaultService) Start() error { } } - s.out.Write([]byte("from skidl import *\n")) + // s.out.Write([]byte("from skidl import *\n")) // Print the ordered result. for i := 0; i < count; i++ { @@ -425,6 +425,8 @@ func handleImportSpec(spec ast.Spec) string { code = "#include <" + name + ".h>\n" } } + } else { + code = "from " + strings.Trim(s.Path.Value, "\"") + " import *\n" } return code } @@ -473,6 +475,7 @@ func handleSpecs(specs []ast.Spec) string { code += handleValueSpec(spec) + ";" } } + code += "\n" return code } diff --git a/transpile_python/service_test.go b/transpile_python/service_test.go index 2e03ab6..cb92b48 100644 --- a/transpile_python/service_test.go +++ b/transpile_python/service_test.go @@ -20,16 +20,46 @@ var _ = Describe("Go Translator to Python/Skidl", func() { Describe("All", func() { It("Empty Package", func() { source := `package test` + expected := `` + Compare(source, expected) + }) + + It("Importing skidl", func() { + source := `package test + import ( + "skidl" + ) + ` expected := `from skidl import * + +main() +` + Compare(source, expected) + }) + + It("Importing skidl and FreeCAD", func() { + source := `package test + import ( + "skidl" + "FreeCAD" + ) + ` + expected := `from skidl import * +from FreeCAD import * + +main() ` Compare(source, expected) }) It("Function Declaration", func() { source := `package test + import "skidl" + func main() {} ` expected := `from skidl import * + def main(): None main() @@ -39,11 +69,14 @@ main() It("Func call", func() { source := `package test + import "skidl" + func main() { generate_netlist() } ` expected := `from skidl import * + def main(): generate_netlist() main() @@ -53,6 +86,8 @@ main() It("Func return", func() { source := `package test + import "skidl" + func myfunc(p1, p2 any) any { return 5 } @@ -61,6 +96,7 @@ main() } ` expected := `from skidl import * + def myfunc(p1,p2): return 5 def main(): @@ -72,11 +108,14 @@ main() It("Net Declaration", func() { source := `package test + import "skidl" + func main() { vin := Net("3.3v") } ` expected := `from skidl import * + def main(): vin = Net("3.3v") main() @@ -86,12 +125,15 @@ main() It("Nets Declaration", func() { source := `package test + import "skidl" + func main() { vin := Net("3.3v") gnd := Net("GND") } ` expected := `from skidl import * + def main(): vin = Net("3.3v") gnd = Net("GND") @@ -102,11 +144,14 @@ main() It("Part Declaration", func() { source := `package test + import "skidl" + func main() { ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") } ` expected := `from skidl import * + def main(): ESP32 = Part("RF_Module","ESP32-WROOM-32",TEMPLATE,footprint="RF_Module:ESP32-WROOM-32") main() @@ -116,12 +161,15 @@ main() It("Parts Declaration", func() { source := `package test + import "skidl" + func main() { ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") Klema := Part("Connector_Generic_MountingPin", "Conn_01x01_MountingPin", TEMPLATE, footprint == "TestPoint_Pad_2.0x2.0mm") } ` expected := `from skidl import * + def main(): ESP32 = Part("RF_Module","ESP32-WROOM-32",TEMPLATE,footprint="RF_Module:ESP32-WROOM-32") Klema = Part("Connector_Generic_MountingPin","Conn_01x01_MountingPin",TEMPLATE,footprint="TestPoint_Pad_2.0x2.0mm") @@ -132,12 +180,15 @@ main() It("Values Declaration", func() { source := `package test + import "skidl" + func main() { esp32 := ESP32(value == "ESP32") k_vin, k_gnd := Klema(2) } ` expected := `from skidl import * + def main(): esp32 = ESP32(value="ESP32") k_vin,k_gnd = Klema(2) @@ -148,11 +199,14 @@ main() It("Values with params Declaration", func() { source := `package test + import "skidl" + func main() { tr_tok_verh := tr_npn(value == "tr Tok verh NPN") } ` expected := `from skidl import * + def main(): tr_tok_verh = tr_npn(value="tr Tok verh NPN") main() @@ -162,12 +216,15 @@ main() It("Assignments += by name ", func() { source := `package test + import "skidl" + func main() { esp32["GND"] += gnd esp32["VDD"] += vin } ` expected := `from skidl import * + def main(): esp32["GND"] += gnd esp32["VDD"] += vin @@ -178,6 +235,8 @@ main() It("Assignments += by index", func() { source := `package test + import "skidl" + func main() { esp32[1] += gnd vin += r_fb[1] @@ -185,6 +244,7 @@ main() } ` expected := `from skidl import * + def main(): esp32[1] += gnd vin += r_fb[1] @@ -196,11 +256,14 @@ main() XIt("Assignments += by index of function call", func() { source := `package test + import "skidl" + func main() { vkl_iface = vkl_iface_part(1)[0] } ` expected := `from skidl import * + def main(): vkl_iface = vkl_iface_part(1)[0] main() @@ -210,6 +273,8 @@ main() It("Assignments &", func() { source := `package test + import "skidl" + func main() { tr_vkl["G"] & vkl tr_vkl["G"] & tr2["S"] @@ -217,6 +282,7 @@ main() } ` expected := `from skidl import * + def main(): tr_vkl["G"] & vkl tr_vkl["G"] & tr2["S"] @@ -228,6 +294,8 @@ main() It("Value Assignment", func() { source := `package test + import "skidl" + func main() { vkl_iface.value = "k vkl" vkl_iface.value = 19 @@ -236,6 +304,7 @@ main() } ` expected := `from skidl import * + def main(): vkl_iface.value = "k vkl" vkl_iface.value = 19 @@ -248,6 +317,8 @@ main() It("Condition", func() { source := `package test + import "skidl" + func main() { if a > 2 { x = 1 @@ -259,6 +330,7 @@ main() } ` expected := `from skidl import * + def main(): if a > 2: x = 1 @@ -273,6 +345,8 @@ main() It("Condition complex", func() { source := `package test + import "skidl" + func main() { if a > 2 { x = 1 @@ -289,6 +363,7 @@ main() } ` expected := `from skidl import * + def main(): if a > 2: x = 1 @@ -307,6 +382,8 @@ main() It("Function decl/call", func() { source := `package test + import "skidl" + func vdiv(inp, outp, param string) { r1 := r(value == 1000) r2 := r(value == param) @@ -321,6 +398,7 @@ main() } ` expected := `from skidl import * + def vdiv(inp,outp,param): r1 = r(value=1000) r2 = r(value=param) @@ -337,6 +415,8 @@ main() It("Subcircuit function decorator", func() { source := `package test + import "skidl" + //@subcircuit func vdiv(inp, outp, param string) { r1 := r(value == 1000) @@ -352,6 +432,7 @@ main() } ` expected := `from skidl import * + @subcircuit def vdiv(inp,outp,param): r1 = r(value=1000) @@ -369,6 +450,8 @@ main() It("Package function decorator", func() { source := `package test + import "skidl" + //@package func vdiv(inp, outp, param string) { r1 := r(value == 1000) @@ -384,6 +467,7 @@ main() } ` expected := `from skidl import * + @package def vdiv(inp,outp,param): r1 = r(value=1000) @@ -401,6 +485,8 @@ main() It("Interfaces", func() { source := `package test + import "skidl" + //@subcircuit func mem_module(intfc any) { ram := Part("Memory_RAM", "AS6C1616") @@ -421,6 +507,7 @@ main() } ` expected := `from skidl import * + @subcircuit def mem_module(intfc): ram = Part("Memory_RAM","AS6C1616")