-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.go
More file actions
150 lines (137 loc) · 3.95 KB
/
Copy pathrepl.go
File metadata and controls
150 lines (137 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/siyul-park/minipy/ast"
"github.com/siyul-park/minipy/compiler"
"github.com/siyul-park/minipy/parser"
"github.com/siyul-park/minivm/interp"
"github.com/siyul-park/minivm/optimize"
)
// runFile compiles and runs a minipy source file, writing program output to out.
func runFile(path string, out io.Writer, level optimize.Level, paths []string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
abs, err := filepath.Abs(path)
if err != nil {
return err
}
opts := []compiler.Option{
compiler.WithOutput(out),
compiler.WithOptimizationLevel(level),
compiler.WithModules(os.DirFS(filepath.Dir(abs))),
}
opts = append(opts, modulePathOptions(paths)...)
prog, err := compiler.Compile(f, opts...)
if err != nil {
return err
}
vm := interp.New(prog)
defer vm.Close()
return vm.Run(context.Background())
}
// repl runs the interactive loop. It persists declarations and assignments as
// session state, and runs bare expressions and print(...) lines transiently —
// re-running the accumulated state each time so prior side effects do not
// repeat. A bare printable expression is auto-echoed via str()+print.
func repl(in io.Reader, out io.Writer, level optimize.Level, paths []string) error {
fmt.Fprintln(out, "minipy REPL — type Ctrl-D to exit")
scanner := bufio.NewScanner(in)
var state strings.Builder
for {
fmt.Fprint(out, ">>> ")
if !scanner.Scan() {
fmt.Fprintln(out)
return scanner.Err()
}
line := scanner.Text()
if strings.TrimSpace(line) == "" {
continue
}
evalLine(&state, line, out, level, paths)
}
}
// evalLine classifies one REPL entry and either extends the session state or
// runs a transient program.
func evalLine(state *strings.Builder, line string, out io.Writer, level optimize.Level, paths []string) {
mod, err := parser.Parse(strings.NewReader(line))
if err != nil {
fmt.Fprintln(out, pyError(err))
return
}
if len(mod.Body) == 0 {
return
}
switch stmt := mod.Body[0].(type) {
case *ast.AnnAssign, *ast.Assign, *ast.AugAssign, *ast.Import, *ast.ImportFrom:
candidate := state.String() + line + "\n"
opts := replOptions(io.Discard, level, paths)
if _, err := compiler.Compile(strings.NewReader(candidate), opts...); err != nil {
fmt.Fprintln(out, pyError(err))
return
}
state.WriteString(line + "\n")
case *ast.ExprStmt:
runTransient(state.String(), line, stmt.X, out, level, paths)
}
}
// runTransient compiles and runs `state + line`, auto-wrapping a bare expression
// in str()+print so its value is echoed.
func runTransient(state, line string, x ast.Expr, out io.Writer, level optimize.Level, paths []string) {
src := state
if isPrintCall(x) {
src += line + "\n"
} else {
src += "print(str(" + strings.TrimSpace(line) + "))\n"
}
opts := replOptions(out, level, paths)
prog, err := compiler.Compile(strings.NewReader(src), opts...)
if err != nil {
fmt.Fprintln(out, pyError(err))
return
}
vm := interp.New(prog)
defer vm.Close()
if err := vm.Run(context.Background()); err != nil {
fmt.Fprintln(out, pyError(err))
}
}
func replOptions(out io.Writer, level optimize.Level, paths []string) []compiler.Option {
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
opts := []compiler.Option{
compiler.WithOutput(out),
compiler.WithOptimizationLevel(level),
compiler.WithModules(os.DirFS(cwd)),
}
opts = append(opts, modulePathOptions(paths)...)
return opts
}
func isPrintCall(x ast.Expr) bool {
call, ok := x.(*ast.CallExpr)
if !ok {
return false
}
name, ok := call.Fn.(*ast.Name)
return ok && name.Name == "print"
}
// pyError renders an error in CPython's style. Compile diagnostics already carry
// a Python exception name; common runtime traps are mapped to their Python
// equivalents.
func pyError(err error) string {
if errors.Is(err, interp.ErrDivideByZero) {
return "ZeroDivisionError: division by zero"
}
return err.Error()
}