Add template and saving functions

This commit is contained in:
2024-11-05 23:58:59 +01:00
parent fd8395fec1
commit cf7675ad23
2 changed files with 50 additions and 3 deletions

2
function.tmpl Normal file
View File

@@ -0,0 +1,2 @@
---@diagnostic disable: missing-return, lowercase-global
function {{.Name}}({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}: {{$param.Type}}{{end}}) end

View File

@@ -1,7 +1,35 @@
package main
import (
_ "embed"
"log"
"os"
"text/template"
)
var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
}
//go:embed function.tmpl
var templatestr string
var functionTemplate *template.Template
func init() {
var err error
functionTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
if err != nil {
Error.Printf("Error parsing template: %v", err)
return
}
log.Printf("%#v", functionTemplate)
}
type (
Function struct {
Name string
Arguments []Argument
Returns []Argument
}
@@ -11,3 +39,20 @@ type (
Description string
}
)
func (f *Function) ResolveFileName() string {
return f.Name + ".lua"
}
func (f *Function) WriteFile() error {
file, err := os.Create(f.ResolveFileName())
if err != nil {
return err
}
defer file.Close()
err = functionTemplate.Execute(file, f)
if err != nil {
return err
}
return nil
}