comments support, script loading support

This commit is contained in:
Aidan 2020-07-15 18:41:54 -07:00
parent 654e8bd55b
commit bd22b84699
No known key found for this signature in database
GPG key ID: 327711E983899316
11 changed files with 145 additions and 69 deletions

View file

@ -18,9 +18,10 @@
package main
import (
"os"
"fmt"
"strconv"
"github.com/chzyer/readline"
"github.com/peterh/liner"
"gitlab.com/whom/shs/ast"
"gitlab.com/whom/shs/log"
"gitlab.com/whom/shs/config"
@ -54,6 +55,7 @@ func main() {
var prompt string
var debug string
var hist string
no_hist := false
ast.SyncTablesWithOSEnviron = true
ast.ExecWhenFuncUndef = true
@ -67,34 +69,61 @@ func main() {
hist_t := ast.GetVar("SH_HIST_FILE", vars)
if hist_t != nil {
hist = hist_t.Value()
} else {
no_hist = true
}
prompt_t := ast.GetVar("SHS_SH_PROMPT", vars)
dyn_prompt := ast.GetFunction("_SH_PROMPT", funcs)
if dyn_prompt == nil || dyn_prompt.Args != 0 {
dyn_prompt = nil
}
prompt_t := ast.GetVar("SHS_STATIC_PROMPT", vars)
if prompt_t != nil {
prompt = prompt_t.Value()
} else {
prompt = def_prompt
}
rl, err := readline.NewEx(&readline.Config{
Prompt: prompt,
HistoryFile: hist,
InterruptPrompt: "^C",
})
line := liner.NewLiner()
defer line.Close()
defer rl.Close()
if err != nil {
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
return
line.SetCtrlCAborts(true)
var histFile *os.File
var err error
if !no_hist {
histFile, err = os.Open(hist)
if err == nil {
line.ReadHistory(histFile)
} else {
log.Log(log.ERR,
"couldnt read history: " + err.Error(),
"repl")
}
defer histFile.Close()
}
for {
setLogLvl(vars)
text, err := rl.Readline()
if err != nil {
var prePrompt string
if dyn_prompt != nil {
p_tok := dyn_prompt.CallFunction(nil, vars, funcs)
if p_tok != nil && p_tok.Tag == ast.STRING {
prePrompt = p_tok.Value()
}
}
fmt.Printf(prePrompt)
text, err := line.Prompt(prompt)
if err != nil && err != liner.ErrPromptAborted{
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
}
if !no_hist {
line.WriteHistory(histFile)
}
userInput := ast.Lex(text)
if userInput == nil {
// errors handled in Lex