From 3fe7d473881ed00ad7e75ac8cbd9ed667c7f3f50 Mon Sep 17 00:00:00 2001 From: Softonik Date: Sat, 10 Dec 2022 05:13:43 +0300 Subject: [PATCH] =?UTF-8?q?Python:=20=D0=B2=D1=8B=D0=B7=D0=BE=D0=B2=20main?= =?UTF-8?q?()=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F=D0=B5=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B5?= =?UTF-8?q?=D1=81=D0=BB=D0=B8=20=D1=84-=D1=86=D0=B8=D1=8F=20=D0=BE=D0=B1?= =?UTF-8?q?=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transpile_python/service.go | 10 +++++++--- transpile_python/service_test.go | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/transpile_python/service.go b/transpile_python/service.go index 67ab295..b2e96c0 100644 --- a/transpile_python/service.go +++ b/transpile_python/service.go @@ -30,6 +30,7 @@ type defaultService struct { } var ind int +var main_func_declared bool // NewService creates a a new transpile and returns its address. func NewService(in io.Reader, out io.Writer) Service { @@ -48,6 +49,7 @@ func (s *defaultService) Start() error { return fmt.Errorf("Error: %s", ErrorWorkerWriterIsNil) } ind = 0 + main_func_declared = false // Read tokens from file by using Go's parser. fset := token.NewFileSet() file, err := parser.ParseFile(fset, "source.go", s.in, parser.ParseComments) @@ -88,7 +90,9 @@ func (s *defaultService) Start() error { } } - s.out.Write([]byte("main()\n")) + if main_func_declared { + s.out.Write([]byte("main()\n")) + } // Print the AST. // ast.Fprint(os.Stderr, fset, file, nil) @@ -293,8 +297,8 @@ func handleFuncDecl(decl ast.Decl) string { code += "def " name := "" name = handleFuncDeclName(fd.Name) - if name == "NewController" { - return "" + if name == "main" { + main_func_declared = true } code += name code += "(" diff --git a/transpile_python/service_test.go b/transpile_python/service_test.go index cc786f6..7b0fe96 100644 --- a/transpile_python/service_test.go +++ b/transpile_python/service_test.go @@ -32,7 +32,6 @@ var _ = Describe("Go Translator to Python/Skidl", func() { ` expected := `from skidl import * -main() ` Compare(source, expected) }) @@ -47,7 +46,6 @@ main() expected := `from skidl import * from FreeCAD import * -main() ` Compare(source, expected) }) @@ -62,7 +60,6 @@ main() expected := `import skidl import FreeCAD -main() ` Compare(source, expected) }) @@ -75,7 +72,6 @@ main() ` expected := `import cadquery as cq -main() ` Compare(source, expected) }) @@ -102,7 +98,6 @@ d = a + b + c / 2 t = True f = False -main() ` Compare(source, expected) }) @@ -197,6 +192,20 @@ main() }) 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 import . "skidl" @@ -228,7 +237,7 @@ main() Compare(source, expected) }) - FIt("Sequence func call", func() { + It("Sequence func call", func() { source := `package test import . "skidl"