SHS/cmd/shs_repl.go
2020-07-03 02:02:12 -07:00

118 lines
2.9 KiB
Go

/* SHS: Syntactically Homogeneous Shell
* Copyright (C) 2019 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"strconv"
"github.com/chzyer/readline"
"gitlab.com/whom/shs/ast"
"gitlab.com/whom/shs/log"
"gitlab.com/whom/shs/config"
)
const (
def_prompt string = "λ "
)
func setLogLvl(vars ast.VarTable) {
var loglvl string
loglvl_t := ast.GetVar("SH_LOGGING", vars)
if loglvl_t != nil {
loglvl = loglvl_t.Value()
}
if loglvl != "" {
llvl, err := strconv.ParseInt(loglvl, 10, 8)
if err != nil {
log.Log(log.ERR,
"couldnt parse log level: " + err.Error(),
"init")
} else {
log.SetLogLvl(llvl)
}
}
}
func main() {
var prompt string
var debug string
var hist string
ast.SyncTablesWithOSEnviron = true
ast.ExecWhenFuncUndef = true
vars, funcs := config.InitFromConfig(".shsrc")
debug_t := ast.GetVar("SH_DEBUG_MODE", vars)
if debug_t != nil {
debug = debug_t.Value()
}
hist_t := ast.GetVar("SH_HIST_FILE", vars)
if hist_t != nil {
hist = hist_t.Value()
}
prompt_t := ast.GetVar("SHS_SH_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",
})
defer rl.Close()
if err != nil {
log.Log(log.ERR, "Couldnt initialize readline: " + err.Error(), "repl")
return
}
for {
setLogLvl(vars)
text, err := rl.Readline()
if err != nil {
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
}
userInput := ast.Lex(text)
if userInput == nil {
// errors handled in Lex
// TODO: return an error instead
continue
}
if debug != "" {
ast.PrintSExprsIndividually(userInput)
}
result := userInput.Eval(funcs, vars, false)
if result != nil {
for i := result; i != nil; i = i.Next {
fmt.Printf(i.String() + " ")
}
}
fmt.Printf("\n")
}
}