diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index d0b90bd9..264316e2 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -673,8 +673,13 @@ func (g *gen) writeExprOther(n *a.Expr, depth uint32) error { return nil case t.KeyOpenParen: - // n is a function call. - // TODO. + // n is a function call. + // TODO: delete this hack that only matches "in.src.read_u8?()". + if isInSrcReadU8(g.idMap, n.LHS().Expr()) && len(n.Args()) == 0 { + // TODO. + g.writes("42") + return nil + } case t.KeyOpenBracket: // n is an index. @@ -698,6 +703,18 @@ func (g *gen) writeExprOther(n *a.Expr, depth uint32) error { return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key()) } +func isInSrcReadU8(m *t.IDMap, n *a.Expr) bool { + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1() != m.ByName("src") { + return false + } + n = n.LHS().Expr() + return n.ID0() == 0 && n.ID1().Key() == t.KeyIn +} + func (g *gen) writeExprUnaryOp(n *a.Expr, depth uint32) error { // TODO. return nil diff --git a/gen/c/gif.c b/gen/c/gif.c index 1bb14a23..f9830819 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 |= ((uint32_t)(0)); + v_bits |= ((uint32_t)(42)); v_n_bits += 8; } v_bits >>= v_width; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 3d53a5b6..84c6eaa8 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -428,6 +428,8 @@ const MaxTypeExprDepth = 63 // "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be // refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For // example, the LHS for "u32[..4095]" is nil. +// +// TODO: function / method types. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 652bf2c0..0bd09243 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -444,7 +444,10 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, // No-op. case t.KeyOpenParen: - // TODO. + // TODO: delete this hack that only matches "in.src.read_u8?()". + if isInSrcReadU8(q.idMap, n.LHS().Expr()) && len(n.Args()) == 0 { + break + } return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) case t.KeyOpenBracket: diff --git a/lang/check/check.go b/lang/check/check.go index c7c0eea9..315bdca8 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -48,11 +48,11 @@ func (e *Error) Error() string { return string(b) } -// TypeExprFoo is an *ast.Node MType (implicit type) that means that n is a -// boolean or a ideal numeric constant. +// TypeExprFoo is an *ast.Node MType (implicit type). var ( TypeExprBoolean = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) TypeExprIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil) + TypeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil) ) func numeric(n *a.TypeExpr) bool { return n == TypeExprIdealNumber || n.IsNumType() } @@ -349,6 +349,14 @@ func (c *Checker) checkFuncBody(node *a.Node) error { n.Node().SetTypeChecked() if err := n.Node().Walk(func(o *a.Node) error { if !o.TypeChecked() { + switch o.Kind() { + case a.KExpr: + return fmt.Errorf("check: internal error: unchecked %s node %q", + o.Kind(), o.Expr().String(q.idMap)) + case a.KTypeExpr: + return fmt.Errorf("check: internal error: unchecked %s node %q", + o.Kind(), o.TypeExpr().String(q.idMap)) + } return fmt.Errorf("check: internal error: unchecked %s node", o.Kind()) } if o.Kind() == a.KExpr { diff --git a/lang/check/type.go b/lang/check/type.go index ab21cc20..b0914c4e 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -293,7 +293,14 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyOpenParen: // n is a function call. - // TODO. + // TODO: delete this hack that only matches "in.src.read_u8?()". + if isInSrcReadU8(q.idMap, n.LHS().Expr()) && len(n.Args()) == 0 { + if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { + return err + } + n.SetMType(TypeExprU8) + return nil + } case t.KeyOpenBracket: // n is an index. @@ -309,6 +316,18 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key()) } +func isInSrcReadU8(m *t.IDMap, n *a.Expr) bool { + if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 { + return false + } + n = n.LHS().Expr() + if n.ID0().Key() != t.KeyDot || n.ID1() != m.ByName("src") { + return false + } + n = n.LHS().Expr() + return n.ID0() == 0 && n.ID1().Key() == t.KeyIn +} + func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { lhs := n.LHS().Expr() if err := q.tcheckExpr(lhs, depth); err != nil { @@ -330,6 +349,14 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { s = q.f.Func.In() case t.KeyOut: s = q.f.Func.Out() + case t.KeyBuf1: + // TODO: remove this hack and be more principled about the built-in + // buf1 type. + // + // Another hack is using TypeExprU8 until a TypeExpr can represent + // function types. + n.SetMType(TypeExprU8) + return nil default: s = q.c.structs[name].Struct } diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 95b45569..4fc5a39c 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 as u32 // TODO + bits |= in.src.read_u8?() as u32 // TODO n_bits += 8 } // TODO: var code u32[..4095] = bits.low_bits(width)