Generate C code for "this.lzw.set_literal_width".

This commit is contained in:
Nigel Tao committed 2017-06-22 14:56:29 +10:00
1 parent 93ca2f7496
commit 497a8776c2
5 files changed
+64 -6

No files matched your search

+12
View File
@@ -651,6 +651,18 @@ 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.set_literal_width(etc)".
if isSetLiteralWidth(q.tm, n) {
a := n.Args()[0].Arg().Value()
aMin, aMax, err := q.bcheckExpr(a, depth)
if err != nil {
return nil, nil, err
}
if aMin.Cmp(big.NewInt(2)) < 0 || aMax.Cmp(big.NewInt(8)) > 0 {
return nil, nil, fmt.Errorf("check: %q not in range [2..8]", a.String(q.tm))
}
return nil, nil, nil
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
case t.KeyOpenBracket:
+25
View File
@@ -359,6 +359,22 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetMType(foo.MType().Unrefined())
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()
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(typeExprPlaceholder) // HACK.
return nil
}
case t.KeyOpenBracket:
// n is an index.
@@ -448,6 +464,15 @@ func isLowBits(tm *t.Map, n *a.Expr) bool {
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLowBits
}
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 {
return false
}
n = n.LHS().Expr()
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width")
}
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lhs := n.LHS().Expr()
if err := q.tcheckExpr(lhs, depth); err != nil {