mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 16:47:28 +01:00
First cut of a code generator.
This commit is contained in:
1 parent
7df75101cd
commit
f01f9c2fba
6 files changed
+208
-1
No files matched your search
@@ -0,0 +1,10 @@
|
||||
// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
const preamble = "" +
|
||||
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdint.h>\n" +
|
||||
""
|
||||
@@ -0,0 +1,59 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// +build ignore
|
||||
|
||||
// gen.go converts preamble.h to data.go.
|
||||
//
|
||||
// Invoke it via "go generate".
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
const columns = 1024
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
in, err := ioutil.ReadFile("preamble.h")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out := &bytes.Buffer{}
|
||||
out.WriteString("// Code generated by running \"go generate\". DO NOT EDIT.\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("// Use of this source code is governed by a BSD-style license that can be found\n")
|
||||
out.WriteString("// in the LICENSE file.\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("package main\n")
|
||||
out.WriteString("\n")
|
||||
out.WriteString("const preamble = \"\" +\n")
|
||||
for len(in) > 0 {
|
||||
s := in
|
||||
if len(s) > columns {
|
||||
s = s[:columns]
|
||||
}
|
||||
in = in[len(s):]
|
||||
fmt.Fprintf(out, "%q +\n", s)
|
||||
}
|
||||
out.WriteString("\"\"\n")
|
||||
|
||||
formatted, err := format.Source(out.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile("data.go", formatted, 0644)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
// puffs-gen-c transpiles a Puffs program to a C program.
|
||||
//
|
||||
// The command line arguments list the source Puffs files. If no arguments are
|
||||
// given, it reads from stdin.
|
||||
//
|
||||
// The generated program is written to stdout.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
|
||||
"github.com/google/puffs/lang/generate"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := generate.Main(gen{}); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
type gen struct{}
|
||||
|
||||
func (gen) WritePreamble(w *bytes.Buffer) error {
|
||||
w.WriteString("// Code generated by puffs-gen-c. DO NOT EDIT.\n\n")
|
||||
w.WriteString(preamble)
|
||||
w.WriteString("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gen) WritePostamble(w *bytes.Buffer) error {
|
||||
w.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
#include <stdint.h>
|
||||
@@ -39,7 +39,7 @@ func usage() {
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user