Add a nullptr literal

This commit is contained in:
Nigel Tao committed 2018-07-15 16:41:23 +10:00
1 parent fdf152f8b3
commit 15f2f42b77
8 files changed
+88 -44

No files matched your search

+3 -1
View File
@@ -45,8 +45,10 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32
} }
if cv := n.ConstValue(); cv != nil { if cv := n.ConstValue(); cv != nil {
if !n.MType().IsBool() { if typ := n.MType(); typ.IsNumTypeOrIdeal() {
b.writes(cv.String()) b.writes(cv.String())
} else if typ.IsNullptr() {
b.writes("NULL")
} else if cv.Cmp(zero) == 0 { } else if cv.Cmp(zero) == 0 {
b.writes("false") b.writes("false")
} else if cv.Cmp(one) == 0 { } else if cv.Cmp(one) == 0 {
+1
View File
@@ -6,6 +6,7 @@
- Renamed Puffs to Wuffs. - Renamed Puffs to Wuffs.
- Ship as a "single file C library" - Ship as a "single file C library"
- Added a `skipgendeps` flag. - Added a `skipgendeps` flag.
- Added a `nullptr` literal.
- Added an `io_bind` keyword. - Added an `io_bind` keyword.
- Added a `use` keyword. - Added a `use` keyword.
- Added a `yield` keyword. - Added a `yield` keyword.
+8
View File
@@ -701,6 +701,10 @@ func (n *TypeExpr) IsIOType() bool {
return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDIOReader || n.id2 == t.IDIOWriter) return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDIOReader || n.id2 == t.IDIOWriter)
} }
func (n *TypeExpr) IsNullptr() bool {
return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDQNullptr
}
func (n *TypeExpr) IsNumType() bool { func (n *TypeExpr) IsNumType() bool {
return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType() return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType()
} }
@@ -717,6 +721,10 @@ func (n *TypeExpr) IsArrayType() bool {
return n.id0 == t.IDArray return n.id0 == t.IDArray
} }
func (n *TypeExpr) IsPointerType() bool {
return n.id0 == t.IDNptr || n.id0 == t.IDPtr
}
func (n *TypeExpr) IsSliceType() bool { func (n *TypeExpr) IsSliceType() bool {
return n.id0 == t.IDSlice return n.id0 == t.IDSlice
} }
+1
View File
@@ -38,6 +38,7 @@ var (
typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil) typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDQIdeal, nil, nil, nil) typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDQIdeal, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil) typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil)
typeExprNullptr = a.NewTypeExpr(0, t.IDBase, t.IDQNullptr, nil, nil, nil)
typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDQPlaceholder, nil, nil, nil) typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDQPlaceholder, nil, nil, nil)
typeExprTypeExpr = a.NewTypeExpr(0, t.IDBase, t.IDQTypeExpr, nil, nil, nil) typeExprTypeExpr = a.NewTypeExpr(0, t.IDBase, t.IDQTypeExpr, nil, nil, nil)
+28 -9
View File
@@ -404,6 +404,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetConstValue(one) n.SetConstValue(one)
n.SetMType(typeExprBool) n.SetMType(typeExprBool)
return nil return nil
case t.IDNullptr:
n.SetConstValue(zero)
n.SetMType(typeExprNullptr)
return nil
} }
case t.IDOpenParen, t.IDTry: case t.IDOpenParen, t.IDTry:
@@ -735,6 +740,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
} }
rTyp := rhs.MType() rTyp := rhs.MType()
pointerComparison := false
switch op { switch op {
case t.IDXBinaryAnd, t.IDXBinaryOr: case t.IDXBinaryAnd, t.IDXBinaryOr:
if !lTyp.IsBool() { if !lTyp.IsBool() {
@@ -745,24 +751,37 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
} }
case t.IDXBinaryNotEq, t.IDXBinaryEqEq:
if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) {
break
}
fallthrough
default: default:
bad := (*a.Expr)(nil)
if !lTyp.IsNumTypeOrIdeal() { if !lTyp.IsNumTypeOrIdeal() {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", bad = lhs
op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) } else if !rTyp.IsNumTypeOrIdeal() {
bad = rhs
} }
if !rTyp.IsNumTypeOrIdeal() { if op == t.IDXBinaryNotEq || op == t.IDXBinaryEqEq {
if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) {
break
}
lNullptr := lTyp.Eq(typeExprNullptr)
rNullptr := rTyp.Eq(typeExprNullptr)
if (lNullptr && rNullptr) ||
(lNullptr && rTyp.IsPointerType()) ||
(rNullptr && lTyp.IsPointerType()) {
pointerComparison = true
break
}
}
if bad != nil {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) op.AmbiguousForm().Str(q.tm), bad.Str(q.tm), bad.MType().Str(q.tm))
} }
} }
switch op { switch op {
default: default:
if pointerComparison {
break
}
if !lTyp.EqIgnoringRefinements(rTyp) && !lTyp.IsIdeal() && !rTyp.IsIdeal() { if !lTyp.EqIgnoringRefinements(rTyp) && !lTyp.IsIdeal() && !rTyp.IsIdeal() {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types", return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
op.AmbiguousForm().Str(q.tm), op.AmbiguousForm().Str(q.tm),
+34 -25
View File
@@ -371,22 +371,24 @@ const (
const ( const (
minBuiltInLiteral = 0xB0 minBuiltInLiteral = 0xB0
minBuiltInNumLiteral = 0xB2 minBuiltInNumLiteral = 0xB3
maxBuiltInNumLiteral = 0xBF maxBuiltInNumLiteral = 0xBF
maxBuiltInLiteral = 0xBF maxBuiltInLiteral = 0xBF
IDFalse = ID(0xB0) IDFalse = ID(0xB0)
IDTrue = ID(0xB1) IDTrue = ID(0xB1)
ID0 = ID(0xB2) IDNullptr = ID(0xB2)
ID1 = ID(0xB3)
ID2 = ID(0xB4) ID0 = ID(0xB3)
ID4 = ID(0xB5) ID1 = ID(0xB4)
ID8 = ID(0xB6) ID2 = ID(0xB5)
ID16 = ID(0xB7) ID4 = ID(0xB6)
ID32 = ID(0xB8) ID8 = ID(0xB7)
ID64 = ID(0xB9) ID16 = ID(0xB8)
ID128 = ID(0xBA) ID32 = ID(0xB9)
ID256 = ID(0xBB) ID64 = ID(0xBA)
ID128 = ID(0xBB)
ID256 = ID(0xBC)
) )
const ( const (
@@ -421,6 +423,7 @@ const (
IDDagger1 = ID(0x11A) IDDagger1 = ID(0x11A)
IDDagger2 = ID(0x11B) IDDagger2 = ID(0x11B)
IDQNullptr = ID(0x11C)
IDQPlaceholder = ID(0x11D) IDQPlaceholder = ID(0x11D)
IDQTypeExpr = ID(0x11E) IDQTypeExpr = ID(0x11E)
@@ -648,18 +651,20 @@ var builtInsByID = [nBuiltInIDs]string{
IDSlice: "slice", IDSlice: "slice",
IDTable: "table", IDTable: "table",
IDFalse: "false", IDFalse: "false",
IDTrue: "true", IDTrue: "true",
ID0: "0", IDNullptr: "nullptr",
ID1: "1",
ID2: "2", ID0: "0",
ID4: "4", ID1: "1",
ID8: "8", ID2: "2",
ID16: "16", ID4: "4",
ID32: "32", ID8: "8",
ID64: "64", ID16: "16",
ID128: "128", ID32: "32",
ID256: "256", ID64: "64",
ID128: "128",
ID256: "256",
// -------- 0x100 block. // -------- 0x100 block.
@@ -692,6 +697,10 @@ var builtInsByID = [nBuiltInIDs]string{
IDDagger1: "†", // U+2020 DAGGER IDDagger1: "†", // U+2020 DAGGER
IDDagger2: "‡", // U+2021 DOUBLE DAGGER IDDagger2: "‡", // U+2021 DOUBLE DAGGER
// IDQNullptr is used by the type checker to build an artificial MType for
// the nullptr literal.
IDQNullptr: "«Nullptr»",
// IDQPlaceholder is used by the type checker to build an artificial MType // IDQPlaceholder is used by the type checker to build an artificial MType
// for AST nodes that aren't expression nodes or type expression nodes, // for AST nodes that aren't expression nodes or type expression nodes,
// such as struct definition nodes and statement nodes. Its presence means // such as struct definition nodes and statement nodes. Its presence means
+5 -3
View File
@@ -6573,9 +6573,11 @@ wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self,
(self->private_impl.f_frame_rect_x1 == self->private_impl.f_width) && (self->private_impl.f_frame_rect_x1 == self->private_impl.f_width) &&
(self->private_impl.f_frame_rect_y1 == self->private_impl.f_height) && (self->private_impl.f_frame_rect_y1 == self->private_impl.f_height) &&
self->private_impl.f_has_full_palette); self->private_impl.f_has_full_palette);
wuffs_base__image_config__initialize( if (a_dst != NULL) {
a_dst, 570984584, 0, self->private_impl.f_width, wuffs_base__image_config__initialize(
self->private_impl.f_height, v_num_loops, v_ffio); a_dst, 570984584, 0, self->private_impl.f_width,
self->private_impl.f_height, v_num_loops, v_ffio);
}
self->private_impl.f_call_sequence = 1; self->private_impl.f_call_sequence = 1;
goto ok; goto ok;
+8 -6
View File
@@ -126,12 +126,14 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)()
(this.frame_rect_y1 == this.height) and (this.frame_rect_y1 == this.height) and
(this.has_full_palette) (this.has_full_palette)
// TODO: rename initialize to set? if in.dst != nullptr {
// // TODO: rename initialize to set?
// TODO: a Wuffs (not just C) name for the //
// WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant. // TODO: a Wuffs (not just C) name for the
in.dst.initialize!(pixfmt:0x22088888, pixsub:0, // WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant.
width:this.width, height:this.height, num_loops:num_loops, first_frame_is_opaque:ffio) in.dst.initialize!(pixfmt:0x22088888, pixsub:0,
width:this.width, height:this.height, num_loops:num_loops, first_frame_is_opaque:ffio)
}
this.call_sequence = 1 this.call_sequence = 1
} }