Fixes the quickfix for repl paren counting

master
sloum 2 years ago
parent c041e61673
commit 99e2779cd9

@ -504,16 +504,30 @@ func createTimeFormatString(s string) string {
// Used by REPL to know if another input line
// should be offered before parsing
func stringParensMatch(s string) bool {
t := Tokenize(s)
count := 0
for _, v := range t {
switch v {
case "(":
count++
case ")":
count--
inString := false
prev := ' '
for _, c := range s {
switch c {
case '(':
if !inString && prev != '\'' {
count++
}
case ')':
if !inString {
count--
}
case '"':
if !inString {
inString = true
} else if prev != '\\' {
inString = false
}
}
prev = c
}
if count > 0 {
return false
}

@ -104,6 +104,46 @@ func outputResult(txt string) {
}
}
func Repl() {
initialTerm, _ = ln.TerminalMode()
line = ln.NewLiner()
line.SetCtrlCAborts(true)
linerTerm, _ = ln.TerminalMode()
defer line.Close()
histFile := ExpandedAbsFilepath(filepath.Join(getModBaseDir(), "..", historyFilename))
if f, e := os.Open(histFile); e == nil {
line.ReadHistory(f)
f.Close()
}
var text strings.Builder
var cont bool
for {
globalenv.vars[symbol("slope-interactive?")] = true
if linerTerm != nil {
linerTerm.ApplyMode()
}
in := prompt(line, cont)
if initialTerm != nil {
initialTerm.ApplyMode()
}
if len(strings.TrimSpace(in)) == 0 {
continue
}
text.WriteString(in)
text.WriteRune('\n')
if !stringParensMatch(text.String()){
cont = true
} else {
cont = false
outputResult(text.String())
text.Reset()
}
}
}
func RunCommand(s string) {
if strings.Count(s, "(") != strings.Count(s, ")") {
fmt.Fprintf(os.Stderr, "Invalid input string, uneven parens")
@ -235,45 +275,6 @@ func PrintVersion() {
}
}
func Repl() {
initialTerm, _ = ln.TerminalMode()
line = ln.NewLiner()
line.SetCtrlCAborts(true)
linerTerm, _ = ln.TerminalMode()
defer line.Close()
histFile := ExpandedAbsFilepath(filepath.Join(getModBaseDir(), "..", historyFilename))
if f, e := os.Open(histFile); e == nil {
line.ReadHistory(f)
f.Close()
}
var text strings.Builder
var cont bool
for {
globalenv.vars[symbol("slope-interactive?")] = true
if linerTerm != nil {
linerTerm.ApplyMode()
}
in := prompt(line, cont)
if initialTerm != nil {
initialTerm.ApplyMode()
}
if len(strings.TrimSpace(in)) == 0 {
continue
}
text.WriteString(in)
text.WriteRune('\n')
if !stringParensMatch(text.String()){
cont = true
} else {
cont = false
outputResult(text.String())
text.Reset()
}
}
}
func main() {
defer RecoverError()
verFlag := flag.Bool("v", false, "Print version information and exit")

Loading…
Cancel
Save