grumpyhome / grumpy

Grumpy is a Python to Go source code transcompiler and runtime.
Apache License 2.0
420 stars 18 forks source link

I cannot run the binary built from the Go code generated by Grumpc #136

Closed phillvancejr closed 5 years ago

phillvancejr commented 5 years ago

I have a simple python file hello.py:

print "hello from Grumpy!"

I can run it with python, and with grumpy run. But when I transpile it into a go file using:

grumpc hello.py > hello.go  

I cannot run hello.go with go run. I get the error:

go run: cannot run non-main package

I noticed that the go file has the package main like python. It also has no main function, only an init function, so I guess this only transpiles for modules?

I build it with:

 go build -o hello hello.go

When I try to run hello I get permission denied. If I chmod +x the file and then run, I get the following error:

./hello: line 1: syntax error near unexpected token `newline'
./hello: line 1: `!<arch>'

How can I convert this python code into go code that I can compile into a binary? Thanks for any help

I'm on macOS High Sierra 10.13 My grumpy project is located in my normal gopath src directory -> /Users/phill.vance/go/src/grumpy

phillvancejr commented 5 years ago

I was able to successfully run the hello.go file and the hello exe by making changes to the hello.go file generated by grumpc.

The hello.go file generated by grumpc was:

package __main__
import (
    πg "grumpy"
)
var Code *πg.Code
func init() {
    Code = πg.NewCode("<module>", "/Users/phill.vance/go/src/grumpy/grumpy-runtime-src/build/src/test/hello.py", nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
        var πR *πg.Object; _ = πR
        var πE *πg.BaseException; _ = πE
        ß__doc__ := πg.InternStr("__doc__")
        var πTemp001 []*πg.Object
        _ = πTemp001
        for ; πF.State() >= 0; πF.PopCheckpoint() {
            switch πF.State() {
            case 0:
            default: panic("unexpected function state")
            }
            // line 2: print "hello from Grumpy!"
            πF.SetLineno(2)
            πTemp001 = make([]*πg.Object, 1)
            // line 2: print "hello from Grumpy!"
            πF.SetLineno(2)
            if πE = πF.Globals().SetItem(πF, ß__doc__.ToObject(), πg.NewStr("hello from Grumpy!").ToObject()); πE != nil {
                continue
            }
            πTemp001[0] = πg.NewStr("hello from Grumpy!").ToObject()
            if πE = πg.Print(πF, πTemp001, true); πE != nil {
                continue
            }
        }
        return nil, πE
    })
    πg.RegisterModule("__main__", Code)
}

I changed the package name to main then added a main function with the following code:

func main() {
    πg.RunMain(Code)
}

hello.go is now:

package main

import (
    πg "grumpy"
)

var Code *πg.Code

func init() {
    Code = πg.NewCode("<module>", "/Users/phill.vance/go/src/grumpy/grumpy-runtime-src/build/src/test/hello.py", nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
        var πR *πg.Object
        _ = πR
        var πE *πg.BaseException
        _ = πE
        ß__doc__ := πg.InternStr("__doc__")
        var πTemp001 []*πg.Object
        _ = πTemp001
        for ; πF.State() >= 0; πF.PopCheckpoint() {
            switch πF.State() {
            case 0:
            default:
                panic("unexpected function state")
            }
            // line 2: print "hello from Grumpy!"
            πF.SetLineno(2)
            πTemp001 = make([]*πg.Object, 1)
            // line 2: print "hello from Grumpy!"
            πF.SetLineno(2)
            if πE = πF.Globals().SetItem(πF, ß__doc__.ToObject(), πg.NewStr("hello from Grumpy!").ToObject()); πE != nil {
                continue
            }
            πTemp001[0] = πg.NewStr("hello from Grumpy!").ToObject()
            if πE = πg.Print(πF, πTemp001, true); πE != nil {
                continue
            }
        }
        return nil, πE
    })
    πg.RegisterModule("__main__", Code)
}
func main() {
    πg.RunMain(Code)
}

This code now runs both via go run and after building a binary with go build. It seems that this should be the output of grumpc. Is there a flag or option that I am missing that enables code for use as a main file as opposed to what I assume is a module in the default grumpc generation?