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

120
fermi.go
View File

@ -345,77 +345,73 @@ 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
}
{
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 */ /* Main event loop */
func main() { func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
stack := Stack{old_dist: init_dist, vars: make(map[string]Dist)} stack := Stack{old_dist: init_dist, vars: make(map[string]Dist)}
EventForLoop:
for { for {
input, _ := reader.ReadString('\n') new_line, _ := reader.ReadString('\n')
if strings.TrimSpace(input) == "" { stack = combineStackAndNewLine(stack, new_line)
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)
} }
} }