Skip to content

Commit

Permalink
use ishell for in-/output
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Sep 28, 2021
1 parent 9d315be commit f6c8642
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
39 changes: 19 additions & 20 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 = `
Expand All @@ -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)
}
}

0 comments on commit f6c8642

Please sign in to comment.