2020-06-21 01:30:54 -07:00
|
|
|
/* 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"
|
2020-06-27 21:27:33 -07:00
|
|
|
"github.com/chzyer/readline"
|
2020-06-28 13:47:44 -07:00
|
|
|
"gitlab.com/whom/shs/ast"
|
|
|
|
|
"gitlab.com/whom/shs/log"
|
2020-07-03 02:02:12 -07:00
|
|
|
"gitlab.com/whom/shs/config"
|
2020-06-21 01:30:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2020-06-27 21:27:33 -07:00
|
|
|
def_prompt string = "λ "
|
2020-06-21 01:30:54 -07:00
|
|
|
)
|
|
|
|
|
|
2020-07-03 02:02:12 -07:00
|
|
|
func setLogLvl(vars ast.VarTable) {
|
|
|
|
|
var loglvl string
|
|
|
|
|
|
|
|
|
|
loglvl_t := ast.GetVar("SH_LOGGING", vars)
|
|
|
|
|
if loglvl_t != nil {
|
|
|
|
|
loglvl = loglvl_t.Value()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 01:30:54 -07:00
|
|
|
if loglvl != "" {
|
2020-06-21 11:11:57 -07:00
|
|
|
llvl, err := strconv.ParseInt(loglvl, 10, 8)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Log(log.ERR,
|
|
|
|
|
"couldnt parse log level: " + err.Error(),
|
2020-06-21 01:30:54 -07:00
|
|
|
"init")
|
|
|
|
|
} else {
|
|
|
|
|
log.SetLogLvl(llvl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2020-07-03 02:02:12 -07:00
|
|
|
var prompt string
|
|
|
|
|
var debug string
|
|
|
|
|
var hist string
|
2020-06-21 01:30:54 -07:00
|
|
|
|
2020-06-26 21:46:00 -07:00
|
|
|
ast.SyncTablesWithOSEnviron = true
|
2020-06-29 19:15:00 -07:00
|
|
|
ast.ExecWhenFuncUndef = true
|
2020-06-21 12:46:25 -07:00
|
|
|
|
2020-07-03 02:02:12 -07:00
|
|
|
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()
|
|
|
|
|
}
|
2020-06-21 01:30:54 -07:00
|
|
|
|
2020-07-03 02:02:12 -07:00
|
|
|
prompt_t := ast.GetVar("SHS_SH_PROMPT", vars)
|
|
|
|
|
if prompt_t != nil {
|
|
|
|
|
prompt = prompt_t.Value()
|
|
|
|
|
} else {
|
2020-06-21 01:30:54 -07:00
|
|
|
prompt = def_prompt
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 21:27:33 -07:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 01:30:54 -07:00
|
|
|
for {
|
2020-07-03 02:02:12 -07:00
|
|
|
setLogLvl(vars)
|
2020-06-27 21:27:33 -07:00
|
|
|
text, err := rl.Readline()
|
2020-06-21 01:30:54 -07:00
|
|
|
if err != nil {
|
2020-06-21 11:11:57 -07:00
|
|
|
log.Log(log.ERR, "couldnt read user input: " + err.Error(), "repl")
|
2020-06-21 01:30:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userInput := ast.Lex(text)
|
|
|
|
|
if userInput == nil {
|
|
|
|
|
// errors handled in Lex
|
|
|
|
|
// TODO: return an error instead
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if debug != "" {
|
2020-06-21 11:11:57 -07:00
|
|
|
ast.PrintSExprsIndividually(userInput)
|
2020-06-21 01:30:54 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-29 19:15:00 -07:00
|
|
|
result := userInput.Eval(funcs, vars, false)
|
2020-06-27 20:31:49 -07:00
|
|
|
if result != nil {
|
|
|
|
|
for i := result; i != nil; i = i.Next {
|
|
|
|
|
fmt.Printf(i.String() + " ")
|
|
|
|
|
}
|
2020-06-21 15:32:47 -07:00
|
|
|
}
|
2020-06-27 20:31:49 -07:00
|
|
|
|
2020-06-21 15:32:47 -07:00
|
|
|
fmt.Printf("\n")
|
2020-06-21 01:30:54 -07:00
|
|
|
}
|
|
|
|
|
}
|