@@ -56,7 +56,7 @@ There are three basic categories of incomplete functionality:
5656
5757## Running Grumpy Programs
5858
59- ### Method 1: grumprun :
59+ ### Method 1: make run :
6060
6161The simplest way to execute a Grumpy program is to use ` make run ` , which wraps a
6262shell script called grumprun that takes Python code on stdin and builds and runs
@@ -67,33 +67,62 @@ root directory of the Grumpy source code distribution:
6767echo "print 'hello, world'" | make run
6868```
6969
70- ### Method 2: grumpc:
70+ ### Method 2: grumpc and grumprun :
7171
7272For more complicated programs, you'll want to compile your Python source code to
7373Go using grumpc (the Grumpy compiler) and then build the Go code using `go
74- build`. First, write a simple .py script:
74+ build`. Since Grumpy programs are statically linked, all the modules in a
75+ program must be findable by the Grumpy toolchain on the GOPATH. Grumpy looks for
76+ Go packages corresponding to Python modules in the \_\_ python\_\_ subdirectory
77+ of the GOPATH. By convention, this subdirectory is also used for staging Python
78+ source code, making it similar to the PYTHONPATH.
7579
76- ```
77- echo 'print "hello, world"' > hello.py
78- ```
79-
80- Next, build the toolchain and export some environment variables that make the
81- toolchain work:
80+ The first step is to set up the shell so that the Grumpy toolchain and libraries
81+ can be found. From the root directory of the Grumpy source distribution run:
8282
8383```
8484make
85+ export PATH=$PWD/build/bin:$PATH
8586export GOPATH=$PWD/build
8687export PYTHONPATH=$PWD/build/lib/python2.7/site-packages
8788```
8889
89- Finally, compile the Python script and build a binary from it:
90+ You will know things are working if you see the expected output from this
91+ command:
92+
93+ ```
94+ echo 'import sys; print sys.version' | grumprun
95+ ```
96+
97+ Next, we will write our simple Python module into the \_\_ python\_\_ directory:
98+
99+ ```
100+ echo 'def hello(): print "hello, world"' > $GOPATH/src/__python__/hello.py
101+ ```
102+
103+ To build a Go package from our Python script, run the following:
104+
105+ ```
106+ mkdir -p $GOPATH/src/__python__/hello
107+ grumpc -modname=hello $GOPATH/src/__python__/hello.py > \
108+ $GOPATH/src/__python__/hello/module.go
109+ ```
110+
111+ You should now be able to build a Go program that imports the package
112+ "\_\_ python\_\_ /hello". We can also import this module into Python programs
113+ that are built using grumprun:
90114
91115```
92- build/bin/grumpc hello.py > hello.go
93- go build -o hello hello.go
116+ echo 'from hello import hello; hello()' | grumprun
94117```
95118
96- Now execute the ` ./hello ` binary to your heart's content.
119+ grumprun is doing a few things under the hood here:
120+
121+ 1 . Compiles the given Python code to a dummy Go package, the same way we
122+ produced \_\_ python\_\_ /hello/module.go above
123+ 2 . Produces a main Go package that imports the Go package from step 1. and
124+ executes it as our \_\_ main\_\_ Python package
125+ 3 . Executes ` go run ` on the main package generated in step 2.
97126
98127## Developing Grumpy
99128
0 commit comments