add echo flag for use when consuming files

This commit is contained in:
NunoSempere 2024-08-09 11:39:38 -04:00
parent 0fb2e7acb3
commit a1595935d6
2 changed files with 9 additions and 5 deletions

View File

@ -170,11 +170,11 @@ Done:
- [x] Document parenthesis syntax - [x] Document parenthesis syntax
- [x] Specify number of samples as a command line option - [x] Specify number of samples as a command line option
- [x] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top? - [x] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top?
- [x] Make -n flag work
- [x] Add flag to repeat input lines (useful when reading from files)
To (possibly) do: To (possibly) do:
- [ ] Add flag to repeat input lines (useful when reading from files)
- [ ] Make -n flag work
- [ ] Add functions. Now easier to do with an explicit representation of the stakc - [ ] Add functions. Now easier to do with an explicit representation of the stakc
- [ ] Think about how to draw a histogram from samples - [ ] Think about how to draw a histogram from samples
- [ ] Dump samples to file - [ ] Dump samples to file

View File

@ -356,10 +356,13 @@ func parseWordsIntoOpAndDist(words []string, vars map[string]Dist) (string, Dist
// We want this as a function (rather than just be in main) // We want this as a function (rather than just be in main)
// to be able to have parenthesis/recusion, possibly functions // to be able to have parenthesis/recusion, possibly functions
func runRepl(stack Stack, reader *bufio.Reader) Stack { func runRepl(stack Stack, reader *bufio.Reader, echo_flag *bool) Stack {
replForLoop: replForLoop:
for { for {
new_line, _ := reader.ReadString('\n') new_line, _ := reader.ReadString('\n')
if *echo_flag {
fmt.Printf(new_line)
}
new_line_before_comments, _, _ := strings.Cut(new_line, "#") new_line_before_comments, _, _ := strings.Cut(new_line, "#")
new_line_trimmed := strings.TrimSpace(new_line_before_comments) new_line_trimmed := strings.TrimSpace(new_line_before_comments)
words := strings.Split(new_line_trimmed, " ") words := strings.Split(new_line_trimmed, " ")
@ -369,7 +372,7 @@ replForLoop:
continue replForLoop continue replForLoop
/* Parenthesis */ /* Parenthesis */
case len(words) == 2 && (words[0] == "*" || words[0] == "+" || words[0] == "-" || words[0] == "/") && words[1] == "(": 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) new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader, echo_flag)
combined_dist, err := operateDists(stack.old_dist, new_stack.old_dist, words[0]) combined_dist, err := operateDists(stack.old_dist, new_stack.old_dist, words[0])
if err == nil { if err == nil {
stack.old_dist = combined_dist stack.old_dist = combined_dist
@ -412,6 +415,7 @@ replForLoop:
func main() { func main() {
num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples") num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples")
filename := flag.String("f", "", "Specifies a file with a model to run") filename := flag.String("f", "", "Specifies a file with a model to run")
echo_flag := flag.Bool("echo", false, "Specifies whether inputs should be echoed back. Useful if reading from a file.")
flag.Parse() flag.Parse()
N_SAMPLES = *num_samples_flag N_SAMPLES = *num_samples_flag
@ -428,6 +432,6 @@ 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)}
runRepl(stack, reader) runRepl(stack, reader, echo_flag)
} }