From db41d789a5af235af288bfc0cfefaabc0bc4c1af Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 3 Jun 2017 10:24:52 +1000 Subject: [PATCH] Generate C code for "x as T". --- cmd/puffs-gen-c/main.go | 25 +++++++++++++++++++++++-- gen/c/gif.c | 2 +- lang/token/list.go | 5 +++++ std/gif/decode_lzw.puffs | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index a5293de5..d0b90bd9 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -706,8 +706,7 @@ func (g *gen) writeExprUnaryOp(n *a.Expr, depth uint32) error { func (g *gen) writeExprBinaryOp(n *a.Expr, depth uint32) error { op := n.ID0() if op.Key() == t.KeyXBinaryAs { - // TODO. - return nil + return g.writeExprAs(n.LHS().Expr(), n.RHS().TypeExpr(), depth) } g.writeb('(') if err := g.writeExpr(n.LHS().Expr(), depth); err != nil { @@ -722,6 +721,28 @@ func (g *gen) writeExprBinaryOp(n *a.Expr, depth uint32) error { return nil } +func (g *gen) writeExprAs(lhs *a.Expr, rhs *a.TypeExpr, depth uint32) error { + g.writes("((") + for { + if rhs.PackageOrDecorator() == 0 { + if k := rhs.Name().Key(); k < t.Key(len(cTypeNames)) { + if s := cTypeNames[k]; s != "" { + g.writes(s) + break + } + } + } + // TODO: fix this. + return fmt.Errorf("cannot convert Puffs type %q to C", rhs.String(g.idMap)) + } + g.writes(")(") + if err := g.writeExpr(lhs, depth); err != nil { + return err + } + g.writes("))") + return nil +} + func (g *gen) writeExprAssociativeOp(n *a.Expr, depth uint32) error { // TODO. return nil diff --git a/gen/c/gif.c b/gen/c/gif.c index e5311549..1bb14a23 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -214,7 +214,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_width = (self->f_literal_width + 1); while (1) { while ((v_n_bits < v_width)) { - v_bits |= 0; + v_bits |= ((uint32_t)(0)); v_n_bits += 8; } v_bits >>= v_width; diff --git a/lang/token/list.go b/lang/token/list.go index a9649359..f981d195 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -438,6 +438,10 @@ var builtInsByKey = [nBuiltInKeys]struct { KeyAmpHat: {"&^", IDAmpHat}, KeyPipe: {"|", IDPipe}, KeyHat: {"^", IDHat}, + // 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. KeyNotEq: {"!=", IDNotEq}, KeyLessThan: {"<", IDLessThan}, @@ -450,6 +454,7 @@ var builtInsByKey = [nBuiltInKeys]struct { KeyOr: {"or", IDOr}, KeyNot: {"not", IDNot}, KeyAs: {"as", IDAs}, + // TODO: do we need a "~as" form of the as operator, similar to "~+". KeyFunc: {"func", IDFunc}, KeyPtr: {"ptr", IDPtr}, diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index b232fcc9..95b45569 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -24,7 +24,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { post n_bits >= width, { assert n_bits < 12 via "a < b: a < c; c <= b"(c:width) - bits |= 0 // TODO. + bits |= 0 as u32 // TODO n_bits += 8 } // TODO: var code u32[..4095] = bits.low_bits(width)