Type check built-in functions more thoroughly

This commit is contained in:
Nigel Tao committed 2017-12-16 13:25:26 +11:00
1 parent 4877279ee7
commit d14b56ea39
15 files changed
+291 -163

No files matched your search

+1 -1
View File
@@ -125,7 +125,7 @@ func do(r io.Reader, filename string) error {
}
// We don't need the AST node to pretty-print, but it's worth rejecting
// syntax errors early. This is just a parse, not a full type check.
if _, err := parse.Parse(tm, filename, tokens); err != nil {
if _, err := parse.Parse(tm, filename, tokens, nil); err != nil {
return err
}
buf := &bytes.Buffer{}
+4 -4
View File
@@ -1981,7 +1981,7 @@ label_0_continue:;
v_bits >>= v_table_entry_n_bits;
v_n_bits -= v_table_entry_n_bits;
if ((v_table_entry >> 31) != 0) {
*b_wptr_dst++ = ((v_table_entry >> 8) & 255);
*b_wptr_dst++ = ((uint8_t)(((v_table_entry >> 8) & 255)));
goto label_0_continue;
} else if ((v_table_entry >> 30) != 0) {
} else if ((v_table_entry >> 29) != 0) {
@@ -2014,7 +2014,7 @@ label_0_continue:;
v_bits >>= v_table_entry_n_bits;
v_n_bits -= v_table_entry_n_bits;
if ((v_table_entry >> 31) != 0) {
*b_wptr_dst++ = ((v_table_entry >> 8) & 255);
*b_wptr_dst++ = ((uint8_t)(((v_table_entry >> 8) & 255)));
goto label_0_continue;
} else if ((v_table_entry >> 30) != 0) {
} else if ((v_table_entry >> 29) != 0) {
@@ -2388,7 +2388,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow(
status = WUFFS_DEFLATE__SUSPENSION_SHORT_WRITE;
goto suspend;
}
*b_wptr_dst++ = ((v_table_entry >> 8) & 255);
*b_wptr_dst++ = ((uint8_t)(((v_table_entry >> 8) & 255)));
goto label_0_continue;
} else if ((v_table_entry >> 30) != 0) {
} else if ((v_table_entry >> 29) != 0) {
@@ -2429,7 +2429,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow(
status = WUFFS_DEFLATE__SUSPENSION_SHORT_WRITE;
goto suspend;
}
*b_wptr_dst++ = ((v_table_entry >> 8) & 255);
*b_wptr_dst++ = ((uint8_t)(((v_table_entry >> 8) & 255)));
goto label_0_continue;
} else if ((v_table_entry >> 30) != 0) {
} else if ((v_table_entry >> 29) != 0) {
+1 -1
View File
@@ -77,7 +77,7 @@ func TestString(t *testing.T) {
t.Errorf("Tokenize(%q): %v", tc, err)
continue
}
expr, err := parse.ParseExpr(tm, filename, tokens)
expr, err := parse.ParseExpr(tm, filename, tokens, nil)
if err != nil {
t.Errorf("ParseExpr(%q): %v", tc, err)
continue
+54
View File
@@ -60,3 +60,57 @@ func init() {
// TODO: a collection of forbidden variable names like and, or, not, as, ref,
// deref, false, true, in, out, this, u8, u16, etc?
var Types = []string{
// TODO: i8, i16, i32, i64.
"u8",
"u16",
"u32",
"u64",
"bool",
"status",
"reader1",
"writer1",
}
var Funcs = []string{
// TODO: some methods like "mark" should probably have a trailing "!".
//
// TODO: status methods (is_error, etc), [] u8 methods (length, etc), u32
// methods (low_bits, etc).
"reader1.read_u8?()(ret u8)",
"reader1.read_u16be?()(ret u16)",
"reader1.read_u16le?()(ret u16)",
"reader1.read_u32be?()(ret u32)",
"reader1.read_u32le?()(ret u32)",
"reader1.read_u64be?()(ret u64)",
"reader1.read_u64le?()(ret u64)",
"reader1.available()(ret u64)",
"reader1.is_marked()(ret bool)",
"reader1.limit(l u64)(ret reader1)",
"reader1.mark()()",
"reader1.since_mark()(ret[] u8)",
"reader1.skip32?(n u32)()",
"reader1.skip64?(n u64)()",
"reader1.unread_u8?()()",
"writer1.write_u8?(x u8)()",
"writer1.write_u16be?(x u16)()",
"writer1.write_u16le?(x u16)()",
"writer1.write_u32be?(x u32)()",
"writer1.write_u32le?(x u32)()",
"writer1.write_u64be?(x u64)()",
"writer1.write_u64le?(x u64)()",
"writer1.available()(ret u64)",
"writer1.copy_from_history32(distance u32, length u32)(ret u32)",
"writer1.copy_from_reader32(r reader1, length u32)(ret u32)",
"writer1.copy_from_slice(s[] u8)(ret u64)",
"writer1.copy_from_slice32(s[] u8, length u32)(ret u32)",
"writer1.is_marked()(ret bool)",
"writer1.limit(l u64)(ret writer1)",
"writer1.mark()()",
"writer1.since_mark()(ret[] u8)",
}
+1 -1
View File
@@ -961,7 +961,7 @@ func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
//
// TODO: bcheck the receiver, e.g. ptr vs nptr.
lhs := n.LHS().Expr()
f, err := q.resolveFunc(lhs.MType())
f, err := q.c.resolveFunc(lhs.MType())
if err != nil {
return err
}
+1 -26
View File
@@ -58,32 +58,6 @@ func (e *Error) Error() string {
return string(b)
}
var (
exprIn = a.NewExpr(a.FlagsTypeChecked, 0, t.IDIn, nil, nil, nil, nil)
exprOut = a.NewExpr(a.FlagsTypeChecked, 0, t.IDOut, nil, nil, nil, nil)
exprThis = a.NewExpr(a.FlagsTypeChecked, 0, t.IDThis, nil, nil, nil, nil)
)
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8)
// TODO: delete this.
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
)
// TypeMap maps from variable names (as token IDs) to types.
type TypeMap map[t.ID]*a.TypeExpr
type Const struct {
ID t.ID // ID of the const name.
Const *a.Const
@@ -194,6 +168,7 @@ type Checker struct {
statuses map[t.ID]Status
structs map[t.ID]Struct
builtInFuncs map[t.QID]*a.Func
unsortedStructs []*a.Struct
}
+19 -2
View File
@@ -24,6 +24,7 @@ import (
"testing"
"github.com/google/wuffs/lang/ast"
"github.com/google/wuffs/lang/builtin"
"github.com/google/wuffs/lang/parse"
"github.com/google/wuffs/lang/render"
"github.com/google/wuffs/lang/token"
@@ -99,7 +100,7 @@ func TestCheck(t *testing.T) {
t.Fatalf("Tokenize: %v", err)
}
file, err := parse.Parse(tm, filename, tokens)
file, err := parse.Parse(tm, filename, tokens, nil)
if err != nil {
t.Fatalf("Parse: %v", err)
}
@@ -199,7 +200,7 @@ func TestConstValues(t *testing.T) {
continue
}
file, err := parse.Parse(tm, filename, tokens)
file, err := parse.Parse(tm, filename, tokens, nil)
if err != nil {
t.Errorf("%q: Parse: %v", s, err)
continue
@@ -277,3 +278,19 @@ func TestBitMask(t *testing.T) {
}
}
}
func TestBuiltInTypeMap(t *testing.T) {
if got, want := len(builtInTypeMap), len(builtin.Types); got != want {
t.Fatalf("lengths: got %d, want %d", got, want)
}
tm := token.Map{}
for _, s := range builtin.Types {
id := tm.ByName(s)
if id == 0 {
t.Fatalf("ID(%q): got 0, want non-0", s)
}
if _, ok := builtInTypeMap[id]; !ok {
t.Fatalf("no builtInTypeMap entry for %q", s)
}
}
}
+121
View File
@@ -0,0 +1,121 @@
// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate go run gen.go
package check
import (
"fmt"
"github.com/google/wuffs/lang/builtin"
"github.com/google/wuffs/lang/parse"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
var (
exprIn = a.NewExpr(a.FlagsTypeChecked, 0, t.IDIn, nil, nil, nil, nil)
exprOut = a.NewExpr(a.FlagsTypeChecked, 0, t.IDOut, nil, nil, nil, nil)
exprThis = a.NewExpr(a.FlagsTypeChecked, 0, t.IDThis, nil, nil, nil, nil)
)
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprU16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
typeExprReader1 = a.NewTypeExpr(0, t.IDReader1, nil, nil, nil)
typeExprWriter1 = a.NewTypeExpr(0, t.IDWriter1, nil, nil, nil)
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8)
// TODO: delete this.
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
)
// TypeMap maps from variable names (as token IDs) to types.
type TypeMap map[t.ID]*a.TypeExpr
var builtInTypeMap = TypeMap{
t.IDU8: typeExprU8,
t.IDU16: typeExprU16,
t.IDU32: typeExprU32,
t.IDU64: typeExprU64,
t.IDBool: typeExprBool,
t.IDStatus: typeExprStatus,
t.IDReader1: typeExprReader1,
t.IDWriter1: typeExprWriter1,
}
func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
if c.builtInFuncs == nil {
c.builtInFuncs = map[t.QID]*a.Func{}
buf := []byte(nil)
opts := parse.Options{
AllowBuiltIns: true,
}
for _, s := range builtin.Funcs {
buf = buf[:0]
buf = append(buf, "pub func "...)
buf = append(buf, s...)
buf = append(buf, "{}\n"...)
const filename = "builtin.wuffs"
tokens, _, err := t.Tokenize(c.tm, filename, buf)
if err != nil {
return nil, err
}
file, err := parse.Parse(c.tm, filename, tokens, &opts)
if err != nil {
return nil, err
}
tlds := file.TopLevelDecls()
if len(tlds) != 1 || tlds[0].Kind() != a.KFunc {
return nil, fmt.Errorf("parsing %c: got %d top level decls, want %d", s, len(tlds), 1)
}
f := tlds[0].Func()
c.builtInFuncs[f.QID()] = f
}
}
return c.builtInFuncs[qid], nil
}
func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
// TODO: look up funcs in used packages.
if typ.Decorator().Key() != t.KeyOpenParen {
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
}
qid := t.QID{typ.Receiver().Name(), typ.Name()}
if f, err := c.builtInFunc(qid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
if f := c.funcs[qid].Func; f != nil {
return f, nil
}
return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm))
}
+60 -109
View File
@@ -425,47 +425,6 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// n.LHS().Expr().LHS().Expr(). Doing this properly will probably
// require a TypeExpr being able to express function and method types.
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrc(q.tm, n, t.KeyReadU8, 0) || isInSrc(q.tm, n, t.KeyUnreadU8, 0) ||
isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) ||
isInSrc(q.tm, n, t.KeySkip32, 1) || isInSrc(q.tm, n, t.KeySinceMark, 0) ||
isInSrc(q.tm, n, t.KeyMark, 0) || isInSrc(q.tm, n, t.KeyLimit, 1) ||
isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) ||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) ||
isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) ||
isThatMethod(q.tm, n, t.KeyMark, 0) || isThatMethod(q.tm, n, t.KeyLimit, 1) ||
isThatMethod(q.tm, n, t.KeySinceMark, 0) {
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
return err
}
for _, o := range n.Args() {
if err := q.tcheckArg(o.Arg(), nil, depth); err != nil {
return err
}
}
if n.ID0().Key() == t.KeyTry {
n.SetMType(typeExprStatus)
} else if isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) {
n.SetMType(typeExprPlaceholder32) // HACK.
} else if isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU16LE, 0) {
n.SetMType(typeExprPlaceholder16) // HACK.
} else if isInSrc(q.tm, n, t.KeySinceMark, 0) || isInDst(q.tm, n, t.KeySinceMark, 0) {
n.SetMType(typeExprSliceU8) // HACK.
} else if isInDst(q.tm, n, t.KeyCopyFromSlice, 1) {
n.SetMType(typeExprU64) // HACK.
} else if isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) || isInDst(q.tm, n, t.KeyCopyFromReader32, 2) ||
isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) {
n.SetMType(typeExprU32) // HACK.
} else if isInDst(q.tm, n, t.KeyIsMarked, 0) {
n.SetMType(typeExprBool) // HACK.
} else {
n.SetMType(typeExprPlaceholder) // HACK.
}
return nil
}
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
if isThatMethod(q.tm, n, t.KeyLowBits, 1) || isThatMethod(q.tm, n, t.KeyHighBits, 1) {
foo := n.LHS().Expr().LHS().Expr()
@@ -517,9 +476,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprSliceU8)
return nil
}
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) ||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
// TODO: delete this hack that only matches "foo.decode(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
@@ -557,22 +515,6 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprU64)
return nil
}
// TODO: delete this hack that only matches "foo.update(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("update").Key(), 1) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
return err
}
n.LHS().SetTypeChecked()
n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK.
for _, o := range n.Args() {
if err := q.tcheckArg(o.Arg(), nil, depth); err != nil {
return err
}
}
n.SetMType(typeExprU32)
return nil
}
return q.tcheckExprCall(n, depth)
@@ -677,21 +619,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.ID0().Key(), n.Str(q.tm))
}
func (q *checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
// TODO: look up funcs in used packages.
f := q.c.funcs[t.QID{typ.Receiver().Name(), typ.Name()}]
if f.Func == nil {
return nil, fmt.Errorf("check: cannot look up %q", typ.Str(q.tm))
}
return f.Func, nil
}
func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr()
if err := q.tcheckExpr(lhs, depth); err != nil {
return err
}
f, err := q.resolveFunc(lhs.MType())
f, err := q.c.resolveFunc(lhs.MType())
if err != nil {
return err
}
@@ -717,9 +650,18 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
if n.ID0().Key() == t.KeyTry {
n.SetMType(typeExprStatus)
} else {
// TODO: figure out how to translate f.Out(), which is an *ast.Struct,
// to an *ast.TypeExpr we can pass to n.SetMType.
n.SetMType(typeExprPlaceholder) // HACK.
outFields := f.Out().Fields()
switch len(outFields) {
default:
// TODO: figure out how to translate f.Out(), which is an
// *ast.Struct, to an *ast.TypeExpr we can pass to n.SetMType.
return fmt.Errorf("TODO: translate an *ast.Struct to an *ast.TypeExpr")
case 0:
// TODO: use a "unit", "void" or "empty struct" type.
n.SetMType(typeExprPlaceholder)
case 1:
n.SetMType(outFields[0].Field().XType())
}
}
return nil
}
@@ -777,7 +719,15 @@ func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != methodName {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() == t.KeyDot &&
(n.ID1() == tm.ByName("src") || n.ID1() == tm.ByName("dst")) {
return false
}
return true
}
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
@@ -792,44 +742,46 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: unsupported decorator for tcheckDot")
}
s := (*a.Struct)(nil)
if q.f.Func != nil {
switch name := lTyp.Name(); name.Key() {
case t.KeyIn:
s = q.f.Func.In()
case t.KeyOut:
s = q.f.Func.Out()
case t.KeyReader1, t.KeyWriter1:
// TODO: remove this hack and be more principled about the built-in
// buf1, reader1, writer1 types.
//
// Another hack is using typeExprPlaceholder until a TypeExpr can
// represent function types.
n.SetMType(typeExprPlaceholder)
return nil
default:
s = q.c.structs[name].Struct
}
qid := t.QID{lTyp.Name(), n.ID1()}
f, err := q.c.builtInFunc(qid)
if err != nil {
return err
} else if f == nil {
f = q.c.funcs[qid].Func
}
if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q",
lTyp.Name().Str(q.tm), lhs.Str(q.tm))
}
for _, field := range s.Fields() {
f := field.Field()
if f.Name() == n.ID1() {
n.SetMType(f.XType())
return nil
}
}
if f := q.c.funcs[t.QID{lTyp.Name(), n.ID1()}]; f.Func != nil {
if f != nil {
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
return nil
}
return fmt.Errorf("check: no field or method named %q found in struct type %q for expression %q",
s := (*a.Struct)(nil)
if q.f.Func != nil {
switch lTyp.Name().Key() {
case t.KeyIn:
s = q.f.Func.In()
case t.KeyOut:
s = q.f.Func.Out()
}
}
if s == nil {
name := lTyp.Name()
s = q.c.structs[name].Struct
if s == nil && builtInTypeMap[name] == nil {
return fmt.Errorf("check: no struct type %q found for expression %q", name.Str(q.tm), lhs.Str(q.tm))
}
}
if s != nil {
for _, field := range s.Fields() {
f := field.Field()
if f.Name() == n.ID1() {
n.SetMType(f.XType())
return nil
}
}
}
return fmt.Errorf("check: no field or method named %q found in type %q for expression %q",
n.ID1().Str(q.tm), lTyp.Name().Str(q.tm), n.Str(q.tm))
}
@@ -1132,8 +1084,7 @@ swtch:
if typ.Min() != nil || typ.Max() != nil {
// TODO: reject. You can only refine numeric types.
}
switch typ.Name().Key() {
case t.KeyBool, t.KeyStatus, t.KeyReader1, t.KeyWriter1:
if _, ok := builtInTypeMap[typ.Name()]; ok {
break swtch
}
for _, s := range q.c.structs {
+2 -2
View File
@@ -93,7 +93,7 @@ func parseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error
if err != nil {
return nil, err
}
f, err := parse.Parse(tm, filename, tokens)
f, err := parse.Parse(tm, filename, tokens, nil)
if err != nil {
return nil, err
}
@@ -112,7 +112,7 @@ func ParseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error
if err != nil {
return nil, err
}
f, err := parse.Parse(tm, filename, tokens)
f, err := parse.Parse(tm, filename, tokens, nil)
if err != nil {
return nil, err
}
+19 -8
View File
@@ -25,34 +25,45 @@ import (
t "github.com/google/wuffs/lang/token"
)
func Parse(tm *t.Map, filename string, src []t.Token) (*a.File, error) {
type Options struct {
AllowBuiltIns bool
}
func Parse(tm *t.Map, filename string, src []t.Token, opts *Options) (*a.File, error) {
p := &parser{
src: src,
tm: tm,
filename: filename,
src: src,
}
if len(src) > 0 {
p.lastLine = src[len(src)-1].Line
}
if opts != nil {
p.opts = *opts
}
return p.parseFile()
}
func ParseExpr(tm *t.Map, filename string, src []t.Token) (*a.Expr, error) {
func ParseExpr(tm *t.Map, filename string, src []t.Token, opts *Options) (*a.Expr, error) {
p := &parser{
src: src,
tm: tm,
filename: filename,
src: src,
}
if len(src) > 0 {
p.lastLine = src[len(src)-1].Line
}
if opts != nil {
p.opts = *opts
}
return p.parseExpr()
}
type parser struct {
src []t.Token
tm *t.Map
filename string
src []t.Token
opts Options
lastLine uint32
}
@@ -160,11 +171,11 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
if err != nil {
return nil, err
}
if id0 != 0 && id0.IsBuiltIn() {
if id0 != 0 && id0.IsBuiltIn() && !p.opts.AllowBuiltIns {
return nil, fmt.Errorf(`parse: built-in %q used for func receiver at %s:%d`,
p.tm.ByID(id0), p.filename, p.line())
}
if id1.IsBuiltIn() {
if id1.IsBuiltIn() && !p.opts.AllowBuiltIns {
return nil, fmt.Errorf(`parse: built-in %q used for func name at %s:%d`,
p.tm.ByID(id1), p.filename, p.line())
}
@@ -230,7 +241,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
if err != nil {
return nil, err
}
if name.IsBuiltIn() {
if name.IsBuiltIn() && !p.opts.AllowBuiltIns {
return nil, fmt.Errorf(`parse: built-in %q used for struct name at %s:%d`,
p.tm.ByID(name), p.filename, p.line())
}
+3 -3
View File
@@ -262,12 +262,12 @@ const (
KeyU16 = Key(IDU16 >> KeyShift)
KeyU32 = Key(IDU32 >> KeyShift)
KeyU64 = Key(IDU64 >> KeyShift)
KeyUsize = Key(IDUsize >> KeyShift)
KeyUsize = Key(IDUsize >> KeyShift) // TODO: unused?
KeyBool = Key(IDBool >> KeyShift)
KeyBuf1 = Key(IDBuf1 >> KeyShift)
KeyBuf1 = Key(IDBuf1 >> KeyShift) // TODO: unused?
KeyReader1 = Key(IDReader1 >> KeyShift)
KeyWriter1 = Key(IDWriter1 >> KeyShift)
KeyBuf2 = Key(IDBuf2 >> KeyShift)
KeyBuf2 = Key(IDBuf2 >> KeyShift) // TODO: unused?
KeyStatus = Key(IDStatus >> KeyShift)
KeyMark = Key(IDMark >> KeyShift)
+2 -2
View File
@@ -92,7 +92,7 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -131,7 +131,7 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
+2 -2
View File
@@ -47,7 +47,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
@@ -77,7 +77,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() {
if (table_entry >> 31) != 0 {
// Literal.
in.dst.write_u8?(x:(table_entry >> 8) & 0xFF)
in.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as u8)
continue:loop
} else if (table_entry >> 30) != 0 {
// No-op; code continues past the if-else chain.
+1 -2
View File
@@ -111,8 +111,7 @@ pri func lzw_decoder.decode?(dst writer1, src reader1)() {
inv c < 256,
{
var expansion[] u8 = this.stack[s:]
// TODO: "x:" instead of "s:" should be a compiler error.
var n_copied u64 = in.dst.copy_from_slice(x:expansion)
var n_copied u64 = in.dst.copy_from_slice(s:expansion)
if n_copied == expansion.length() {
break
}