fix releaser tools
This commit is contained in:
37
repl/repl.go
37
repl/repl.go
@@ -7,7 +7,6 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"monkey/compiler"
|
||||
"monkey/evaluator"
|
||||
@@ -68,6 +67,10 @@ type REPL struct {
|
||||
}
|
||||
|
||||
func New(user string, args []string, opts *Options) *REPL {
|
||||
object.StandardInput = os.Stdin
|
||||
object.StandardOutput = os.Stdout
|
||||
object.ExitFunction = os.Exit
|
||||
|
||||
return &REPL{user, args, opts}
|
||||
}
|
||||
|
||||
@@ -76,7 +79,7 @@ func New(user string, args []string, opts *Options) *REPL {
|
||||
func (r *REPL) Eval(f io.Reader) (env *object.Environment) {
|
||||
env = object.NewEnvironment()
|
||||
|
||||
b, err := ioutil.ReadAll(f)
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error reading source file: %s", err)
|
||||
return
|
||||
@@ -98,7 +101,7 @@ func (r *REPL) Eval(f io.Reader) (env *object.Environment) {
|
||||
// Exec parses, compiles and executes the program given by f and returns
|
||||
// the resulting virtual machine, any errors are printed to stderr
|
||||
func (r *REPL) Exec(f io.Reader) (state *VMState) {
|
||||
b, err := ioutil.ReadAll(f)
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error reading source file: %s", err)
|
||||
return
|
||||
@@ -225,12 +228,30 @@ func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) {
|
||||
}
|
||||
|
||||
func (r *REPL) Run() {
|
||||
if len(r.args) == 1 {
|
||||
object.Arguments = make([]string, len(r.args))
|
||||
copy(object.Arguments, r.args)
|
||||
|
||||
if len(r.args) == 0 {
|
||||
fmt.Printf("Hello %s! This is the Monkey programming language!\n", r.user)
|
||||
fmt.Printf("Feel free to type in commands\n")
|
||||
if r.opts.Engine == "eval" {
|
||||
r.StartEvalLoop(os.Stdin, os.Stdout, nil)
|
||||
} else {
|
||||
r.StartExecLoop(os.Stdin, os.Stdout, nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(r.args) > 0 {
|
||||
f, err := os.Open(r.args[0])
|
||||
if err != nil {
|
||||
log.Fatalf("could not open source file %s: %s", r.args[0], err)
|
||||
}
|
||||
|
||||
// Remove program argument (zero)
|
||||
r.args = r.args[1:]
|
||||
object.Arguments = object.Arguments[1:]
|
||||
|
||||
if r.opts.Engine == "eval" {
|
||||
env := r.Eval(f)
|
||||
if r.opts.Interactive {
|
||||
@@ -242,14 +263,6 @@ func (r *REPL) Run() {
|
||||
r.StartExecLoop(os.Stdin, os.Stdout, state)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Hello %s! This is the Monkey programming language!\n", r.user)
|
||||
fmt.Printf("Feel free to type in commands\n")
|
||||
if r.opts.Engine == "eval" {
|
||||
r.StartEvalLoop(os.Stdin, os.Stdout, nil)
|
||||
} else {
|
||||
r.StartExecLoop(os.Stdin, os.Stdout, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user