Generate C code for "x as T".

This commit is contained in:
Nigel Tao committed 2017-06-03 10:24:52 +10:00
1 parent e87cdaa9f5
commit db41d789a5
4 files changed
+30 -4

No files matched your search

+23 -2
View File
@@ -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
+1 -1
View File
@@ -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;
+5
View File
@@ -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},
+1 -1
View File
@@ -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)