Skip to content

Commit ecdd85c

Browse files
committed
Reduce code duplication
1 parent e7dcdff commit ecdd85c

File tree

3 files changed

+51
-70
lines changed

3 files changed

+51
-70
lines changed

main.go

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func xmain(args []string) {
6060
defer pprof.StopCPUProfile()
6161
}
6262

63+
var err error
6364
// IF no args, enter REPL mode
6465
if len(args) == 0 {
6566

@@ -69,43 +70,46 @@ func xmain(args []string) {
6970
fmt.Printf("- go version: %s\n", runtime.Version())
7071

7172
replCtx := repl.New(ctx)
72-
cli.RunREPL(replCtx)
73+
err = cli.RunREPL(replCtx)
74+
} else {
75+
_, err = py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
76+
}
77+
if err != nil {
78+
if py.IsException(py.SystemExit, err) {
79+
handleSystemExit(err.(py.ExceptionInfo).Value.(*py.Exception))
80+
}
81+
py.TracebackDump(err)
82+
os.Exit(1)
83+
}
84+
}
7385

86+
func handleSystemExit(exc *py.Exception) {
87+
args := exc.Args.(py.Tuple)
88+
if len(args) == 0 {
89+
os.Exit(0)
90+
} else if len(args) == 1 {
91+
if code, ok := args[0].(py.Int); ok {
92+
c, err := code.GoInt()
93+
if err != nil {
94+
fmt.Fprintln(os.Stderr, err)
95+
os.Exit(1)
96+
}
97+
os.Exit(c)
98+
}
99+
msg, err := py.ReprAsString(args[0])
100+
if err != nil {
101+
fmt.Fprintln(os.Stderr, err)
102+
} else {
103+
fmt.Fprintln(os.Stderr, msg)
104+
}
105+
os.Exit(1)
74106
} else {
75-
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
107+
msg, err := py.ReprAsString(args)
76108
if err != nil {
77-
if py.IsException(py.SystemExit, err) {
78-
args := err.(py.ExceptionInfo).Value.(*py.Exception).Args.(py.Tuple)
79-
if len(args) == 0 {
80-
os.Exit(0)
81-
} else if len(args) == 1 {
82-
if code, ok := args[0].(py.Int); ok {
83-
c, err := code.GoInt()
84-
if err != nil {
85-
fmt.Fprintln(os.Stderr, err)
86-
os.Exit(1)
87-
}
88-
os.Exit(c)
89-
}
90-
msg, err := py.ReprAsString(args[0])
91-
if err != nil {
92-
fmt.Fprintln(os.Stderr, err)
93-
} else {
94-
fmt.Fprintln(os.Stderr, msg)
95-
}
96-
os.Exit(1)
97-
} else {
98-
msg, err := py.ReprAsString(args)
99-
if err != nil {
100-
fmt.Fprintln(os.Stderr, err)
101-
} else {
102-
fmt.Fprintln(os.Stderr, msg)
103-
}
104-
os.Exit(1)
105-
}
106-
}
107-
py.TracebackDump(err)
108-
os.Exit(1)
109+
fmt.Fprintln(os.Stderr, err)
110+
} else {
111+
fmt.Fprintln(os.Stderr, msg)
109112
}
113+
os.Exit(1)
110114
}
111115
}

repl/cli/cli.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (rl *readline) Print(out string) {
117117
}
118118

119119
// RunREPL starts the REPL loop
120-
func RunREPL(replCtx *repl.REPL) {
120+
func RunREPL(replCtx *repl.REPL) error {
121121
if replCtx == nil {
122122
replCtx = repl.New(nil)
123123
}
@@ -144,6 +144,10 @@ func RunREPL(replCtx *repl.REPL) {
144144
if line != "" {
145145
rl.AppendHistory(line)
146146
}
147-
rl.repl.Run(line)
147+
err = rl.repl.Run(line)
148+
if err != nil {
149+
return err
150+
}
148151
}
152+
return nil
149153
}

repl/repl.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package repl
77

88
import (
99
"fmt"
10-
"os"
1110
"sort"
1211
"strings"
1312

@@ -67,7 +66,7 @@ func (r *REPL) SetUI(term UI) {
6766
}
6867

6968
// Run runs a single line of the REPL
70-
func (r *REPL) Run(line string) {
69+
func (r *REPL) Run(line string) error {
7170
// Override the PrintExpr output temporarily
7271
oldPrintExpr := vm.PrintExpr
7372
vm.PrintExpr = r.term.Print
@@ -77,13 +76,13 @@ func (r *REPL) Run(line string) {
7776
if r.continuation {
7877
if line != "" {
7978
r.previous += string(line) + "\n"
80-
return
79+
return nil
8180
}
8281
}
8382
// need +"\n" because "single" expects \n terminated input
8483
toCompile := r.previous + string(line)
8584
if toCompile == "" {
86-
return
85+
return nil
8786
}
8887
code, err := py.Compile(toCompile+"\n", r.prog, py.SingleMode, 0, true)
8988
if err != nil {
@@ -98,50 +97,24 @@ func (r *REPL) Run(line string) {
9897
r.previous += string(line) + "\n"
9998
r.term.SetPrompt(ContinuationPrompt)
10099
}
101-
return
100+
return nil
102101
}
103102
}
104103
r.continuation = false
105104
r.term.SetPrompt(NormalPrompt)
106105
r.previous = ""
107106
if err != nil {
108107
r.term.Print(fmt.Sprintf("Compile error: %v", err))
109-
return
108+
return nil
110109
}
111110
_, err = r.Context.RunCode(code, r.Module.Globals, r.Module.Globals, nil)
112111
if err != nil {
113112
if py.IsException(py.SystemExit, err) {
114-
args := err.(py.ExceptionInfo).Value.(*py.Exception).Args.(py.Tuple)
115-
if len(args) == 0 {
116-
os.Exit(0)
117-
} else if len(args) == 1 {
118-
if code, ok := args[0].(py.Int); ok {
119-
c, err := code.GoInt()
120-
if err != nil {
121-
fmt.Fprintln(os.Stderr, err)
122-
os.Exit(1)
123-
}
124-
os.Exit(c)
125-
}
126-
msg, err := py.ReprAsString(args[0])
127-
if err != nil {
128-
fmt.Fprintln(os.Stderr, err)
129-
} else {
130-
fmt.Fprintln(os.Stderr, msg)
131-
}
132-
os.Exit(1)
133-
} else {
134-
msg, err := py.ReprAsString(args)
135-
if err != nil {
136-
fmt.Fprintln(os.Stderr, err)
137-
} else {
138-
fmt.Fprintln(os.Stderr, msg)
139-
}
140-
os.Exit(1)
141-
}
113+
return err
142114
}
143115
py.TracebackDump(err)
144116
}
117+
return nil
145118
}
146119

147120
// WordCompleter takes the currently edited line with the cursor

0 commit comments

Comments
 (0)