From c195c2362e2ea923c23a47f7ee9a384a4c43fcd0 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 1 Jun 2017 10:38:38 +1000 Subject: [PATCH] Remove the cmd/internal/cgen package. There's no longer separate .c and .h generator executables, so there's no need for an (internal) package for common code. --- cmd/{internal/cgen => puffs-gen-c}/cgen.go | 20 +++++++++---------- cmd/{internal/cgen => puffs-gen-c}/data.go | 2 +- cmd/{internal/cgen => puffs-gen-c}/gen.go | 2 +- cmd/puffs-gen-c/main.go | 3 +-- cmd/{internal/cgen => puffs-gen-c}/preamble.h | 0 5 files changed, 13 insertions(+), 14 deletions(-) rename cmd/{internal/cgen => puffs-gen-c}/cgen.go (96%) rename cmd/{internal/cgen => puffs-gen-c}/data.go (98%) rename cmd/{internal/cgen => puffs-gen-c}/gen.go (97%) rename cmd/{internal/cgen => puffs-gen-c}/preamble.h (100%) diff --git a/cmd/internal/cgen/cgen.go b/cmd/puffs-gen-c/cgen.go similarity index 96% rename from cmd/internal/cgen/cgen.go rename to cmd/puffs-gen-c/cgen.go index 100f3d8e..7e350025 100644 --- a/cmd/internal/cgen/cgen.go +++ b/cmd/puffs-gen-c/cgen.go @@ -3,7 +3,7 @@ //go:generate go run gen.go -package cgen +package main import ( "bytes" @@ -24,10 +24,10 @@ const ( priOnly ) -type Generator struct { +type gen struct { } -func (*Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) { +func (gen) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) { b := &bytes.Buffer{} includeGuard := "PUFFS_" + strings.ToUpper(pkgName) + "_H" @@ -362,7 +362,7 @@ func writeField(b *bytes.Buffer, m *t.IDMap, n *a.Field) error { func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uint32) error { if depth > a.MaxBodyDepth { - return fmt.Errorf("cgen: body recursion depth too large") + return fmt.Errorf("body recursion depth too large") } depth++ @@ -375,7 +375,7 @@ func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uin } } // TODO: fix this. - return fmt.Errorf("cgen: cannot convert Puffs type %q to C", x.String(m)) + return fmt.Errorf("cannot convert Puffs type %q to C", x.String(m)) } for _, l := range n.Raw().SubLists() { @@ -390,7 +390,7 @@ func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uin func writeStatement(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uint32) error { if depth > a.MaxBodyDepth { - return fmt.Errorf("cgen: body recursion depth too large") + return fmt.Errorf("body recursion depth too large") } depth++ @@ -451,12 +451,12 @@ func writeStatement(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, dept // TODO. } - return fmt.Errorf("cgen: unrecognized ast.Kind (%s) for writeStatement", n.Kind()) + return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind()) } func writeExpr(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error { if depth > a.MaxExprDepth { - return fmt.Errorf("cgen: expression recursion depth too large") + return fmt.Errorf("expression recursion depth too large") } depth++ @@ -484,7 +484,7 @@ func writeExpr(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uin return err } default: - return fmt.Errorf("cgen: unrecognized token.Key (0x%X) for writeExpr", n.ID0().Key()) + return fmt.Errorf("unrecognized token.Key (0x%X) for writeExpr", n.ID0().Key()) } return nil @@ -525,7 +525,7 @@ func writeExprOther(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, dept b.WriteString(n.ID1().String(m)) return nil } - return fmt.Errorf("cgen: unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key()) + return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key()) } func writeExprUnaryOp(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error { diff --git a/cmd/internal/cgen/data.go b/cmd/puffs-gen-c/data.go similarity index 98% rename from cmd/internal/cgen/data.go rename to cmd/puffs-gen-c/data.go index e578f08c..b43c6bcb 100644 --- a/cmd/internal/cgen/data.go +++ b/cmd/puffs-gen-c/data.go @@ -3,7 +3,7 @@ // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. -package cgen +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 \n#include \n#include \n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n\n// PUFFS_VERSION is the major.minor version number as a uint32. The major\n// number is the high 16 bits. The minor number is the low 16 bits.\n//\n// The intention is to bump the version number at least on every API / ABI\n// backwards incompatible change.\n//\n// For now, the API and ABI are simply unstable and can change at any time.\n//\n// TODO: don't hard code this in preamble.h.\n#define PUFFS_VERSION (0x00001)\n" + diff --git a/cmd/internal/cgen/gen.go b/cmd/puffs-gen-c/gen.go similarity index 97% rename from cmd/internal/cgen/gen.go rename to cmd/puffs-gen-c/gen.go index 2f2f67dd..f4490f8a 100644 --- a/cmd/internal/cgen/gen.go +++ b/cmd/puffs-gen-c/gen.go @@ -47,7 +47,7 @@ func main1() error { 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 cgen\n") + out.WriteString("package main\n") out.WriteString("\n") out.WriteString("const preamble = \"\" +\n") for len(in) > 0 { diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index eb7cac91..576c8f42 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -10,10 +10,9 @@ package main import ( - "github.com/google/puffs/cmd/internal/cgen" "github.com/google/puffs/lang/generate" ) func main() { - generate.Main(&cgen.Generator{}) + generate.Main(gen{}) } diff --git a/cmd/internal/cgen/preamble.h b/cmd/puffs-gen-c/preamble.h similarity index 100% rename from cmd/internal/cgen/preamble.h rename to cmd/puffs-gen-c/preamble.h