Add a ~+ operator.

This commit is contained in:
Nigel Tao committed 2017-10-19 16:50:05 +11:00
1 parent e7d01e7b2a
commit 6492c52156
8 files changed
+62 -7

No files matched your search

+2
View File
@@ -522,6 +522,7 @@ var cOpNames = [256]string{
t.KeyPipeEq: " |= ", t.KeyPipeEq: " |= ",
t.KeyHatEq: " ^= ", t.KeyHatEq: " ^= ",
t.KeyPercentEq: " %= ", t.KeyPercentEq: " %= ",
t.KeyTildePlusEq: " += ",
t.KeyXUnaryPlus: "+", t.KeyXUnaryPlus: "+",
t.KeyXUnaryMinus: "-", t.KeyXUnaryMinus: "-",
@@ -547,6 +548,7 @@ var cOpNames = [256]string{
t.KeyXBinaryAnd: " && ", t.KeyXBinaryAnd: " && ",
t.KeyXBinaryOr: " || ", t.KeyXBinaryOr: " || ",
t.KeyXBinaryAs: " no_such_as_C_operator ", t.KeyXBinaryAs: " no_such_as_C_operator ",
t.KeyXBinaryTildePlus: " + ",
t.KeyXAssociativePlus: " + ", t.KeyXAssociativePlus: " + ",
t.KeyXAssociativeStar: " * ", t.KeyXAssociativeStar: " * ",
+1 -1
View File
@@ -1493,7 +1493,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
} }
if (v_n_copied > 0) { if (v_n_copied > 0) {
v_hlen -= v_n_copied; v_hlen -= v_n_copied;
v_hdist = ((v_hdist + (v_n_copied & 32767)) & 32767); v_hdist = ((v_hdist + v_n_copied) & 32767);
if (v_hdist == 0) { if (v_hdist == 0) {
goto label_5_break; goto label_5_break;
} }
+5
View File
@@ -524,6 +524,11 @@ func (n *TypeExpr) IsRefined() bool {
return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil)
} }
func (n *TypeExpr) IsUnsignedInteger() bool {
return n.id0 == 0 && (n.id1.Key() == t.KeyU8 || n.id1.Key() == t.KeyU16 ||
n.id1.Key() == t.KeyU32 || n.id1.Key() == t.KeyU64) // TODO: t.KeyUsize?
}
func (n *TypeExpr) HasPointers() bool { func (n *TypeExpr) HasPointers() bool {
for ; n != nil; n = n.Inner() { for ; n != nil; n = n.Inner() {
switch n.id0.Key() { switch n.id0.Key() {
+1
View File
@@ -158,6 +158,7 @@ var opStrings = [256]string{
t.KeyXBinaryAnd: " and ", t.KeyXBinaryAnd: " and ",
t.KeyXBinaryOr: " or ", t.KeyXBinaryOr: " or ",
t.KeyXBinaryAs: " as ", t.KeyXBinaryAs: " as ",
t.KeyXBinaryTildePlus: " ~+ ",
t.KeyXAssociativePlus: " + ", t.KeyXAssociativePlus: " + ",
t.KeyXAssociativeStar: " * ", t.KeyXAssociativeStar: " * ",
+8
View File
@@ -997,6 +997,14 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
case t.KeyXBinaryAs: case t.KeyXBinaryAs:
// Unreachable, as this is checked by the caller. // Unreachable, as this is checked by the caller.
case t.KeyXBinaryTildePlus:
typ := lhs.MType()
if typ.IsIdeal() {
typ = rhs.MType()
}
b := numTypeBounds[typ.Name().Key()]
return b[0], b[1], nil
} }
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op)
} }
+29
View File
@@ -262,6 +262,11 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
} }
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type", return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm)) n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
case t.KeyTildePlusEq:
if !lTyp.IsUnsignedInteger() {
return fmt.Errorf("check: assignment %q: %q, of type %q, does not have unsigned integer type",
n.Operator().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
} }
if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) { if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) {
@@ -826,6 +831,28 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
} }
} }
switch op.Key() {
case t.KeyXBinaryTildePlus:
typ := lTyp
if typ.IsIdeal() {
typ = rTyp
if typ.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have non-ideal types",
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
}
if !typ.IsUnsignedInteger() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have unsigned integer types",
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
}
if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil { if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil {
ncv, err := evalConstValueBinaryOp(q.tm, n, lcv, rcv) ncv, err := evalConstValueBinaryOp(q.tm, n, lcv, rcv)
if err != nil { if err != nil {
@@ -901,6 +928,8 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.
return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil
case t.KeyXBinaryOr: case t.KeyXBinaryOr:
return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil
case t.KeyXBinaryTildePlus:
return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers")
} }
return nil, fmt.Errorf("check: unrecognized token.Key (0x%02X) for evalConstValueBinaryOp", n.ID0().Key()) return nil, fmt.Errorf("check: unrecognized token.Key (0x%02X) for evalConstValueBinaryOp", n.ID0().Key())
} }
+15 -4
View File
@@ -185,6 +185,7 @@ const (
KeyPipeEq = Key(IDPipeEq >> KeyShift) KeyPipeEq = Key(IDPipeEq >> KeyShift)
KeyHatEq = Key(IDHatEq >> KeyShift) KeyHatEq = Key(IDHatEq >> KeyShift)
KeyPercentEq = Key(IDPercentEq >> KeyShift) KeyPercentEq = Key(IDPercentEq >> KeyShift)
KeyTildePlusEq = Key(IDTildePlusEq >> KeyShift)
KeyPlus = Key(IDPlus >> KeyShift) KeyPlus = Key(IDPlus >> KeyShift)
KeyMinus = Key(IDMinus >> KeyShift) KeyMinus = Key(IDMinus >> KeyShift)
@@ -197,6 +198,7 @@ const (
KeyPipe = Key(IDPipe >> KeyShift) KeyPipe = Key(IDPipe >> KeyShift)
KeyHat = Key(IDHat >> KeyShift) KeyHat = Key(IDHat >> KeyShift)
KeyPercent = Key(IDPercent >> KeyShift) KeyPercent = Key(IDPercent >> KeyShift)
KeyTildePlus = Key(IDTildePlus >> KeyShift)
KeyNotEq = Key(IDNotEq >> KeyShift) KeyNotEq = Key(IDNotEq >> KeyShift)
KeyLessThan = Key(IDLessThan >> KeyShift) KeyLessThan = Key(IDLessThan >> KeyShift)
@@ -320,6 +322,7 @@ const (
KeyXBinaryAnd = Key(IDXBinaryAnd >> KeyShift) KeyXBinaryAnd = Key(IDXBinaryAnd >> KeyShift)
KeyXBinaryOr = Key(IDXBinaryOr >> KeyShift) KeyXBinaryOr = Key(IDXBinaryOr >> KeyShift)
KeyXBinaryAs = Key(IDXBinaryAs >> KeyShift) KeyXBinaryAs = Key(IDXBinaryAs >> KeyShift)
KeyXBinaryTildePlus = Key(IDXBinaryTildePlus >> KeyShift)
KeyXAssociativePlus = Key(IDXAssociativePlus >> KeyShift) KeyXAssociativePlus = Key(IDXAssociativePlus >> KeyShift)
KeyXAssociativeStar = Key(IDXAssociativeStar >> KeyShift) KeyXAssociativeStar = Key(IDXAssociativeStar >> KeyShift)
@@ -363,6 +366,7 @@ const (
IDPipeEq = ID(0x39<<KeyShift | FlagsAssign) IDPipeEq = ID(0x39<<KeyShift | FlagsAssign)
IDHatEq = ID(0x3A<<KeyShift | FlagsAssign) IDHatEq = ID(0x3A<<KeyShift | FlagsAssign)
IDPercentEq = ID(0x3B<<KeyShift | FlagsAssign) IDPercentEq = ID(0x3B<<KeyShift | FlagsAssign)
IDTildePlusEq = ID(0x3C<<KeyShift | FlagsAssign)
IDPlus = ID(0x41<<KeyShift | FlagsBinaryOp | FlagsUnaryOp | FlagsAssociativeOp) IDPlus = ID(0x41<<KeyShift | FlagsBinaryOp | FlagsUnaryOp | FlagsAssociativeOp)
IDMinus = ID(0x42<<KeyShift | FlagsBinaryOp | FlagsUnaryOp) IDMinus = ID(0x42<<KeyShift | FlagsBinaryOp | FlagsUnaryOp)
@@ -375,6 +379,7 @@ const (
IDPipe = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp) IDPipe = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDHat = ID(0x4A<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp) IDHat = ID(0x4A<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDPercent = ID(0x4B<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp) IDPercent = ID(0x4B<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDTildePlus = ID(0x4C<<KeyShift | FlagsBinaryOp) // TODO: FlagsAssociativeOp?
IDNotEq = ID(0x50<<KeyShift | FlagsBinaryOp) IDNotEq = ID(0x50<<KeyShift | FlagsBinaryOp)
IDLessThan = ID(0x51<<KeyShift | FlagsBinaryOp) IDLessThan = ID(0x51<<KeyShift | FlagsBinaryOp)
@@ -505,6 +510,7 @@ const (
IDXBinaryAnd = ID(0xE9<<KeyShift | FlagsBinaryOp) IDXBinaryAnd = ID(0xE9<<KeyShift | FlagsBinaryOp)
IDXBinaryOr = ID(0xEA<<KeyShift | FlagsBinaryOp) IDXBinaryOr = ID(0xEA<<KeyShift | FlagsBinaryOp)
IDXBinaryAs = ID(0xEB<<KeyShift | FlagsBinaryOp) IDXBinaryAs = ID(0xEB<<KeyShift | FlagsBinaryOp)
IDXBinaryTildePlus = ID(0xEC<<KeyShift | FlagsBinaryOp)
IDXAssociativePlus = ID(0xF0<<KeyShift | FlagsAssociativeOp) IDXAssociativePlus = ID(0xF0<<KeyShift | FlagsAssociativeOp)
IDXAssociativeStar = ID(0xF1<<KeyShift | FlagsAssociativeOp) IDXAssociativeStar = ID(0xF1<<KeyShift | FlagsAssociativeOp)
@@ -558,6 +564,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyPipeEq: {"|=", IDPipeEq}, KeyPipeEq: {"|=", IDPipeEq},
KeyHatEq: {"^=", IDHatEq}, KeyHatEq: {"^=", IDHatEq},
KeyPercentEq: {"%=", IDPercentEq}, KeyPercentEq: {"%=", IDPercentEq},
KeyTildePlusEq: {"~+=", IDTildePlusEq},
KeyPlus: {"+", IDPlus}, KeyPlus: {"+", IDPlus},
KeyMinus: {"-", IDMinus}, KeyMinus: {"-", IDMinus},
@@ -570,10 +577,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyPipe: {"|", IDPipe}, KeyPipe: {"|", IDPipe},
KeyHat: {"^", IDHat}, KeyHat: {"^", IDHat},
KeyPercent: {"%", IDPercent}, KeyPercent: {"%", IDPercent},
// TODO: tilde-ops such as ~+ for ignoring overflow. Swift defines overflow KeyTildePlus: {"~+", IDPercent},
// ops for +, - and *. Swift syntax uses a leading &. Puffs uses a leading
// ~ as it is less ambiguous with the &^ operator (and any potential use of
// & as address-of) and ~ is otherwise unused.
KeyNotEq: {"!=", IDNotEq}, KeyNotEq: {"!=", IDNotEq},
KeyLessThan: {"<", IDLessThan}, KeyLessThan: {"<", IDLessThan},
@@ -770,6 +774,10 @@ var lexers = [256][]suffixLexer{
{"=", IDGreaterEq}, {"=", IDGreaterEq},
{"", IDGreaterThan}, {"", IDGreaterThan},
}, },
'~': {
{"+=", IDTildePlusEq},
{"+", IDTildePlus},
},
} }
var ambiguousForms = [256]ID{ var ambiguousForms = [256]ID{
@@ -797,6 +805,7 @@ var ambiguousForms = [256]ID{
KeyXBinaryAnd: IDAnd, KeyXBinaryAnd: IDAnd,
KeyXBinaryOr: IDOr, KeyXBinaryOr: IDOr,
KeyXBinaryAs: IDAs, KeyXBinaryAs: IDAs,
KeyXBinaryTildePlus: IDTildePlus,
KeyXAssociativePlus: IDPlus, KeyXAssociativePlus: IDPlus,
KeyXAssociativeStar: IDStar, KeyXAssociativeStar: IDStar,
@@ -825,6 +834,7 @@ var binaryForms = [256]ID{
KeyPipeEq: IDXBinaryPipe, KeyPipeEq: IDXBinaryPipe,
KeyHatEq: IDXBinaryHat, KeyHatEq: IDXBinaryHat,
KeyPercentEq: IDXBinaryPercent, KeyPercentEq: IDXBinaryPercent,
KeyTildePlusEq: IDXBinaryTildePlus,
KeyPlus: IDXBinaryPlus, KeyPlus: IDXBinaryPlus,
KeyMinus: IDXBinaryMinus, KeyMinus: IDXBinaryMinus,
@@ -846,6 +856,7 @@ var binaryForms = [256]ID{
KeyAnd: IDXBinaryAnd, KeyAnd: IDXBinaryAnd,
KeyOr: IDXBinaryOr, KeyOr: IDXBinaryOr,
KeyAs: IDXBinaryAs, KeyAs: IDXBinaryAs,
KeyTildePlus: IDXBinaryTildePlus,
} }
var associativeForms = [256]ID{ var associativeForms = [256]ID{
+1 -2
View File
@@ -390,8 +390,7 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() {
} }
if n_copied > 0 { if n_copied > 0 {
hlen -= n_copied hlen -= n_copied
// TODO: a "~+" tilde-operator that ignores overflow. hdist = (hdist ~+ n_copied) & 0x7FFF
hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF
if hdist == 0 { if hdist == 0 {
// Wrap around the this.history ringbuffer. // Wrap around the this.history ringbuffer.
break break