integrated readline

This commit is contained in:
Aidan 2020-06-27 21:27:33 -07:00
parent eb85a10415
commit 3c2dde3665
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 31 additions and 29 deletions

View file

@ -20,9 +20,8 @@ package main
import ( import (
"os" "os"
"fmt" "fmt"
"bufio"
"strings"
"strconv" "strconv"
"github.com/chzyer/readline"
"git.callpipe.com/aidan/shs/ast" "git.callpipe.com/aidan/shs/ast"
"git.callpipe.com/aidan/shs/log" "git.callpipe.com/aidan/shs/log"
"git.callpipe.com/aidan/shs/stdlib" "git.callpipe.com/aidan/shs/stdlib"
@ -59,44 +58,31 @@ func main() {
ast.InitVarTable(vars) ast.InitVarTable(vars)
ast.SyncTablesWithOSEnviron = true ast.SyncTablesWithOSEnviron = true
useHist := false
var histFile *os.File
var err error var err error
if hist != "" {
useHist = true
histFile, err = os.OpenFile(hist, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
useHist = false
log.Log(log.ERR, "coudlnt open histfile: " + err.Error(), "init")
}
}
if prompt == "" { if prompt == "" {
prompt = def_prompt prompt = def_prompt
} }
// TODO: is bufio right for this? rl, err := readline.NewEx(&readline.Config{
reader := bufio.NewReader(os.Stdin) Prompt: prompt,
HistoryFile: hist,
InterruptPrompt: "^C",
})
defer rl.Close()
if err != nil {
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
return
}
for { for {
setLogLvl() setLogLvl()
fmt.Print(prompt + " ") text, err := rl.Readline()
text, err := reader.ReadString('\n')
if err != nil { if err != nil {
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl") log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
} }
// TODO: replace with a regex
text = strings.Replace(text, "\r\n", "", -1)
text = strings.Replace(text, "\n", "", -1)
if useHist {
_, err = histFile.Write([]byte(text + "\n"))
if err != nil {
log.Log(log.DEBUG, "couldnt write to histfile: " + err.Error(), "repl")
}
}
userInput := ast.Lex(text) userInput := ast.Lex(text)
if userInput == nil { if userInput == nil {
// errors handled in Lex // errors handled in Lex

2
go.mod
View file

@ -1,3 +1,5 @@
module git.callpipe.com/aidan/shs module git.callpipe.com/aidan/shs
go 1.14 go 1.14
require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=

View file

@ -114,7 +114,19 @@ func GenFuncTable() ast.FuncTable {
TimesCalled: 0, TimesCalled: 0,
Args: 1, Args: 1,
}, },
"exit": &ast.Function{
Function: exit,
Name: "exit",
TimesCalled 0,
Args: 0,
},
} }
return stdlib return stdlib
} }
func exit_shell(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
os.Exit(0)
return nil // I hope execution doesnt get here
}