github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/vector/generate/expr.go (about) 1 package main 2 3 import ( 4 "regexp" 5 ) 6 7 var rxVariable = regexp.MustCompile("\\$[a-zA-Z]+") 8 9 // TODO: handle operation order 10 11 func mul(a, b Expr) Expr { 12 as := a.String() 13 bs := b.String() 14 if as == "1" { 15 return Expr{Expr: bs} 16 } 17 if bs == "1" { 18 return Expr{Expr: as} 19 } 20 return Expr{Expr: as + " * " + bs} 21 } 22 23 func add(a, b Expr) Expr { 24 as := a.String() 25 bs := b.String() 26 if as == "0" { 27 return Expr{Expr: bs} 28 } 29 if bs == "0" { 30 return Expr{Expr: as} 31 } 32 return Expr{Expr: as + " + " + bs} 33 } 34 35 func sub(a, b Expr) Expr { 36 as := a.String() 37 bs := b.String() 38 if as == "0" { 39 return Expr{Expr: "-" + bs} 40 } 41 if bs == "0" { 42 return Expr{Expr: as} 43 } 44 return Expr{Expr: as + " - " + bs} 45 } 46 47 func increment(varname string, by Expr) string { 48 bys := by.String() 49 switch bys { 50 case "1": 51 return varname + "++" 52 } 53 return varname + " += " + bys 54 }