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 {
if strings.TrimSpace(new_line) == "" {
return stack
}
// Various stack manipulations that don't involve distributions
words := strings.Split(strings.TrimSpace(new_line), " ")
switch {
case words[0] == "exit" || words[0] == "e":
os.Exit(0)
case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG)
return stack
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
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
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
}
op, new_dist, err := parseLineIntoOpAndDist(new_line, stack.vars)
if err != nil {
return stack
}
stack = combineStackAndDist(stack, new_dist, op)
prettyPrintDist(stack.old_dist)
return stack
}
func runRepl(stack Stack, reader *bufio.Reader) Stack {
replForLoop:
for {
new_line, _ := reader.ReadString('\n')
stack = combineStackAndNewLine(stack, new_line)
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)
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)
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)
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()
continue replForLoop
default:
op, new_dist, err := parseLineIntoOpAndDist(new_line, stack.vars)
if err != nil {
continue replForLoop
}
stack = combineStackAndDist(stack, new_dist, op)
prettyPrintDist(stack.old_dist)
}
}
}