From dfb9f3cc2543ffcda5e661db5be91c114e4d945a Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 9 Jun 2024 12:58:16 +0200 Subject: [PATCH] add some notion of what a more powerful parser could look like --- README.md | 3 ++- f2.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 f2.go diff --git a/README.md b/README.md index 1bd3477..16e201e 100644 --- a/README.md +++ b/README.md @@ -122,5 +122,6 @@ Some possible syntax for a more expressive stack-based DSL 10 to 100 =: y # content of the stack at this point saved into y -x - 10B * y # if interpreting x and y as list of samples, you could do - and + operations as well +x # put x on the stack +- y # substract y from the content of the stack. Requires interpreting x and y as list of samples ``` diff --git a/f2.go b/f2.go new file mode 100644 index 0000000..a1e7b54 --- /dev/null +++ b/f2.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "fmt" + // "math" + "os" + // "strconv" + "strings" +) + +// Actually, I should look up how do do a) enums in go, b) union types +type Lognormal struct { + low float64 + high float64 +} + +type Dist struct { + Type string + Lognormal Lognormal + Samples []float64 +} + +func parseLine(line string) (string, Dist, error) { + + words := strings.Split(strings.TrimSpace(line), " ") + + switch { + case len(words) == 2 && words[0] == "*": + fmt.Printf("Hello world") + } +} + +func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, err) { + return old_dist, nil +} + +func main() { + reader := bufio.NewReader(os.Stdin) + old_dist := Dist{Type: "Lognormal", Lognormal: Lognormal{low: 1, high: 1}, Samples: nil} // Could also just be a scalar + fmt.Printf("Hello world") + +EventForLoop: + for { + input, _ := reader.ReadString('\n') + if strings.TrimSpace(input) == "" { + continue EventForLoop + } + + op, new_dist, err := parseLine(input) + if err != nil { + + } + joint_dist, err := joinDists(old_dist, new_dist, op) + if err != nil { + + } + old_dist = joint_dist + } +}