add some notion of what a more powerful parser could look like

This commit is contained in:
NunoSempere 2024-06-09 12:58:16 +02:00
parent 09583a312c
commit dfb9f3cc25
2 changed files with 62 additions and 1 deletions

View File

@ -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
```

60
f2.go Normal file
View File

@ -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
}
}