SHS/cmd/shs_repl.go

108 lines
2.7 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 (
"os"
"fmt"
"strconv"
"github.com/chzyer/readline"
"gitlab.com/whom/shs/ast"
"gitlab.com/whom/shs/log"
"gitlab.com/whom/shs/stdlib"
)
const (
def_prompt string = "λ "
)
func setLogLvl() {
loglvl := os.Getenv("SH_LOGGING")
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() {
debug := os.Getenv("SH_DEBUG_MODE")
hist := os.Getenv("SH_HIST_FILE")
prompt := os.Getenv("SHS_SH_PROMPT")
var vars ast.VarTable
var funcs ast.FuncTable
funcs = stdlib.GenFuncTable()
vars = &map[string]*ast.Token{}
ast.InitVarTable(vars)
ast.SyncTablesWithOSEnviron = true
ast.ExecWhenFuncUndef = true
var err error
if prompt == "" {
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()
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")
}
}