get parenthesis to work; simplify functions

This commit is contained in:
NunoSempere 2024-06-18 22:28:19 -04:00
parent 039ff56bbe
commit 3133938334

View File

@ -389,57 +389,59 @@ func combineStackAndDist(stack Stack, new_dist Dist, op string) Stack {
}
func combineStackAndNewLine(stack Stack, new_line string) Stack {
func runRepl(stack Stack, reader *bufio.Reader) Stack {
if strings.TrimSpace(new_line) == "" {
return stack
}
// Various stack manipulations that don't involve distributions
replForLoop:
for {
new_line, _ := reader.ReadString('\n')
words := strings.Split(strings.TrimSpace(new_line), " ")
switch {
/* Empty line case */
case strings.TrimSpace(new_line) == "":
continue replForLoop
/* Parenthesis */
case len(words) == 2 && (words[0] == "*" || words[0] == "+" || words[0] == "-" || words[0] == "/") && words[1] == "(":
new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader)
stack = combineStackAndDist(stack, new_stack.old_dist, words[0])
prettyPrintDist(stack.old_dist)
case len(words) == 1 && words[0] == ")":
return stack
/* Special operations */
case words[0] == "exit" || words[0] == "e":
os.Exit(0)
case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG)
return stack
continue replForLoop
case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", stack.old_dist)
fmt.Printf("Vars: %v\n", stack.vars)
return stack
continue replForLoop
case words[0] == "clear" || words[0] == "c" || words[0] == ".":
stack.old_dist = INIT_DIST
fmt.Println()
continue replForLoop
/* Variable assignment */
case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
return stack
case words[0] == "." || words[0] == "clear" || words[0] == "c":
stack.old_dist = INIT_DIST
fmt.Println()
return stack
continue replForLoop
case words[0] == "=." && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
stack.old_dist = INIT_DIST
fmt.Println()
return stack
// Other possible cases: save to file, sample n samples, save stack to a variable, clear stack, define a function
}
continue replForLoop
default:
op, new_dist, err := parseLineIntoOpAndDist(new_line, stack.vars)
if err != nil {
return stack
continue replForLoop
}
stack = combineStackAndDist(stack, new_dist, op)
prettyPrintDist(stack.old_dist)
return stack
}
func runRepl(stack Stack, reader *bufio.Reader) Stack {
for {
new_line, _ := reader.ReadString('\n')
stack = combineStackAndNewLine(stack, new_line)
}
}