Add a slice.suffix method.

This commit is contained in:
Nigel Tao committed 2017-09-22 14:12:54 +10:00
1 parent 51f2b183a2
commit 2fb9972bb9
10 files changed
+176 -2

No files matched your search

+1 -1
View File
@@ -689,7 +689,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return zero, bitMask(int(aMax.Int64())), nil
}
// TODO: delete this hack that only matches "foo.is_suspension(etc)".
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) {
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) || isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
return nil, nil, nil
}
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
+25
View File
@@ -410,6 +410,23 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprBool)
return nil
}
// TODO: delete this hack that only matches "foo.suffix(etc)".
if isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
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(), depth); err != nil {
return err
}
}
// TODO: don't assume that the slice is a slice of u8.
n.SetMType(typeExprSliceU8)
return nil
}
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
if isSetLiteralWidth(q.tm, n) {
foo := n.LHS().Expr().LHS().Expr()
@@ -597,6 +614,14 @@ func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool {
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
}
func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool {
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
}
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(lw:bar)".
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
+6
View File
@@ -271,6 +271,8 @@ const (
KeyIsSuspension = Key(IDIsSuspension >> KeyShift)
KeySlice = Key(IDSlice >> KeyShift)
KeyLength = Key(IDLength >> KeyShift)
KeyPrefix = Key(IDPrefix >> KeyShift)
KeySuffix = Key(IDSuffix >> KeyShift)
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
@@ -446,6 +448,8 @@ const (
IDIsSuspension = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDPrefix = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSuffix = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
)
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
@@ -643,6 +647,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyIsSuspension: {"is_suspension", IDIsSuspension},
KeySlice: {"slice", IDSlice},
KeyLength: {"length", IDLength},
KeyPrefix: {"prefix", IDPrefix},
KeySuffix: {"suffix", IDSuffix},
}
var builtInsByName = map[string]ID{}