-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (115 loc) · 3.27 KB
/
Copy pathmain.go
File metadata and controls
131 lines (115 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Command codegen turns model.json into a working implementation of the
// generated form (see FORMS.md) in one of several target languages.
//
// go run ./tools/codegen -lang go model.json
//
// The emitted program is interpreter-shaped — a scheduler loop applying the
// canonical scheduling policy (FORMS.md): fire the enabled transition whose
// name is lexicographically least. Every arc is unrolled into straight-line
// conditionals, so nothing walks an arrow list at runtime.
//
// Output is deterministic: places, transitions and arc lists are all sorted by
// name, so regenerating an unchanged model produces byte-identical source.
// //tools/codegen:codegen_up_to_date_test depends on that.
package main
import (
"bytes"
"embed"
"flag"
"fmt"
"os"
"sort"
"strings"
"text/template"
"github.com/stackdump/pflow-polyglot/tools/codegen/internal/petri"
)
//go:embed templates/*.tmpl
var templates embed.FS
// --- template helpers ------------------------------------------------------
// lowerFirst turns BoilWater into boilWater for languages that want unexported
// or camelCase function names.
func lowerFirst(s string) string {
if s == "" {
return s
}
return strings.ToLower(s[:1]) + s[1:]
}
// snake turns BoilWater into boil_water for Python and Rust.
func snake(s string) string {
var b strings.Builder
for i, r := range s {
if r >= 'A' && r <= 'Z' {
if i > 0 {
b.WriteByte('_')
}
b.WriteRune(r + ('a' - 'A'))
continue
}
b.WriteRune(r)
}
return b.String()
}
// quoteAll renders a list of names as a language-specific literal list body,
// e.g. `"Water", "Cup"`.
func quoteAll(names []string) string {
quoted := make([]string, len(names))
for i, n := range names {
quoted[i] = `"` + n + `"`
}
return strings.Join(quoted, ", ")
}
var funcs = template.FuncMap{
"lowerFirst": lowerFirst,
"snake": snake,
"quoteAll": quoteAll,
"join": strings.Join,
}
var langs = map[string]string{
"go": "templates/go.tmpl",
"rust": "templates/rust.tmpl",
"python": "templates/python.tmpl",
"js": "templates/js.tmpl",
"solidity": "templates/solidity.tmpl",
}
func main() {
lang := flag.String("lang", "", "target language: go, rust, python, js, solidity")
out := flag.String("out", "", "output file (default: stdout)")
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "usage: codegen -lang <lang> [-out <file>] <model.json>")
os.Exit(2)
}
tmplPath, ok := langs[*lang]
if !ok {
names := make([]string, 0, len(langs))
for n := range langs {
names = append(names, n)
}
sort.Strings(names)
fmt.Fprintf(os.Stderr, "unknown -lang %q (have: %s)\n", *lang, strings.Join(names, ", "))
os.Exit(2)
}
model, err := petri.Load(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, "codegen:", err)
os.Exit(1)
}
tmpl, err := template.New(*lang).Funcs(funcs).ParseFS(templates, tmplPath)
if err != nil {
fmt.Fprintln(os.Stderr, "codegen: parsing template:", err)
os.Exit(1)
}
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "main", model); err != nil {
fmt.Fprintln(os.Stderr, "codegen: rendering:", err)
os.Exit(1)
}
if *out == "" {
os.Stdout.Write(buf.Bytes())
return
}
if err := os.WriteFile(*out, buf.Bytes(), 0o644); err != nil {
fmt.Fprintln(os.Stderr, "codegen:", err)
os.Exit(1)
}
}