commit a212a6c3afcc3576ef8388600ac48b35e35c4e36
parent 0dbe3842a75345554bd1dc8857d7397170a89912
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date: Tue, 18 Jun 2024 20:37:25 -0400
continue refactoring to explicitly manipulate a stack
Diffstat:
| M | fermi.go | | | 120 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
1 file changed, 58 insertions(+), 62 deletions(-)
diff --git a/fermi.go b/fermi.go
@@ -345,77 +345,73 @@ type Stack struct {
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 {
+ if strings.TrimSpace(new_line) == "" {
+ return stack
+ }
+
+ {
+ 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] == "clean" || 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?
+ // clean stack
+ // Define a function? No, too much of a nerdsnipea
+ }
+ }
+
+ op, new_dist, err := parseLine(new_line, stack.vars)
+ if err != nil {
+ return stack
+ }
+
+ joint_dist, err := joinDists(stack.old_dist, new_dist, op)
+ if err != nil {
+ fmt.Printf("%v\n", err)
+ fmt.Printf("Dist on stack: ")
+ prettyPrintDist(stack.old_dist)
+ return stack
+ }
+ stack.old_dist = joint_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)}
-EventForLoop:
for {
- input, _ := reader.ReadString('\n')
- if strings.TrimSpace(input) == "" {
- continue EventForLoop
- }
-
- {
- words := strings.Split(strings.TrimSpace(input), " ")
- switch {
- case words[0] == "exit" || words[0] == "e":
- break EventForLoop
- case words[0] == "help" || words[0] == "h":
- fmt.Println(GENERAL_ERR_MSG)
- continue EventForLoop
- case words[0] == "debug" || words[0] == "d":
- fmt.Printf("Old dist: %v\n", stack.old_dist)
- fmt.Printf("Vars: %v\n", stack.vars)
- continue EventForLoop
- case words[0] == "=:" && len(words) == 2:
- stack.vars[words[1]] = stack.old_dist
- fmt.Printf("%s ", words[1])
- prettyPrintDist(stack.old_dist)
- continue EventForLoop
- case words[0] == "." || words[0] == "clean" || words[0] == "c":
- stack.old_dist = init_dist
- fmt.Println()
- continue EventForLoop
- 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 EventForLoop
- // Other possible cases:
- // Save to file
- // Sample n samples
- // Save stack to a variable?
- // clean stack
- // Define a function? No, too much of a nerdsnipea
- }
- }
-
- op, new_dist, err := parseLine(input, stack.vars)
- if err != nil {
- continue EventForLoop
- }
-
- joint_dist, err := joinDists(stack.old_dist, new_dist, op)
- if err != nil {
- fmt.Printf("%v\n", err)
- fmt.Printf("Dist on stack: ")
- prettyPrintDist(stack.old_dist)
- continue EventForLoop
- }
- stack.old_dist = joint_dist
- prettyPrintDist(stack.old_dist)
+ new_line, _ := reader.ReadString('\n')
+ stack = combineStackAndNewLine(stack, new_line)
}
}