go-hep.org/x/hep@v0.38.1/groot/rtree/rfunc/gen-rfuncs.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"log"
    12  	"os"
    13  
    14  	"go-hep.org/x/hep/groot/internal/genroot"
    15  )
    16  
    17  func main() {
    18  	genRFuncs()
    19  }
    20  
    21  type Func struct {
    22  	Name  string
    23  	Funcs []genroot.RFunc
    24  }
    25  
    26  func genRFuncs() {
    27  	for _, typ := range []Func{
    28  		{
    29  			Name: "bool",
    30  			Funcs: []genroot.RFunc{
    31  				{Def: "func() bool"},
    32  				// float32
    33  				{Def: "func(x1 float32) bool"},
    34  				{Def: "func(x1,x2 float32) bool"},
    35  				{Def: "func(x1,x2,x3 float32) bool"},
    36  				// float64
    37  				{Def: "func(x1 float64) bool"},
    38  				{Def: "func(x1,x2 float64) bool"},
    39  				{Def: "func(x1,x2,x3 float64) bool"},
    40  			},
    41  		},
    42  		{
    43  			Name: "i32",
    44  			Funcs: []genroot.RFunc{
    45  				{Def: "func() int32"},
    46  				{Def: "func(x1 int32) int32"},
    47  				{Def: "func(x1,x2 int32) int32"},
    48  				{Def: "func(x1,x2,x3 int32) int32"},
    49  			},
    50  		},
    51  		{
    52  			Name: "i64",
    53  			Funcs: []genroot.RFunc{
    54  				{Def: "func() int64"},
    55  				{Def: "func(x1 int64) int64"},
    56  				{Def: "func(x1,x2 int64) int64"},
    57  				{Def: "func(x1,x2,x3 int64) int64"},
    58  			},
    59  		},
    60  		{
    61  			Name: "u32",
    62  			Funcs: []genroot.RFunc{
    63  				{Def: "func() uint32"},
    64  				{Def: "func(x1 uint32) uint32"},
    65  				{Def: "func(x1,x2 uint32) uint32"},
    66  				{Def: "func(x1,x2,x3 uint32) uint32"},
    67  			},
    68  		},
    69  		{
    70  			Name: "u64",
    71  			Funcs: []genroot.RFunc{
    72  				{Def: "func() uint64"},
    73  				{Def: "func(x1 uint64) uint64"},
    74  				{Def: "func(x1,x2 uint64) uint64"},
    75  				{Def: "func(x1,x2,x3 uint64) uint64"},
    76  			},
    77  		},
    78  		{
    79  			Name: "f32",
    80  			Funcs: []genroot.RFunc{
    81  				// float32
    82  				{Def: "func() float32"},
    83  				{Def: "func(x1 float32) float32"},
    84  				{Def: "func(x1,x2 float32) float32"},
    85  				{Def: "func(x1,x2,x3 float32) float32"},
    86  			},
    87  		},
    88  		{
    89  			Name: "f64",
    90  			Funcs: []genroot.RFunc{
    91  				// int32
    92  				{Def: "func(x1 int32) float64"},
    93  				// float32
    94  				{Def: "func(x1 float32) float64"},
    95  				// float64
    96  				{Def: "func() float64"},
    97  				{Def: "func(x1 float64) float64"},
    98  				{Def: "func(x1,x2 float64) float64"},
    99  				{Def: "func(x1,x2,x3 float64) float64"},
   100  			},
   101  		},
   102  		// slices
   103  		{
   104  			Name: "f64s",
   105  			Funcs: []genroot.RFunc{
   106  				// float32s
   107  				{Def: "func(xs []float32) []float64"},
   108  				// float64s
   109  				{Def: "func(xs []float64) []float64"},
   110  			},
   111  		},
   112  	} {
   113  		genRFunc(typ)
   114  	}
   115  }
   116  
   117  func genRFunc(typ Func) {
   118  	fcodeName := "./rfunc_" + typ.Name + "_gen.go"
   119  	yearCode := genroot.ExtractYear(fcodeName)
   120  
   121  	ftestName := "./rfunc_" + typ.Name + "_gen_test.go"
   122  	yearTest := genroot.ExtractYear(ftestName)
   123  
   124  	f, err := os.Create(fcodeName)
   125  	if err != nil {
   126  		log.Fatal(err)
   127  	}
   128  	defer f.Close()
   129  
   130  	ft, err := os.Create(ftestName)
   131  	if err != nil {
   132  		log.Fatal(err)
   133  	}
   134  	defer ft.Close()
   135  
   136  	genroot.GenImports(yearCode, "rfunc", f,
   137  		"fmt",
   138  	)
   139  
   140  	genroot.GenImports(yearTest, "rfunc", ft,
   141  		"reflect",
   142  		"testing",
   143  	)
   144  
   145  	for i := range typ.Funcs {
   146  		if i > 0 {
   147  			fmt.Fprintf(f, "\n")
   148  			fmt.Fprintf(ft, "\n")
   149  		}
   150  		fct := typ.Funcs[i]
   151  		fct.Pkg = "go-hep.org/x/hep/groot/rtree/rfunc"
   152  		gen, err := genroot.NewRFuncGenerator(f, fct)
   153  		if err != nil {
   154  			log.Fatalf("could not create generator: %+v", err)
   155  		}
   156  		err = gen.Generate()
   157  		if err != nil {
   158  			log.Fatalf("could not generate code for %q: %v\n", fct.Def, err)
   159  		}
   160  		err = gen.GenerateTest(ft)
   161  		if err != nil {
   162  			log.Fatalf("could not generate test for %q: %v\n", fct.Def, err)
   163  		}
   164  	}
   165  
   166  	err = f.Close()
   167  	if err != nil {
   168  		log.Fatal(err)
   169  	}
   170  	genroot.GoFmt(f)
   171  
   172  	err = ft.Close()
   173  	if err != nil {
   174  		log.Fatal(err)
   175  	}
   176  	genroot.GoFmt(ft)
   177  }