mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-21 16:47:28 +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
@@ -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"
|
||||
|
||||
"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
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user