Skip to content

Commit f0cbe48

Browse files
committed
compile: re-organise code
1 parent cc59dde commit f0cbe48

File tree

2 files changed

+101
-98
lines changed

2 files changed

+101
-98
lines changed

compile/compile.go

+36-98
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,56 @@
11
// compile python code
2-
//
3-
// Need to port the 10,000 lines of compiling machinery, into a
4-
// different module probably.
5-
//
6-
// In the mean time, cheat horrendously by calling python3.4 to do our
7-
// dirty work under the hood!
82

93
package compile
104

115
import (
12-
"bytes"
136
"fmt"
14-
"os"
15-
"os/exec"
16-
"strings"
177

188
"github.com/ncw/gpython/ast"
19-
"github.com/ncw/gpython/marshal"
209
"github.com/ncw/gpython/parser"
2110
"github.com/ncw/gpython/py"
2211
"github.com/ncw/gpython/symtable"
2312
"github.com/ncw/gpython/vm"
2413
)
2514

26-
// Set in py to avoid circular import
27-
func init() {
28-
py.Compile = Compile
15+
// Loop
16+
type loop struct {
17+
Start *Label
18+
End *Label
19+
IsForLoop bool
2920
}
3021

31-
// Compile(source, filename, mode, flags, dont_inherit) -> code object
32-
//
33-
// Compile the source string (a Python module, statement or expression)
34-
// into a code object that can be executed by exec() or eval().
35-
// The filename will be used for run-time error messages.
36-
// The mode must be 'exec' to compile a module, 'single' to compile a
37-
// single (interactive) statement, or 'eval' to compile an expression.
38-
// The flags argument, if present, controls which future statements influence
39-
// the compilation of the code.
40-
// The dont_inherit argument, if non-zero, stops the compilation inheriting
41-
// the effects of any future statements in effect in the code calling
42-
// compile; if absent or zero these statements do influence the compilation,
43-
// in addition to any features explicitly specified.
44-
func LegacyCompile(str, filename, mode string, flags int, dont_inherit bool) py.Object {
45-
dont_inherit_str := "False"
46-
if dont_inherit {
47-
dont_inherit_str = "True"
48-
}
49-
// FIXME escaping in filename
50-
code := fmt.Sprintf(`import sys, marshal
51-
str = sys.stdin.buffer.read().decode("utf-8")
52-
code = compile(str, "%s", "%s", %d, %s)
53-
marshalled_code = marshal.dumps(code)
54-
sys.stdout.buffer.write(marshalled_code)
55-
sys.stdout.close()`,
56-
filename,
57-
mode,
58-
flags,
59-
dont_inherit_str,
60-
)
61-
cmd := exec.Command("python3.4", "-c", code)
62-
cmd.Stdin = strings.NewReader(str)
63-
var out bytes.Buffer
64-
cmd.Stdout = &out
65-
var stderr bytes.Buffer
66-
cmd.Stderr = &stderr
67-
err := cmd.Run()
68-
if err != nil {
69-
fmt.Fprintf(os.Stderr, "--- Failed to run python3.4 compile ---\n")
70-
fmt.Fprintf(os.Stderr, "--------------------\n")
71-
os.Stderr.Write(stderr.Bytes())
72-
fmt.Fprintf(os.Stderr, "--------------------\n")
73-
panic(err)
74-
}
75-
obj, err := marshal.ReadObject(bytes.NewBuffer(out.Bytes()))
76-
if err != nil {
77-
panic(err)
22+
// Loopstack
23+
type loopstack []loop
24+
25+
// Push a loop
26+
func (ls *loopstack) Push(l loop) {
27+
*ls = append(*ls, l)
28+
}
29+
30+
// Pop a loop
31+
func (ls *loopstack) Pop() {
32+
*ls = (*ls)[:len(*ls)-1]
33+
}
34+
35+
// Return current loop or nil for none
36+
func (ls loopstack) Top() *loop {
37+
if len(ls) == 0 {
38+
return nil
7839
}
79-
return obj
40+
return &ls[len(ls)-1]
41+
}
42+
43+
// State for the compiler
44+
type compiler struct {
45+
Code *py.Code // code being built up
46+
OpCodes Instructions
47+
loops loopstack
48+
SymTable *symtable.SymTable
49+
}
50+
51+
// Set in py to avoid circular import
52+
func init() {
53+
py.Compile = Compile
8054
}
8155

8256
// Compile(source, filename, mode, flags, dont_inherit) -> code object
@@ -159,42 +133,6 @@ func CompileAst(Ast ast.Ast, filename string, flags int, dont_inherit bool, SymT
159133
return code, nil
160134
}
161135

162-
// Loop
163-
type loop struct {
164-
Start *Label
165-
End *Label
166-
IsForLoop bool
167-
}
168-
169-
// Loopstack
170-
type loopstack []loop
171-
172-
// Push a loop
173-
func (ls *loopstack) Push(l loop) {
174-
*ls = append(*ls, l)
175-
}
176-
177-
// Pop a loop
178-
func (ls *loopstack) Pop() {
179-
*ls = (*ls)[:len(*ls)-1]
180-
}
181-
182-
// Return current loop or nil for none
183-
func (ls loopstack) Top() *loop {
184-
if len(ls) == 0 {
185-
return nil
186-
}
187-
return &ls[len(ls)-1]
188-
}
189-
190-
// State for the compiler
191-
type compiler struct {
192-
Code *py.Code // code being built up
193-
OpCodes Instructions
194-
loops loopstack
195-
SymTable *symtable.SymTable
196-
}
197-
198136
// Check for docstring as first Expr in body and remove it and set the
199137
// first constant if found.
200138
func (c *compiler) docString(body []ast.Stmt) []ast.Stmt {

compile/legacy.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package compile
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"strings"
9+
10+
"github.com/ncw/gpython/marshal"
11+
"github.com/ncw/gpython/py"
12+
)
13+
14+
// Compile with python3.4 - not used any more but keep for the moment!
15+
16+
// Compile(source, filename, mode, flags, dont_inherit) -> code object
17+
//
18+
// Compile the source string (a Python module, statement or expression)
19+
// into a code object that can be executed by exec() or eval().
20+
// The filename will be used for run-time error messages.
21+
// The mode must be 'exec' to compile a module, 'single' to compile a
22+
// single (interactive) statement, or 'eval' to compile an expression.
23+
// The flags argument, if present, controls which future statements influence
24+
// the compilation of the code.
25+
// The dont_inherit argument, if non-zero, stops the compilation inheriting
26+
// the effects of any future statements in effect in the code calling
27+
// compile; if absent or zero these statements do influence the compilation,
28+
// in addition to any features explicitly specified.
29+
func LegacyCompile(str, filename, mode string, flags int, dont_inherit bool) py.Object {
30+
dont_inherit_str := "False"
31+
if dont_inherit {
32+
dont_inherit_str = "True"
33+
}
34+
// FIXME escaping in filename
35+
code := fmt.Sprintf(`import sys, marshal
36+
str = sys.stdin.buffer.read().decode("utf-8")
37+
code = compile(str, "%s", "%s", %d, %s)
38+
marshalled_code = marshal.dumps(code)
39+
sys.stdout.buffer.write(marshalled_code)
40+
sys.stdout.close()`,
41+
filename,
42+
mode,
43+
flags,
44+
dont_inherit_str,
45+
)
46+
cmd := exec.Command("python3.4", "-c", code)
47+
cmd.Stdin = strings.NewReader(str)
48+
var out bytes.Buffer
49+
cmd.Stdout = &out
50+
var stderr bytes.Buffer
51+
cmd.Stderr = &stderr
52+
err := cmd.Run()
53+
if err != nil {
54+
fmt.Fprintf(os.Stderr, "--- Failed to run python3.4 compile ---\n")
55+
fmt.Fprintf(os.Stderr, "--------------------\n")
56+
os.Stderr.Write(stderr.Bytes())
57+
fmt.Fprintf(os.Stderr, "--------------------\n")
58+
panic(err)
59+
}
60+
obj, err := marshal.ReadObject(bytes.NewBuffer(out.Bytes()))
61+
if err != nil {
62+
panic(err)
63+
}
64+
return obj
65+
}

0 commit comments

Comments
 (0)