Python: добавлен безымянный import и оба вида приведены в соответствие

Этот коммит содержится в:
Softonik 2022-12-09 01:04:27 +03:00 коммит произвёл Nikolay Kopitonenko
родитель b26ef37282
коммит ee76ec40a8
2 изменённых файлов: 49 добавлений и 41 удалений

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

@ -457,18 +457,11 @@ func handleIfStmt(stmt *ast.IfStmt) string {
func handleImportSpec(spec ast.Spec) string { func handleImportSpec(spec ast.Spec) string {
s := spec.(*ast.ImportSpec) s := spec.(*ast.ImportSpec)
code := "" code := ""
if s.Name != nil { libName := strings.Trim(s.Path.Value, "\"")
name := handleIdent(s.Name) if s.Name == nil {
if val, ok := mapping[name]; ok { code = "import " + libName + "\n"
name = val
}
if name != "" {
if name != "controller" {
code = "#include <" + name + ".h>\n"
}
}
} else { } else {
code = "from " + strings.Trim(s.Path.Value, "\"") + " import *\n" code = "from " + libName + " import *\n"
} }
return code return code
} }

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

@ -27,7 +27,7 @@ var _ = Describe("Go Translator to Python/Skidl", func() {
It("Importing skidl", func() { It("Importing skidl", func() {
source := `package test source := `package test
import ( import (
"skidl" . "skidl"
) )
` `
expected := `from skidl import * expected := `from skidl import *
@ -40,13 +40,28 @@ main()
It("Importing skidl and FreeCAD", func() { It("Importing skidl and FreeCAD", func() {
source := `package test source := `package test
import ( import (
"skidl" . "skidl"
"FreeCAD" . "FreeCAD"
) )
` `
expected := `from skidl import * expected := `from skidl import *
from FreeCAD import * from FreeCAD import *
main()
`
Compare(source, expected)
})
It("Importing inline", func() {
source := `package test
import (
"skidl"
"FreeCAD"
)
`
expected := `import skidl
import FreeCAD
main() main()
` `
Compare(source, expected) Compare(source, expected)
@ -54,7 +69,7 @@ main()
It("Variables declaration", func() { It("Variables declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
var ( var (
a = 1 a = 1
@ -81,7 +96,7 @@ main()
It("Simple bool assignment", func() { It("Simple bool assignment", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
t := true t := true
@ -100,7 +115,7 @@ main()
It("Empty Array assignment", func() { It("Empty Array assignment", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
a := []any{} a := []any{}
@ -119,7 +134,7 @@ main()
It("Inited array assignment", func() { It("Inited array assignment", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
a := []any{1, 2, x} a := []any{1, 2, x}
@ -136,7 +151,7 @@ main()
It("Array append", func() { It("Array append", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
append(a, 1) append(a, 1)
@ -153,7 +168,7 @@ main()
It("Deep property assign", func() { It("Deep property assign", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
a.b.c = 1 a.b.c = 1
@ -170,7 +185,7 @@ main()
It("Function Declaration", func() { It("Function Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() {} func main() {}
` `
@ -185,7 +200,7 @@ main()
It("Func call", func() { It("Func call", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
generate_netlist() generate_netlist()
@ -202,7 +217,7 @@ main()
It("Func return", func() { It("Func return", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func myfunc(p1, p2 any) any { func myfunc(p1, p2 any) any {
return 5 return 5
@ -224,7 +239,7 @@ main()
It("Net Declaration", func() { It("Net Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
vin := Net("3.3v") vin := Net("3.3v")
@ -241,7 +256,7 @@ main()
It("Nets Declaration", func() { It("Nets Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
vin := Net("3.3v") vin := Net("3.3v")
@ -260,7 +275,7 @@ main()
It("Part Declaration", func() { It("Part Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32")
@ -277,7 +292,7 @@ main()
It("Parts Declaration", func() { It("Parts Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32") ESP32 := Part("RF_Module", "ESP32-WROOM-32", TEMPLATE, footprint == "RF_Module:ESP32-WROOM-32")
@ -296,7 +311,7 @@ main()
It("Values Declaration", func() { It("Values Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
esp32 := ESP32(value == "ESP32") esp32 := ESP32(value == "ESP32")
@ -315,7 +330,7 @@ main()
It("Values with params Declaration", func() { It("Values with params Declaration", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
tr_tok_verh := tr_npn(value == "tr Tok verh NPN") tr_tok_verh := tr_npn(value == "tr Tok verh NPN")
@ -332,7 +347,7 @@ main()
It("Assignments += by name ", func() { It("Assignments += by name ", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
esp32["GND"] += gnd esp32["GND"] += gnd
@ -351,7 +366,7 @@ main()
It("Assignments += by index", func() { It("Assignments += by index", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
esp32[1] += gnd esp32[1] += gnd
@ -372,7 +387,7 @@ main()
XIt("Assignments += by index of function call", func() { XIt("Assignments += by index of function call", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
vkl_iface = vkl_iface_part(1)[0] vkl_iface = vkl_iface_part(1)[0]
@ -389,7 +404,7 @@ main()
It("Assignments &", func() { It("Assignments &", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
tr_vkl["G"] & vkl tr_vkl["G"] & vkl
@ -410,7 +425,7 @@ main()
It("Value Assignment", func() { It("Value Assignment", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
vkl_iface.value = "k vkl" vkl_iface.value = "k vkl"
@ -433,7 +448,7 @@ main()
It("Condition", func() { It("Condition", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
if a > 2 { if a > 2 {
@ -461,7 +476,7 @@ main()
It("Condition complex", func() { It("Condition complex", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
if a > 2 { if a > 2 {
@ -498,7 +513,7 @@ main()
It("For with range", func() { It("For with range", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func main() { func main() {
for v := range list { for v := range list {
@ -518,7 +533,7 @@ main()
It("Function decl/call", func() { It("Function decl/call", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
func vdiv(inp, outp, param string) { func vdiv(inp, outp, param string) {
r1 := r(value == 1000) r1 := r(value == 1000)
@ -551,7 +566,7 @@ main()
It("Subcircuit function decorator", func() { It("Subcircuit function decorator", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
//@subcircuit //@subcircuit
func vdiv(inp, outp, param string) { func vdiv(inp, outp, param string) {
@ -586,7 +601,7 @@ main()
It("Package function decorator", func() { It("Package function decorator", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
//@package //@package
func vdiv(inp, outp, param string) { func vdiv(inp, outp, param string) {
@ -621,7 +636,7 @@ main()
It("Interfaces", func() { It("Interfaces", func() {
source := `package test source := `package test
import "skidl" import . "skidl"
//@subcircuit //@subcircuit
func mem_module(intfc any) { func mem_module(intfc any) {