Add a slice.length method.

This commit is contained in:
Nigel Tao committed 2017-09-22 12:37:07 +10:00
1 parent fda696bf95
commit 51f2b183a2
7 files changed
+57 -6

No files matched your search

+3
View File
@@ -715,6 +715,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
}
return nil, nil, nil
}
if isLength(q.tm, n) {
break
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
case t.KeyOpenBracket:
+24
View File
@@ -442,6 +442,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(typeExprPlaceholder) // HACK.
return nil
}
// TODO: delete this hack that only matches "foo.length(etc)".
if isLength(q.tm, n) {
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
}
}
n.SetMType(typeExprU64)
return nil
}
case t.KeyOpenBracket:
// n is an index.
@@ -599,6 +615,14 @@ func isDecode(tm *t.Map, n *a.Expr) bool {
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
}
func isLength(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() || len(n.Args()) != 0 {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLength
}
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr()
if err := q.tcheckExpr(lhs, depth); err != nil {
+3
View File
@@ -270,6 +270,7 @@ const (
KeyIsOK = Key(IDIsOK >> KeyShift)
KeyIsSuspension = Key(IDIsSuspension >> KeyShift)
KeySlice = Key(IDSlice >> KeyShift)
KeyLength = Key(IDLength >> KeyShift)
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
@@ -444,6 +445,7 @@ const (
IDIsOK = ID(0xC6<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDIsSuspension = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
)
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
@@ -640,6 +642,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyIsOK: {"is_ok", IDIsOK},
KeyIsSuspension: {"is_suspension", IDIsSuspension},
KeySlice: {"slice", IDSlice},
KeyLength: {"length", IDLength},
}
var builtInsByName = map[string]ID{}