continue refactoring to explicitly manipulate a stack

This commit is contained in:
NunoSempere 2024-06-18 20:37:25 -04:00
parent 0dbe3842a7
commit a212a6c3af

View File

@ -345,55 +345,40 @@ type Stack struct {
vars map[string]Dist vars map[string]Dist
} }
// Could eventually be a more complex struct with:
// { Dist, VariableMaps, ConfigParams } or smth
/*
func combineStackAndNewLine(stack Stack, new_line string) Stack { func combineStackAndNewLine(stack Stack, new_line string) Stack {
} if strings.TrimSpace(new_line) == "" {
*/ return stack
/* Main event loop */
func main() {
reader := bufio.NewReader(os.Stdin)
stack := Stack{old_dist: init_dist, vars: make(map[string]Dist)}
EventForLoop:
for {
input, _ := reader.ReadString('\n')
if strings.TrimSpace(input) == "" {
continue EventForLoop
} }
{ {
words := strings.Split(strings.TrimSpace(input), " ") words := strings.Split(strings.TrimSpace(new_line), " ")
switch { switch {
case words[0] == "exit" || words[0] == "e": case words[0] == "exit" || words[0] == "e":
break EventForLoop os.Exit(0)
case words[0] == "help" || words[0] == "h": case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG) fmt.Println(GENERAL_ERR_MSG)
continue EventForLoop return stack
case words[0] == "debug" || words[0] == "d": case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", stack.old_dist) fmt.Printf("Old dist: %v\n", stack.old_dist)
fmt.Printf("Vars: %v\n", stack.vars) fmt.Printf("Vars: %v\n", stack.vars)
continue EventForLoop return stack
case words[0] == "=:" && len(words) == 2: case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1]) fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist) prettyPrintDist(stack.old_dist)
continue EventForLoop return stack
case words[0] == "." || words[0] == "clean" || words[0] == "c": case words[0] == "." || words[0] == "clean" || words[0] == "c":
stack.old_dist = init_dist stack.old_dist = init_dist
fmt.Println() fmt.Println()
continue EventForLoop return stack
case words[0] == "=." && len(words) == 2: case words[0] == "=." && len(words) == 2:
stack.vars[words[1]] = stack.old_dist stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1]) fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist) prettyPrintDist(stack.old_dist)
stack.old_dist = init_dist stack.old_dist = init_dist
fmt.Println() fmt.Println()
continue EventForLoop return stack
// Other possible cases: // Other possible cases:
// Save to file // Save to file
// Sample n samples // Sample n samples
@ -403,9 +388,9 @@ EventForLoop:
} }
} }
op, new_dist, err := parseLine(input, stack.vars) op, new_dist, err := parseLine(new_line, stack.vars)
if err != nil { if err != nil {
continue EventForLoop return stack
} }
joint_dist, err := joinDists(stack.old_dist, new_dist, op) joint_dist, err := joinDists(stack.old_dist, new_dist, op)
@ -413,9 +398,20 @@ EventForLoop:
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)
fmt.Printf("Dist on stack: ") fmt.Printf("Dist on stack: ")
prettyPrintDist(stack.old_dist) prettyPrintDist(stack.old_dist)
continue EventForLoop return stack
} }
stack.old_dist = joint_dist stack.old_dist = joint_dist
prettyPrintDist(stack.old_dist) prettyPrintDist(stack.old_dist)
return stack
}
/* Main event loop */
func main() {
reader := bufio.NewReader(os.Stdin)
stack := Stack{old_dist: init_dist, vars: make(map[string]Dist)}
for {
new_line, _ := reader.ReadString('\n')
stack = combineStackAndNewLine(stack, new_line)
} }
} }