From f6c86427b31f3cd06eb26f25b50ce20fb07a194f Mon Sep 17 00:00:00 2001 From: Flipez Date: Tue, 28 Sep 2021 20:50:14 +0200 Subject: [PATCH] use ishell for in-/output Signed-off-by: Flipez --- go.mod | 10 ++++++++++ go.sum | 23 +++++++++++++++++++++++ repl/repl.go | 39 +++++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index ff46885..960ec7d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,13 @@ module github.com/flipez/rocket-lang go 1.17 + +require ( + github.com/abiosoft/ishell/v2 v2.0.2 // indirect + github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect + github.com/fatih/color v1.12.0 // indirect + github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect + golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f680b90 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/abiosoft/ishell/v2 v2.0.2 h1:5qVfGiQISaYM8TkbBl7RFO6MddABoXpATrsFbVI+SNo= +github.com/abiosoft/ishell/v2 v2.0.2/go.mod h1:E4oTCXfo6QjoCart0QYa5m9w4S+deXs/P/9jA77A9Bs= +github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db h1:CjPUSXOiYptLbTdr1RceuZgSFDQ7U15ITERUGrUORx8= +github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc= +github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BMXYYRWTLOJKlh+lOBt6nUQgXAfB7oVIQt5cNreqSLI= +github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/repl/repl.go b/repl/repl.go index c8fc6e5..15f6112 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -1,10 +1,12 @@ package repl import ( - "bufio" + //"bufio" "fmt" "io" + "strings" + "github.com/abiosoft/ishell/v2" "github.com/flipez/rocket-lang/evaluator" "github.com/flipez/rocket-lang/lexer" "github.com/flipez/rocket-lang/object" @@ -17,34 +19,31 @@ var buildVersion = "v0.9.0" var buildDate = "2021-09-27T21:13:44Z" func Start(in io.Reader, out io.Writer) { - io.WriteString(out, fmt.Sprintf(ROCKET, buildVersion, buildDate)) + shell := ishell.New() + shell.SetHomeHistoryPath(".rocket_history") + shell.SetOut(out) - scanner := bufio.NewScanner((in)) env := object.NewEnvironment() - for { - fmt.Printf(PROMPT) - scanned := scanner.Scan() - if !scanned { - return - } + shell.Println(fmt.Sprintf(ROCKET, buildVersion, buildDate)) + shell.NotFound(func(ctx *ishell.Context) { - line := scanner.Text() - l := lexer.New(line) + l := lexer.New(strings.Join(ctx.RawArgs, " ")) p := parser.New(l) program := p.ParseProgram() if len(p.Errors()) > 0 { - printParserErrors(out, p.Errors()) - continue + printParserErrors(ctx, p.Errors()) + return } evaluated := evaluator.Eval(program, env) if evaluated != nil { - io.WriteString(out, evaluated.Inspect()) - io.WriteString(out, "\n") + ctx.Println(evaluated.Inspect()) } - } + }) + + shell.Run() } const ROCKET = ` @@ -56,10 +55,10 @@ const ROCKET = ` %10s | %-15s |___/ ` -func printParserErrors(out io.Writer, errors []string) { - io.WriteString(out, "🔥 Great, you broke it!\n") - io.WriteString(out, " parser errors:\n") +func printParserErrors(ctx *ishell.Context, errors []string) { + ctx.Println("🔥 Great, you broke it!") + ctx.Println(" parser errors:") for _, msg := range errors { - io.WriteString(out, "\t"+msg+"\n") + ctx.Printf("\t %s\n", msg) } }