commit 29ea33811208377cfeb5e9a4a0b235ed3d577bf7
parent de72432c53db09dc4e3f174d15afbf764967e087
Author: NunoSempere <nuno.semperelh@protonmail.com>
Date: Sun, 9 Jun 2024 13:43:03 +0200
continue with new parser
Diffstat:
| M | f2.go | | | 44 | +++++++++++++++++++++++++++----------------- |
1 file changed, 27 insertions(+), 17 deletions(-)
diff --git a/f2.go b/f2.go
@@ -4,10 +4,9 @@ import (
"bufio"
"errors"
"fmt"
-
- // "math"
+ "math"
"os"
- // "strconv"
+ "strconv"
"strings"
)
@@ -25,40 +24,51 @@ type Dist struct {
Samples []float64
}
-// Parse line into Distribution,
+// Parse line into Distribution
+func parseLineErr(err_msg string) (string, Dist, error) {
+ return "", Dist{}, errors.New(err_msg)
+}
func parseLine(line string) (string, Dist, error) {
words := strings.Split(strings.TrimSpace(line), " ")
- op = ""
+ op := ""
var dist Dist
switch words[0] {
case "*":
op = "*"
+ words = words[1:]
case "/":
op = "/"
+ words = words[1:]
case "+":
- return "", Dist{}, error.New("+ operation not implemented yet")
+ return parseLineErr("+ operation not implemented yet")
case "-":
- return "", Dist{}, error.New("- operation not implemented yet")
+ return parseLineErr("- operation not implemented yet")
default:
- return "", Dist{}, error.New("operation not recognized")
+ op = "*" // later, change the below to
}
switch len(words) {
+ case 0:
+ return parseLineErr("Can't operate on nothing")
case 1:
- return "", Dist{}, error.New("Can't operate on nothing")
- case 2:
- single_float, err1 := strconv.ParseFloat(words[1], 64)
+ single_float, err1 := strconv.ParseFloat(words[0], 64)
if err1 != nil {
fmt.Println("Trying to operate on a scalar, but scalar is not a float")
- fmt.Println(error_msg_cont)
- continue EventForLoop
}
- new_low = single_float
- new_high = single_float
-
+ dist = Dist{Type: "Lognormal", Lognormal: Lognormal{low: single_float, high: single_float}, Samples: nil}
+ case 2:
+ new_low, err1 := strconv.ParseFloat(words[0], 64)
+ new_high, err2 := strconv.ParseFloat(words[1], 64)
+ if err1 != nil || err2 != nil {
+ return parseLineErr("Trying to operate by a distribution, but distribution is not specified as two floats")
+ }
+ dist = Dist{Type: "Lognormal", Lognormal: Lognormal{low: new_low, high: new_high}, Samples: nil}
+ default:
+ return parseLineErr("Other input methods not implemented yet")
}
+ return op, dist, nil
}
@@ -94,7 +104,7 @@ func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
switch {
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "*":
low, high := multiplyLogBounds(old_dist.Lognormal.low, old_dist.Lognormal.high, new_dist.Lognormal.low, new_dist.Lognormal.high)
- return Dist{Type: "Lognormal", Lognormal: Lognormal{low: low, high: high}, Samples: nil}
+ return Dist{Type: "Lognormal", Lognormal: Lognormal{low: low, high: high}, Samples: nil}, nil
default:
fmt.Printf("For now, can't do anything besides multiplying lognormals")
}