From 77ce00970ff13322997758c7f248b482db2828ea Mon Sep 17 00:00:00 2001 From: Aidan Date: Fri, 17 Jul 2020 22:38:15 -0700 Subject: [PATCH] print statement should allow for escaping stuff --- ast/print.go | 3 ++- cmd/shs.go | 20 ++++++++++++++++++-- stdlib/string.go | 16 +++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/ast/print.go b/ast/print.go index 944b698..ea5251c 100644 --- a/ast/print.go +++ b/ast/print.go @@ -18,6 +18,7 @@ package ast import ( + "fmt" "strings" ) @@ -49,7 +50,7 @@ loop: constructor.WriteString(iter.FmtToken()) } - println(constructor.String()) + fmt.Printf(constructor.String() + "\n") goto loop } diff --git a/cmd/shs.go b/cmd/shs.go index 1535264..4b43d3c 100644 --- a/cmd/shs.go +++ b/cmd/shs.go @@ -31,6 +31,22 @@ const ( def_prompt string = "λ " ) + +// useful for when input contains escape sequences +// not checking delims cause thats up to the user who defines their prompts +func parseString(in string) string { + in = "\"" + in + "\"" + out, err := strconv.Unquote(in) + if err != nil { + log.Log(log.ERR, + "Couldnt parse (pre?)prompt", + "init") + return "" + } + + return out +} + func setLogLvl(vars ast.VarTable) { var loglvl string @@ -80,7 +96,7 @@ func main() { prompt_t := ast.GetVar("SHS_STATIC_PROMPT", vars) if prompt_t != nil { - prompt = prompt_t.Value() + prompt = parseString(prompt_t.Value()) } else { prompt = def_prompt } @@ -110,7 +126,7 @@ func main() { 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() + prePrompt = parseString(p_tok.Value()) } } diff --git a/stdlib/string.go b/stdlib/string.go index b8321d7..ed1b861 100644 --- a/stdlib/string.go +++ b/stdlib/string.go @@ -19,6 +19,7 @@ package stdlib import ( "fmt" + "strconv" "gitlab.com/whom/shs/ast" "gitlab.com/whom/shs/log" ) @@ -43,6 +44,19 @@ func concat(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { } func print_str(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token { - fmt.Println(in.Eval(ft, vt, false)) + body := in.Eval(ft, vt, false).String() + if body[0] != body[len(body)-1] && body[0] != '"' { + body = "`" + body + "`" + } + + text, err := strconv.Unquote(body) + if err != nil { + log.Log(log.ERR, + "error unquoting string", + "print") + return nil + } + + fmt.Printf(text + "\n") return nil }