Python: вызов main() добавляется только если ф-ция объявлена

Этот коммит содержится в:
Softonik 2022-12-10 05:13:43 +03:00 коммит произвёл Nikolay Kopitonenko
родитель 37c9ded2b0
коммит 3fe7d47388
2 изменённых файлов: 22 добавлений и 9 удалений

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

@ -30,6 +30,7 @@ type defaultService struct {
} }
var ind int var ind int
var main_func_declared bool
// NewService creates a a new transpile and returns its address. // NewService creates a a new transpile and returns its address.
func NewService(in io.Reader, out io.Writer) Service { func NewService(in io.Reader, out io.Writer) Service {
@ -48,6 +49,7 @@ func (s *defaultService) Start() error {
return fmt.Errorf("Error: %s", ErrorWorkerWriterIsNil) return fmt.Errorf("Error: %s", ErrorWorkerWriterIsNil)
} }
ind = 0 ind = 0
main_func_declared = false
// Read tokens from file by using Go's parser. // Read tokens from file by using Go's parser.
fset := token.NewFileSet() fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "source.go", s.in, parser.ParseComments) file, err := parser.ParseFile(fset, "source.go", s.in, parser.ParseComments)
@ -88,7 +90,9 @@ func (s *defaultService) Start() error {
} }
} }
if main_func_declared {
s.out.Write([]byte("main()\n")) s.out.Write([]byte("main()\n"))
}
// Print the AST. // Print the AST.
// ast.Fprint(os.Stderr, fset, file, nil) // ast.Fprint(os.Stderr, fset, file, nil)
@ -293,8 +297,8 @@ func handleFuncDecl(decl ast.Decl) string {
code += "def " code += "def "
name := "" name := ""
name = handleFuncDeclName(fd.Name) name = handleFuncDeclName(fd.Name)
if name == "NewController" { if name == "main" {
return "" main_func_declared = true
} }
code += name code += name
code += "(" code += "("

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

@ -32,7 +32,6 @@ var _ = Describe("Go Translator to Python/Skidl", func() {
` `
expected := `from skidl import * expected := `from skidl import *
main()
` `
Compare(source, expected) Compare(source, expected)
}) })
@ -47,7 +46,6 @@ main()
expected := `from skidl import * expected := `from skidl import *
from FreeCAD import * from FreeCAD import *
main()
` `
Compare(source, expected) Compare(source, expected)
}) })
@ -62,7 +60,6 @@ main()
expected := `import skidl expected := `import skidl
import FreeCAD import FreeCAD
main()
` `
Compare(source, expected) Compare(source, expected)
}) })
@ -75,7 +72,6 @@ main()
` `
expected := `import cadquery as cq expected := `import cadquery as cq
main()
` `
Compare(source, expected) Compare(source, expected)
}) })
@ -102,7 +98,6 @@ d = a + b + c / 2
t = True t = True
f = False f = False
main()
` `
Compare(source, expected) Compare(source, expected)
}) })
@ -197,6 +192,20 @@ main()
}) })
It("Function Declaration", func() { It("Function Declaration", func() {
source := `package test
import . "skidl"
func myfunc() {}
`
expected := `from skidl import *
def myfunc():
None
`
Compare(source, expected)
})
It("Function Declaration: main adds main call", func() {
source := `package test source := `package test
import . "skidl" import . "skidl"
@ -228,7 +237,7 @@ main()
Compare(source, expected) Compare(source, expected)
}) })
FIt("Sequence func call", func() { It("Sequence func call", func() {
source := `package test source := `package test
import . "skidl" import . "skidl"