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
+154 -99

No files matched your search

+14 -12
View File
@@ -510,18 +510,19 @@ var cTypeNames = [...]string{
}
var cOpNames = [256]string{
t.KeyEq: " = ",
t.KeyPlusEq: " += ",
t.KeyMinusEq: " -= ",
t.KeyStarEq: " *= ",
t.KeySlashEq: " /= ",
t.KeyShiftLEq: " <<= ",
t.KeyShiftREq: " >>= ",
t.KeyAmpEq: " &= ",
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
t.KeyPipeEq: " |= ",
t.KeyHatEq: " ^= ",
t.KeyPercentEq: " %= ",
t.KeyEq: " = ",
t.KeyPlusEq: " += ",
t.KeyMinusEq: " -= ",
t.KeyStarEq: " *= ",
t.KeySlashEq: " /= ",
t.KeyShiftLEq: " <<= ",
t.KeyShiftREq: " >>= ",
t.KeyAmpEq: " &= ",
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
t.KeyPipeEq: " |= ",
t.KeyHatEq: " ^= ",
t.KeyPercentEq: " %= ",
t.KeyTildePlusEq: " += ",
t.KeyXUnaryPlus: "+",
t.KeyXUnaryMinus: "-",
@@ -547,6 +548,7 @@ var cOpNames = [256]string{
t.KeyXBinaryAnd: " && ",
t.KeyXBinaryOr: " || ",
t.KeyXBinaryAs: " no_such_as_C_operator ",
t.KeyXBinaryTildePlus: " + ",
t.KeyXAssociativePlus: " + ",
t.KeyXAssociativeStar: " * ",
+1 -1
View File
@@ -1493,7 +1493,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
}
if (v_n_copied > 0) {
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) {
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)
}
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 {
for ; n != nil; n = n.Inner() {
switch n.id0.Key() {
+1
View File
@@ -158,6 +158,7 @@ var opStrings = [256]string{
t.KeyXBinaryAnd: " and ",
t.KeyXBinaryOr: " or ",
t.KeyXBinaryAs: " as ",
t.KeyXBinaryTildePlus: " ~+ ",
t.KeyXAssociativePlus: " + ",
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:
// 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)
}
+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",
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) {
@@ -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 {
ncv, err := evalConstValueBinaryOp(q.tm, n, lcv, rcv)
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
case t.KeyXBinaryOr:
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())
}
+95 -84
View File
@@ -173,30 +173,32 @@ const (
KeySemicolon = Key(IDSemicolon >> KeyShift)
KeyDollar = Key(IDDollar >> KeyShift)
KeyEq = Key(IDEq >> KeyShift)
KeyPlusEq = Key(IDPlusEq >> KeyShift)
KeyMinusEq = Key(IDMinusEq >> KeyShift)
KeyStarEq = Key(IDStarEq >> KeyShift)
KeySlashEq = Key(IDSlashEq >> KeyShift)
KeyShiftLEq = Key(IDShiftLEq >> KeyShift)
KeyShiftREq = Key(IDShiftREq >> KeyShift)
KeyAmpEq = Key(IDAmpEq >> KeyShift)
KeyAmpHatEq = Key(IDAmpHatEq >> KeyShift)
KeyPipeEq = Key(IDPipeEq >> KeyShift)
KeyHatEq = Key(IDHatEq >> KeyShift)
KeyPercentEq = Key(IDPercentEq >> KeyShift)
KeyEq = Key(IDEq >> KeyShift)
KeyPlusEq = Key(IDPlusEq >> KeyShift)
KeyMinusEq = Key(IDMinusEq >> KeyShift)
KeyStarEq = Key(IDStarEq >> KeyShift)
KeySlashEq = Key(IDSlashEq >> KeyShift)
KeyShiftLEq = Key(IDShiftLEq >> KeyShift)
KeyShiftREq = Key(IDShiftREq >> KeyShift)
KeyAmpEq = Key(IDAmpEq >> KeyShift)
KeyAmpHatEq = Key(IDAmpHatEq >> KeyShift)
KeyPipeEq = Key(IDPipeEq >> KeyShift)
KeyHatEq = Key(IDHatEq >> KeyShift)
KeyPercentEq = Key(IDPercentEq >> KeyShift)
KeyTildePlusEq = Key(IDTildePlusEq >> KeyShift)
KeyPlus = Key(IDPlus >> KeyShift)
KeyMinus = Key(IDMinus >> KeyShift)
KeyStar = Key(IDStar >> KeyShift)
KeySlash = Key(IDSlash >> KeyShift)
KeyShiftL = Key(IDShiftL >> KeyShift)
KeyShiftR = Key(IDShiftR >> KeyShift)
KeyAmp = Key(IDAmp >> KeyShift)
KeyAmpHat = Key(IDAmpHat >> KeyShift)
KeyPipe = Key(IDPipe >> KeyShift)
KeyHat = Key(IDHat >> KeyShift)
KeyPercent = Key(IDPercent >> KeyShift)
KeyPlus = Key(IDPlus >> KeyShift)
KeyMinus = Key(IDMinus >> KeyShift)
KeyStar = Key(IDStar >> KeyShift)
KeySlash = Key(IDSlash >> KeyShift)
KeyShiftL = Key(IDShiftL >> KeyShift)
KeyShiftR = Key(IDShiftR >> KeyShift)
KeyAmp = Key(IDAmp >> KeyShift)
KeyAmpHat = Key(IDAmpHat >> KeyShift)
KeyPipe = Key(IDPipe >> KeyShift)
KeyHat = Key(IDHat >> KeyShift)
KeyPercent = Key(IDPercent >> KeyShift)
KeyTildePlus = Key(IDTildePlus >> KeyShift)
KeyNotEq = Key(IDNotEq >> KeyShift)
KeyLessThan = Key(IDLessThan >> KeyShift)
@@ -320,6 +322,7 @@ const (
KeyXBinaryAnd = Key(IDXBinaryAnd >> KeyShift)
KeyXBinaryOr = Key(IDXBinaryOr >> KeyShift)
KeyXBinaryAs = Key(IDXBinaryAs >> KeyShift)
KeyXBinaryTildePlus = Key(IDXBinaryTildePlus >> KeyShift)
KeyXAssociativePlus = Key(IDXAssociativePlus >> KeyShift)
KeyXAssociativeStar = Key(IDXAssociativeStar >> KeyShift)
@@ -351,30 +354,32 @@ const (
IDSemicolon = ID(0x26<<KeyShift | FlagsTightLeft)
IDDollar = ID(0x27<<KeyShift | FlagsTightRight)
IDEq = ID(0x30<<KeyShift | FlagsAssign)
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
IDMinusEq = ID(0x32<<KeyShift | FlagsAssign)
IDStarEq = ID(0x33<<KeyShift | FlagsAssign)
IDSlashEq = ID(0x34<<KeyShift | FlagsAssign)
IDShiftLEq = ID(0x35<<KeyShift | FlagsAssign)
IDShiftREq = ID(0x36<<KeyShift | FlagsAssign)
IDAmpEq = ID(0x37<<KeyShift | FlagsAssign)
IDAmpHatEq = ID(0x38<<KeyShift | FlagsAssign)
IDPipeEq = ID(0x39<<KeyShift | FlagsAssign)
IDHatEq = ID(0x3A<<KeyShift | FlagsAssign)
IDPercentEq = ID(0x3B<<KeyShift | FlagsAssign)
IDEq = ID(0x30<<KeyShift | FlagsAssign)
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
IDMinusEq = ID(0x32<<KeyShift | FlagsAssign)
IDStarEq = ID(0x33<<KeyShift | FlagsAssign)
IDSlashEq = ID(0x34<<KeyShift | FlagsAssign)
IDShiftLEq = ID(0x35<<KeyShift | FlagsAssign)
IDShiftREq = ID(0x36<<KeyShift | FlagsAssign)
IDAmpEq = ID(0x37<<KeyShift | FlagsAssign)
IDAmpHatEq = ID(0x38<<KeyShift | FlagsAssign)
IDPipeEq = ID(0x39<<KeyShift | FlagsAssign)
IDHatEq = ID(0x3A<<KeyShift | FlagsAssign)
IDPercentEq = ID(0x3B<<KeyShift | FlagsAssign)
IDTildePlusEq = ID(0x3C<<KeyShift | FlagsAssign)
IDPlus = ID(0x41<<KeyShift | FlagsBinaryOp | FlagsUnaryOp | FlagsAssociativeOp)
IDMinus = ID(0x42<<KeyShift | FlagsBinaryOp | FlagsUnaryOp)
IDStar = ID(0x43<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDSlash = ID(0x44<<KeyShift | FlagsBinaryOp)
IDShiftL = ID(0x45<<KeyShift | FlagsBinaryOp)
IDShiftR = ID(0x46<<KeyShift | FlagsBinaryOp)
IDAmp = ID(0x47<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDAmpHat = ID(0x48<<KeyShift | FlagsBinaryOp)
IDPipe = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDHat = ID(0x4A<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDPercent = ID(0x4B<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDPlus = ID(0x41<<KeyShift | FlagsBinaryOp | FlagsUnaryOp | FlagsAssociativeOp)
IDMinus = ID(0x42<<KeyShift | FlagsBinaryOp | FlagsUnaryOp)
IDStar = ID(0x43<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDSlash = ID(0x44<<KeyShift | FlagsBinaryOp)
IDShiftL = ID(0x45<<KeyShift | FlagsBinaryOp)
IDShiftR = ID(0x46<<KeyShift | FlagsBinaryOp)
IDAmp = ID(0x47<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDAmpHat = ID(0x48<<KeyShift | FlagsBinaryOp)
IDPipe = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDHat = ID(0x4A<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDPercent = ID(0x4B<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDTildePlus = ID(0x4C<<KeyShift | FlagsBinaryOp) // TODO: FlagsAssociativeOp?
IDNotEq = ID(0x50<<KeyShift | FlagsBinaryOp)
IDLessThan = ID(0x51<<KeyShift | FlagsBinaryOp)
@@ -505,6 +510,7 @@ const (
IDXBinaryAnd = ID(0xE9<<KeyShift | FlagsBinaryOp)
IDXBinaryOr = ID(0xEA<<KeyShift | FlagsBinaryOp)
IDXBinaryAs = ID(0xEB<<KeyShift | FlagsBinaryOp)
IDXBinaryTildePlus = ID(0xEC<<KeyShift | FlagsBinaryOp)
IDXAssociativePlus = ID(0xF0<<KeyShift | FlagsAssociativeOp)
IDXAssociativeStar = ID(0xF1<<KeyShift | FlagsAssociativeOp)
@@ -546,34 +552,32 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeySemicolon: {";", IDSemicolon},
KeyDollar: {"$", IDDollar},
KeyEq: {"=", IDEq},
KeyPlusEq: {"+=", IDPlusEq},
KeyMinusEq: {"-=", IDMinusEq},
KeyStarEq: {"*=", IDStarEq},
KeySlashEq: {"/=", IDSlashEq},
KeyShiftLEq: {"<<=", IDShiftLEq},
KeyShiftREq: {">>=", IDShiftREq},
KeyAmpEq: {"&=", IDAmpEq},
KeyAmpHatEq: {"&^=", IDAmpHatEq},
KeyPipeEq: {"|=", IDPipeEq},
KeyHatEq: {"^=", IDHatEq},
KeyPercentEq: {"%=", IDPercentEq},
KeyEq: {"=", IDEq},
KeyPlusEq: {"+=", IDPlusEq},
KeyMinusEq: {"-=", IDMinusEq},
KeyStarEq: {"*=", IDStarEq},
KeySlashEq: {"/=", IDSlashEq},
KeyShiftLEq: {"<<=", IDShiftLEq},
KeyShiftREq: {">>=", IDShiftREq},
KeyAmpEq: {"&=", IDAmpEq},
KeyAmpHatEq: {"&^=", IDAmpHatEq},
KeyPipeEq: {"|=", IDPipeEq},
KeyHatEq: {"^=", IDHatEq},
KeyPercentEq: {"%=", IDPercentEq},
KeyTildePlusEq: {"~+=", IDTildePlusEq},
KeyPlus: {"+", IDPlus},
KeyMinus: {"-", IDMinus},
KeyStar: {"*", IDStar},
KeySlash: {"/", IDSlash},
KeyShiftL: {"<<", IDShiftL},
KeyShiftR: {">>", IDShiftR},
KeyAmp: {"&", IDAmp},
KeyAmpHat: {"&^", IDAmpHat},
KeyPipe: {"|", IDPipe},
KeyHat: {"^", IDHat},
KeyPercent: {"%", IDPercent},
// TODO: tilde-ops such as ~+ for ignoring overflow. Swift defines overflow
// 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.
KeyPlus: {"+", IDPlus},
KeyMinus: {"-", IDMinus},
KeyStar: {"*", IDStar},
KeySlash: {"/", IDSlash},
KeyShiftL: {"<<", IDShiftL},
KeyShiftR: {">>", IDShiftR},
KeyAmp: {"&", IDAmp},
KeyAmpHat: {"&^", IDAmpHat},
KeyPipe: {"|", IDPipe},
KeyHat: {"^", IDHat},
KeyPercent: {"%", IDPercent},
KeyTildePlus: {"~+", IDPercent},
KeyNotEq: {"!=", IDNotEq},
KeyLessThan: {"<", IDLessThan},
@@ -770,6 +774,10 @@ var lexers = [256][]suffixLexer{
{"=", IDGreaterEq},
{"", IDGreaterThan},
},
'~': {
{"+=", IDTildePlusEq},
{"+", IDTildePlus},
},
}
var ambiguousForms = [256]ID{
@@ -797,6 +805,7 @@ var ambiguousForms = [256]ID{
KeyXBinaryAnd: IDAnd,
KeyXBinaryOr: IDOr,
KeyXBinaryAs: IDAs,
KeyXBinaryTildePlus: IDTildePlus,
KeyXAssociativePlus: IDPlus,
KeyXAssociativeStar: IDStar,
@@ -814,17 +823,18 @@ var unaryForms = [256]ID{
}
var binaryForms = [256]ID{
KeyPlusEq: IDXBinaryPlus,
KeyMinusEq: IDXBinaryMinus,
KeyStarEq: IDXBinaryStar,
KeySlashEq: IDXBinarySlash,
KeyShiftLEq: IDXBinaryShiftL,
KeyShiftREq: IDXBinaryShiftR,
KeyAmpEq: IDXBinaryAmp,
KeyAmpHatEq: IDXBinaryAmpHat,
KeyPipeEq: IDXBinaryPipe,
KeyHatEq: IDXBinaryHat,
KeyPercentEq: IDXBinaryPercent,
KeyPlusEq: IDXBinaryPlus,
KeyMinusEq: IDXBinaryMinus,
KeyStarEq: IDXBinaryStar,
KeySlashEq: IDXBinarySlash,
KeyShiftLEq: IDXBinaryShiftL,
KeyShiftREq: IDXBinaryShiftR,
KeyAmpEq: IDXBinaryAmp,
KeyAmpHatEq: IDXBinaryAmpHat,
KeyPipeEq: IDXBinaryPipe,
KeyHatEq: IDXBinaryHat,
KeyPercentEq: IDXBinaryPercent,
KeyTildePlusEq: IDXBinaryTildePlus,
KeyPlus: IDXBinaryPlus,
KeyMinus: IDXBinaryMinus,
@@ -846,6 +856,7 @@ var binaryForms = [256]ID{
KeyAnd: IDXBinaryAnd,
KeyOr: IDXBinaryOr,
KeyAs: IDXBinaryAs,
KeyTildePlus: IDXBinaryTildePlus,
}
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 {
hlen -= n_copied
// TODO: a "~+" tilde-operator that ignores overflow.
hdist = (hdist + (n_copied & 0x7FFF)) & 0x7FFF
hdist = (hdist ~+ n_copied) & 0x7FFF
if hdist == 0 {
// Wrap around the this.history ringbuffer.
break