mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Add a lang/builtin package.
This commit is contained in:
1 parent
010c4db54f
commit
2efdf5fab6
5 files changed
+80
-67
No files matched your search
@@ -14,6 +14,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/puffs/lang/base38"
|
"github.com/google/puffs/lang/base38"
|
||||||
|
"github.com/google/puffs/lang/builtin"
|
||||||
"github.com/google/puffs/lang/check"
|
"github.com/google/puffs/lang/check"
|
||||||
"github.com/google/puffs/lang/generate"
|
"github.com/google/puffs/lang/generate"
|
||||||
|
|
||||||
@@ -83,43 +84,16 @@ const (
|
|||||||
"// - bits 0-7 are a package-namespaced numeric code\n"
|
"// - bits 0-7 are a package-namespaced numeric code\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
var builtInStatuses = [...]string{
|
|
||||||
"status ok",
|
|
||||||
"error bad puffs version",
|
|
||||||
"error bad receiver",
|
|
||||||
"error bad argument",
|
|
||||||
"error initializer not called",
|
|
||||||
"error closed for writes",
|
|
||||||
"error unexpected EOF", // Used if reading when closed == true.
|
|
||||||
"suspension short read", // Used if reading when closed == false.
|
|
||||||
"suspension short write",
|
|
||||||
"suspension limited read",
|
|
||||||
"suspension limited write",
|
|
||||||
}
|
|
||||||
|
|
||||||
var builtInStatusSuffixes = map[string]string{
|
|
||||||
// TODO: fill in the rest of the built in statuses.
|
|
||||||
"short read": "SUSPENSION_SHORT_READ",
|
|
||||||
"short write": "SUSPENSION_SHORT_WRITE",
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// The +1 is for the error bit (the sign bit).
|
// The +1 is for the error bit (the sign bit).
|
||||||
if statusCodeNamespaceShift+base38.MaxBits+1 != 32 {
|
if statusCodeNamespaceShift+base38.MaxBits+1 != 32 {
|
||||||
panic("inconsistent status code namespace shift")
|
panic("inconsistent status code namespace shift")
|
||||||
}
|
}
|
||||||
if len(builtInStatuses) > maxNamespacedStatusCode {
|
if len(builtin.StatusList) > maxNamespacedStatusCode {
|
||||||
panic("too many builtInStatuses")
|
panic("too many built-in statuses")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimQuotes(s string) string {
|
|
||||||
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
|
||||||
return s[1 : len(s)-1]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
type replacementPolicy bool
|
type replacementPolicy bool
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -241,13 +215,13 @@ func (g *gen) genHeader(b *buffer) error {
|
|||||||
pkgID := g.checker.PackageID()
|
pkgID := g.checker.PackageID()
|
||||||
b.printf("#define %spackageid %d // %#08x\n\n", g.pkgPrefix, pkgID, pkgID)
|
b.printf("#define %spackageid %d // %#08x\n\n", g.pkgPrefix, pkgID, pkgID)
|
||||||
|
|
||||||
for i, s := range builtInStatuses {
|
for i, z := range builtin.StatusList {
|
||||||
code := uint32(0)
|
code := uint32(0)
|
||||||
if strings.HasPrefix(s, "error ") {
|
if z.Keyword == t.IDError {
|
||||||
code |= 1 << 31
|
code |= 1 << 31
|
||||||
}
|
}
|
||||||
code |= uint32(i)
|
code |= uint32(i)
|
||||||
b.printf("#define %s %d // %#08x\n", strings.ToUpper(g.cName(s)), int32(code), code)
|
b.printf("#define %s %d // %#08x\n", strings.ToUpper(g.cName(z.String())), int32(code), code)
|
||||||
}
|
}
|
||||||
b.writes("\n")
|
b.writes("\n")
|
||||||
|
|
||||||
@@ -305,17 +279,9 @@ func (g *gen) genImpl(b *buffer) error {
|
|||||||
b.writes("// ---------------- Status Codes Implementations\n\n")
|
b.writes("// ---------------- Status Codes Implementations\n\n")
|
||||||
b.printf("bool %sstatus_is_error(%sstatus s) { return s < 0; }\n\n", g.pkgPrefix, g.pkgPrefix)
|
b.printf("bool %sstatus_is_error(%sstatus s) { return s < 0; }\n\n", g.pkgPrefix, g.pkgPrefix)
|
||||||
|
|
||||||
b.printf("const char* %sstatus_strings0[%d] = {\n", g.pkgPrefix, len(builtInStatuses))
|
b.printf("const char* %sstatus_strings0[%d] = {\n", g.pkgPrefix, len(builtin.StatusList))
|
||||||
for _, s := range builtInStatuses {
|
for _, z := range builtin.StatusList {
|
||||||
if strings.HasPrefix(s, "status ") {
|
b.printf("%q,", g.pkgName+": "+z.Message)
|
||||||
s = s[len("status "):]
|
|
||||||
} else if strings.HasPrefix(s, "suspension ") {
|
|
||||||
s = s[len("suspension "):]
|
|
||||||
} else if strings.HasPrefix(s, "error ") {
|
|
||||||
s = s[len("error "):]
|
|
||||||
}
|
|
||||||
s = g.pkgName + ": " + s
|
|
||||||
b.printf("%q,", s)
|
|
||||||
}
|
}
|
||||||
b.writes("};\n\n")
|
b.writes("};\n\n")
|
||||||
|
|
||||||
@@ -329,8 +295,7 @@ func (g *gen) genImpl(b *buffer) error {
|
|||||||
b.printf("const char** a = NULL;\n")
|
b.printf("const char** a = NULL;\n")
|
||||||
b.printf("uint32_t n = 0;\n")
|
b.printf("uint32_t n = 0;\n")
|
||||||
b.printf("switch ((s >> %d) & %#x) {\n", statusCodeNamespaceShift, statusCodeNamespaceMask)
|
b.printf("switch ((s >> %d) & %#x) {\n", statusCodeNamespaceShift, statusCodeNamespaceMask)
|
||||||
b.printf("case 0: a = %sstatus_strings0; n = %d; break;\n",
|
b.printf("case 0: a = %sstatus_strings0; n = %d; break;\n", g.pkgPrefix, len(builtin.StatusList))
|
||||||
g.pkgPrefix, len(builtInStatuses))
|
|
||||||
b.printf("case %spackageid: a = %sstatus_strings1; n = %d; break;\n",
|
b.printf("case %spackageid: a = %sstatus_strings1; n = %d; break;\n",
|
||||||
g.pkgPrefix, g.pkgPrefix, len(g.statusList))
|
g.pkgPrefix, g.pkgPrefix, len(g.statusList))
|
||||||
// TODO: add cases for other packages used by this one.
|
// TODO: add cases for other packages used by this one.
|
||||||
@@ -479,10 +444,10 @@ func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("bad status message %q", raw)
|
return fmt.Errorf("bad status message %q", raw)
|
||||||
}
|
}
|
||||||
prefix := "suspension "
|
prefix := "SUSPENSION_"
|
||||||
isError := n.Keyword().Key() == t.KeyError
|
isError := n.Keyword().Key() == t.KeyError
|
||||||
if isError {
|
if isError {
|
||||||
prefix = "error "
|
prefix = "ERROR_"
|
||||||
}
|
}
|
||||||
s := status{
|
s := status{
|
||||||
name: strings.ToUpper(g.cName(prefix + msg)),
|
name: strings.ToUpper(g.cName(prefix + msg)),
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ package cgen
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/puffs/lang/builtin"
|
||||||
|
|
||||||
a "github.com/google/puffs/lang/ast"
|
a "github.com/google/puffs/lang/ast"
|
||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
@@ -123,12 +126,12 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
|||||||
if n.Keyword() != 0 {
|
if n.Keyword() != 0 {
|
||||||
ret = g.statusMap[n.Message()]
|
ret = g.statusMap[n.Message()]
|
||||||
if ret.name == "" {
|
if ret.name == "" {
|
||||||
msg := trimQuotes(n.Message().String(g.tm))
|
msg := builtin.TrimQuotes(n.Message().String(g.tm))
|
||||||
suffix := builtInStatusSuffixes[msg]
|
z := builtin.StatusMap[msg]
|
||||||
if suffix == "" {
|
if z.Message == "" {
|
||||||
return fmt.Errorf("no status code for %q", msg)
|
return fmt.Errorf("no status code for %q", msg)
|
||||||
}
|
}
|
||||||
ret.name = g.PKGPREFIX + suffix
|
ret.name = strings.ToUpper(g.cName(z.String()))
|
||||||
}
|
}
|
||||||
} else if retExpr = n.Value(); retExpr == nil {
|
} else if retExpr = n.Value(); retExpr == nil {
|
||||||
ret.name = g.PKGPREFIX + "STATUS_OK"
|
ret.name = g.PKGPREFIX + "STATUS_OK"
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
// Package builtin lists Puffs' built-in concepts such as status codes.
|
||||||
|
package builtin
|
||||||
|
|
||||||
|
import (
|
||||||
|
t "github.com/google/puffs/lang/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
Keyword t.ID
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (z Status) String() string {
|
||||||
|
prefix := "status "
|
||||||
|
switch z.Keyword {
|
||||||
|
case t.IDError:
|
||||||
|
prefix = "error "
|
||||||
|
case t.IDSuspension:
|
||||||
|
prefix = "suspension "
|
||||||
|
}
|
||||||
|
return prefix + z.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
var StatusList = [...]Status{
|
||||||
|
{0, "ok"},
|
||||||
|
{t.IDError, "bad puffs version"},
|
||||||
|
{t.IDError, "bad receiver"},
|
||||||
|
{t.IDError, "bad argument"},
|
||||||
|
{t.IDError, "initializer not called"},
|
||||||
|
{t.IDError, "closed for writes"},
|
||||||
|
{t.IDError, "unexpected EOF"}, // Used if reading when closed == true.
|
||||||
|
{t.IDSuspension, "short read"}, // Used if reading when closed == false.
|
||||||
|
{t.IDSuspension, "short write"},
|
||||||
|
{t.IDSuspension, "limited read"},
|
||||||
|
{t.IDSuspension, "limited write"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var StatusMap = map[string]Status{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
for _, s := range StatusList {
|
||||||
|
StatusMap[s.Message] = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TrimQuotes(s string) string {
|
||||||
|
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
||||||
|
return s[1 : len(s)-1]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
+2
-14
@@ -11,6 +11,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/google/puffs/lang/base38"
|
"github.com/google/puffs/lang/base38"
|
||||||
|
"github.com/google/puffs/lang/builtin"
|
||||||
|
|
||||||
a "github.com/google/puffs/lang/ast"
|
a "github.com/google/puffs/lang/ast"
|
||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
@@ -47,13 +48,6 @@ func (e *Error) Error() string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimQuotes(s string) string {
|
|
||||||
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
|
||||||
return s[1 : len(s)-1]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeExprFoo is an *ast.Node MType (implicit type).
|
// typeExprFoo is an *ast.Node MType (implicit type).
|
||||||
var (
|
var (
|
||||||
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||||
@@ -95,12 +89,6 @@ type Struct struct {
|
|||||||
Struct *a.Struct
|
Struct *a.Struct
|
||||||
}
|
}
|
||||||
|
|
||||||
var builtInStatuses = map[string]t.ID{
|
|
||||||
// TODO: fill in the rest of the built in statuses.
|
|
||||||
"short read": t.IDSuspension,
|
|
||||||
"short write": t.IDSuspension,
|
|
||||||
}
|
|
||||||
|
|
||||||
func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
|
func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
@@ -242,7 +230,7 @@ func (c *Checker) checkStatus(node *a.Node) error {
|
|||||||
id := n.Message()
|
id := n.Message()
|
||||||
if other, ok := c.statuses[id]; ok {
|
if other, ok := c.statuses[id]; ok {
|
||||||
return &Error{
|
return &Error{
|
||||||
Err: fmt.Errorf("check: duplicate status %q", trimQuotes(id.String(c.tm))),
|
Err: fmt.Errorf("check: duplicate status %q", builtin.TrimQuotes(id.String(c.tm))),
|
||||||
Filename: n.Filename(),
|
Filename: n.Filename(),
|
||||||
Line: n.Line(),
|
Line: n.Line(),
|
||||||
OtherFilename: other.Status.Filename(),
|
OtherFilename: other.Status.Filename(),
|
||||||
|
|||||||
+5
-2
@@ -7,6 +7,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/google/puffs/lang/builtin"
|
||||||
|
|
||||||
a "github.com/google/puffs/lang/ast"
|
a "github.com/google/puffs/lang/ast"
|
||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
)
|
)
|
||||||
@@ -124,11 +126,12 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
if s, ok := q.c.statuses[n.Message()]; ok {
|
if s, ok := q.c.statuses[n.Message()]; ok {
|
||||||
sk = s.Status.Keyword()
|
sk = s.Status.Keyword()
|
||||||
} else {
|
} else {
|
||||||
msg := trimQuotes(n.Message().String(q.tm))
|
msg := builtin.TrimQuotes(n.Message().String(q.tm))
|
||||||
sk, ok = builtInStatuses[msg]
|
z, ok := builtin.StatusMap[msg]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("check: no error or status with message %q", msg)
|
return fmt.Errorf("check: no error or status with message %q", msg)
|
||||||
}
|
}
|
||||||
|
sk = z.Keyword
|
||||||
}
|
}
|
||||||
if nk != sk {
|
if nk != sk {
|
||||||
return fmt.Errorf("check: return statement says %q but declaration says %q",
|
return fmt.Errorf("check: return statement says %q but declaration says %q",
|
||||||
|
|||||||
Reference in new issue
Block a user