<commit_before>// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

// codegen generates go code from templates. Intended to be
// used with go generate; Also makes mgl64 from mgl32.
// See the invocation in mgl32/util.go for details.
// To use it, just run "go generate github.com/go-gl/mathgl/mgl32"
// (or "go generate" in mgl32 directory).

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"text/template"
)

type Context struct {
	Comment      string
	TemplateName string
}

type MatrixIter struct {
	M     int // row
	N     int // column
	index int
}

var mgl64RewriteRules = []string{
	"mgl32 -> mgl64",
	"float32 -> float64",
	"f32 -> f64",
	"a.Float32 -> a.Float64",
	"math.MaxFloat32 -> math.MaxFloat64",
	"math.SmallestNonzeroFloat32 -> math.SmallestNonzeroFloat64",
}

func main() {
	flag.Usage = func() {
		fmt.Println("Usage: codegen -template file.tmpl -output file.go")
		fmt.Println("Usage: codegen -mgl64 [-dir ../mgl64]")
		flag.PrintDefaults()
	}

	tmplPath := flag.String("template", "file.tmpl", "template path")
	oPath := flag.String("output", "file.go", "output path")
	mgl64 := flag.Bool("mgl64", false, "make mgl64")
	mgl64Path := flag.String("dir", "../mgl64", "path to mgl64 location")

	flag.Parse()
	if flag.NArg() > 0 || flag.NFlag() == 0 {
		flag.Usage()
		os.Exit(2)
	}

	if *mgl64 {
		genMgl64(*mgl64Path)
		return
	}

	tmpl := template.New("").Delims("<<", ">>").Funcs(template.FuncMap{
		"typename":    typenameHelper,
		"elementname": elementNameHelper,
		"iter":        iterHelper,
		"matiter":     matrixIterHelper,
		"enum":        enumHelper,
		"sep":         separatorHelper,
		"repeat":      repeatHelper,
		"add":         addHelper,
		"mul":         mulHelper,
	})
	tmpl = template.Must(tmpl.ParseFiles(*tmplPath))
	tmplName := filepath.Base(*tmplPath)

	oFile, err := os.Create(*oPath)
	if err != nil {
		panic(err)
	}

	context := Context{
		Comment:      "This file is generated by codegen.go; DO NOT EDIT",
		TemplateName: tmplName,
	}
	if err = tmpl.ExecuteTemplate(oFile, tmplName, context); err != nil {
		panic(err)
	}
	oFile.Close()

	if err = rungofmt(*oPath, false, nil); err != nil {
		panic(err)
	}
}

func genMgl64(destPath string) {
	HandleFile := func(source string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		dest := filepath.Join(destPath, source)

		if info.IsDir() {
			return os.MkdirAll(dest, info.Mode())
		}
		if !strings.HasSuffix(source, ".go") || info.Name() == "codegen.go" {
			return nil
		}
		if !info.Mode().IsRegular() {
			fmt.Println("Ignored, not a regular file:", source)
			return nil
		}

		in, err := ioutil.ReadFile(source)
		if err != nil {
			return err
		}

		out, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
			info.Mode())
		if err != nil {
			return err
		}
		defer out.Close()

		comment := fmt.Sprintf(
			"// This file is generated from mgl32/%s; DO NOT EDIT\n\n",
			source)
		if _, err = out.WriteString(comment); err != nil {
			return err
		}<commit_msg>Fix generated code on Windows having backslashes in paths in comments.<commit_after>// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

// codegen generates go code from templates. Intended to be
// used with go generate; Also makes mgl64 from mgl32.
// See the invocation in mgl32/util.go for details.
// To use it, just run "go generate github.com/go-gl/mathgl/mgl32"
// (or "go generate" in mgl32 directory).

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"text/template"
)

type Context struct {
	Comment      string
	TemplateName string
}

type MatrixIter struct {
	M     int // row
	N     int // column
	index int
}

var mgl64RewriteRules = []string{
	"mgl32 -> mgl64",
	"float32 -> float64",
	"f32 -> f64",
	"a.Float32 -> a.Float64",
	"math.MaxFloat32 -> math.MaxFloat64",
	"math.SmallestNonzeroFloat32 -> math.SmallestNonzeroFloat64",
}

func main() {
	flag.Usage = func() {
		fmt.Println("Usage: codegen -template file.tmpl -output file.go")
		fmt.Println("Usage: codegen -mgl64 [-dir ../mgl64]")
		flag.PrintDefaults()
	}

	tmplPath := flag.String("template", "file.tmpl", "template path")
	oPath := flag.String("output", "file.go", "output path")
	mgl64 := flag.Bool("mgl64", false, "make mgl64")
	mgl64Path := flag.String("dir", "../mgl64", "path to mgl64 location")

	flag.Parse()
	if flag.NArg() > 0 || flag.NFlag() == 0 {
		flag.Usage()
		os.Exit(2)
	}

	if *mgl64 {
		genMgl64(*mgl64Path)
		return
	}

	tmpl := template.New("").Delims("<<", ">>").Funcs(template.FuncMap{
		"typename":    typenameHelper,
		"elementname": elementNameHelper,
		"iter":        iterHelper,
		"matiter":     matrixIterHelper,
		"enum":        enumHelper,
		"sep":         separatorHelper,
		"repeat":      repeatHelper,
		"add":         addHelper,
		"mul":         mulHelper,
	})
	tmpl = template.Must(tmpl.ParseFiles(*tmplPath))
	tmplName := filepath.Base(*tmplPath)

	oFile, err := os.Create(*oPath)
	if err != nil {
		panic(err)
	}

	context := Context{
		Comment:      "This file is generated by codegen.go; DO NOT EDIT",
		TemplateName: tmplName,
	}
	if err = tmpl.ExecuteTemplate(oFile, tmplName, context); err != nil {
		panic(err)
	}
	oFile.Close()

	if err = rungofmt(*oPath, false, nil); err != nil {
		panic(err)
	}
}

func genMgl64(destPath string) {
	HandleFile := func(source string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		dest := filepath.Join(destPath, source)

		if info.IsDir() {
			return os.MkdirAll(dest, info.Mode())
		}
		if !strings.HasSuffix(source, ".go") || info.Name() == "codegen.go" {
			return nil
		}
		if !info.Mode().IsRegular() {
			fmt.Println("Ignored, not a regular file:", source)
			return nil
		}

		in, err := ioutil.ReadFile(source)
		if err != nil {
			return err
		}

		out, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
			info.Mode())
		if err != nil {
			return err
		}
		defer out.Close()

		comment := fmt.Sprintf(
			"// This file is generated from mgl32/%s; DO NOT EDIT\n\n",
			filepath.ToSlash(source))
		if _, err = out.WriteString(comment); err != nil {
			return err
		}
{'fraction_non_alphanumeric': 0.1542512002477931, 'fraction_numerical': 0.02137215425120025, 'mean_word_length': 3.212654924983692, 'pattern_counts': {'":': 18, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832063', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2362, 'n_words': 624}