Add a lang/builtin package.

This commit is contained in:
Nigel Tao committed 2017-10-07 11:37:10 +11:00
1 parent 010c4db54f
commit 2efdf5fab6
5 files changed
+80 -67

No files matched your search

+12 -47
View File
@@ -14,6 +14,7 @@ import (
"strings"
"github.com/google/puffs/lang/base38"
"github.com/google/puffs/lang/builtin"
"github.com/google/puffs/lang/check"
"github.com/google/puffs/lang/generate"
@@ -83,43 +84,16 @@ const (
"// - 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() {
// The +1 is for the error bit (the sign bit).
if statusCodeNamespaceShift+base38.MaxBits+1 != 32 {
panic("inconsistent status code namespace shift")
}
if len(builtInStatuses) > maxNamespacedStatusCode {
panic("too many builtInStatuses")
if len(builtin.StatusList) > maxNamespacedStatusCode {
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
const (
@@ -241,13 +215,13 @@ func (g *gen) genHeader(b *buffer) error {
pkgID := g.checker.PackageID()
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)
if strings.HasPrefix(s, "error ") {
if z.Keyword == t.IDError {
code |= 1 << 31
}
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")
@@ -305,17 +279,9 @@ func (g *gen) genImpl(b *buffer) error {
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("const char* %sstatus_strings0[%d] = {\n", g.pkgPrefix, len(builtInStatuses))
for _, s := range builtInStatuses {
if strings.HasPrefix(s, "status ") {
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.printf("const char* %sstatus_strings0[%d] = {\n", g.pkgPrefix, len(builtin.StatusList))
for _, z := range builtin.StatusList {
b.printf("%q,", g.pkgName+": "+z.Message)
}
b.writes("};\n\n")
@@ -329,8 +295,7 @@ func (g *gen) genImpl(b *buffer) error {
b.printf("const char** a = NULL;\n")
b.printf("uint32_t n = 0;\n")
b.printf("switch ((s >> %d) & %#x) {\n", statusCodeNamespaceShift, statusCodeNamespaceMask)
b.printf("case 0: a = %sstatus_strings0; n = %d; break;\n",
g.pkgPrefix, len(builtInStatuses))
b.printf("case 0: a = %sstatus_strings0; n = %d; break;\n", g.pkgPrefix, len(builtin.StatusList))
b.printf("case %spackageid: a = %sstatus_strings1; n = %d; break;\n",
g.pkgPrefix, g.pkgPrefix, len(g.statusList))
// 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 {
return fmt.Errorf("bad status message %q", raw)
}
prefix := "suspension "
prefix := "SUSPENSION_"
isError := n.Keyword().Key() == t.KeyError
if isError {
prefix = "error "
prefix = "ERROR_"
}
s := status{
name: strings.ToUpper(g.cName(prefix + msg)),
+7 -4
View File
@@ -5,6 +5,9 @@ package cgen
import (
"fmt"
"strings"
"github.com/google/puffs/lang/builtin"
a "github.com/google/puffs/lang/ast"
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 {
ret = g.statusMap[n.Message()]
if ret.name == "" {
msg := trimQuotes(n.Message().String(g.tm))
suffix := builtInStatusSuffixes[msg]
if suffix == "" {
msg := builtin.TrimQuotes(n.Message().String(g.tm))
z := builtin.StatusMap[msg]
if z.Message == "" {
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 {
ret.name = g.PKGPREFIX + "STATUS_OK"
+54
View File
@@ -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
View File
@@ -11,6 +11,7 @@ import (
"math/big"
"github.com/google/puffs/lang/base38"
"github.com/google/puffs/lang/builtin"
a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token"
@@ -47,13 +48,6 @@ func (e *Error) Error() string {
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).
var (
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
@@ -95,12 +89,6 @@ type Struct 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) {
for _, f := range files {
if f == nil {
@@ -242,7 +230,7 @@ func (c *Checker) checkStatus(node *a.Node) error {
id := n.Message()
if other, ok := c.statuses[id]; ok {
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(),
Line: n.Line(),
OtherFilename: other.Status.Filename(),
+5 -2
View File
@@ -7,6 +7,8 @@ import (
"fmt"
"math/big"
"github.com/google/puffs/lang/builtin"
a "github.com/google/puffs/lang/ast"
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 {
sk = s.Status.Keyword()
} else {
msg := trimQuotes(n.Message().String(q.tm))
sk, ok = builtInStatuses[msg]
msg := builtin.TrimQuotes(n.Message().String(q.tm))
z, ok := builtin.StatusMap[msg]
if !ok {
return fmt.Errorf("check: no error or status with message %q", msg)
}
sk = z.Keyword
}
if nk != sk {
return fmt.Errorf("check: return statement says %q but declaration says %q",