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 (
|
2020-06-21 11:11:57 -07:00
|
|
|
"os"
|
2020-06-21 01:30:54 -07:00
|
|
|
"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"
|
|
|
|
|
"gitlab.com/whom/shs/stdlib"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func setLogLvl() {
|
|
|
|
|
loglvl := os.Getenv("SH_LOGGING")
|
|
|
|
|
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() {
|
|
|
|
|
debug := os.Getenv("SH_DEBUG_MODE")
|
|
|
|
|
hist := os.Getenv("SH_HIST_FILE")
|
|
|
|
|
prompt := os.Getenv("SHS_SH_PROMPT")
|
|
|
|
|
|
|
|
|
|
var vars ast.VarTable
|
2020-06-21 11:11:57 -07:00
|
|
|
var funcs ast.FuncTable
|
2020-06-21 01:30:54 -07:00
|
|
|
|
2020-06-21 12:46:25 -07:00
|
|
|
funcs = stdlib.GenFuncTable()
|
|
|
|
|
vars = &map[string]*ast.Token{}
|
2020-06-26 21:17:18 -07:00
|
|
|
ast.InitVarTable(vars)
|
2020-06-26 21:46:00 -07:00
|
|
|
ast.SyncTablesWithOSEnviron = true
|
2020-06-21 12:46:25 -07:00
|
|
|
|
2020-06-21 01:30:54 -07:00
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if prompt == "" {
|
|
|
|
|
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 {
|
|
|
|
|
setLogLvl()
|
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-21 15:32:47 -07:00
|
|
|
result, unwrap := userInput.Eval(funcs, vars)
|
2020-06-27 20:31:49 -07:00
|
|
|
if result != nil {
|
|
|
|
|
if result.Tag == ast.LIST && unwrap {
|
|
|
|
|
result = result.Inner.(*ast.Token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|