Generate C code for "if foo?()".

This commit is contained in:
Nigel Tao committed 2017-06-16 13:05:07 +10:00
1 parent f4db300358
commit 8882085f91
5 files changed
+74 -9

No files matched your search

+21 -4
View File
@@ -798,9 +798,24 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible")
case a.KIf:
// TODO: consider suspendible calls.
n := n.If()
for {
// TODO: for writeSuspendibles, make sure that we get order of
// sub-expression evaluation correct.
n, nCloseCurly := n.If(), 1
for first := true; ; first = false {
if n.Condition().Suspendible() {
if !first {
g.writeb('{')
const maxCloseCurly = 1000
if nCloseCurly == maxCloseCurly {
return fmt.Errorf("too many nested if's")
}
nCloseCurly++
}
if err := g.writeSuspendibles(n.Condition(), depth); err != nil {
return err
}
}
g.writes("if (")
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err
@@ -826,7 +841,9 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
}
g.writes("} else ")
}
g.writes("}\n")
for ; nCloseCurly > 0; nCloseCurly-- {
g.writes("}\n")
}
return nil
case a.KJump:
+13 -2
View File
@@ -106,7 +106,6 @@ typedef struct {
puffs_gif_status status;
uint32_t magic;
puffs_gif_lzw_decoder f_lzw;
uint32_t f_temporary_hack;
} private_impl;
} puffs_gif_decoder;
@@ -286,7 +285,19 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
goto cleanup0;
}
if (self->private_impl.f_temporary_hack != 71) {
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_0 = a_src->ptr[a_src->ri++];
if (a_src->ri >= a_src->wi) {
status = a_src->closed ? puffs_gif_error_unexpected_eof
: puffs_gif_status_short_read;
goto cleanup0;
}
uint8_t t_1 = a_src->ptr[a_src->ri++];
if ((t_0 != 71) || (t_1 != 73)) {
status = puffs_gif_error_bad_gif_header;
goto cleanup0;
}
-1
View File
@@ -106,7 +106,6 @@ typedef struct {
puffs_gif_status status;
uint32_t magic;
puffs_gif_lzw_decoder f_lzw;
uint32_t f_temporary_hack;
} private_impl;
} puffs_gif_decoder;
+1 -2
View File
@@ -5,11 +5,10 @@ pub error "bad GIF header"
pub struct decoder?(
lzw lzw_decoder,
temporary_hack u32,
)
pub func decoder.decode?(dst ptr buf1, src ptr buf1)() {
if this.temporary_hack != 0x47 {
if (in.src.read_u8?() != 0x47) or (in.src.read_u8?() != 0x49) {
return error "bad GIF header"
}
}
+39
View File
@@ -244,6 +244,42 @@ cleanup1:
cleanup0:;
}
// ---------------- GIF Tests
void test_gif_decode_input_is_a_xxx(const char* filename,
puffs_gif_status want) {
puffs_base_buf1 dst = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
if (!read_file(&src, filename)) {
goto cleanup0;
}
puffs_gif_decoder dec;
puffs_gif_decoder_constructor(&dec, PUFFS_VERSION, 0);
puffs_gif_status got = puffs_gif_decoder_decode(&dec, &dst, &src);
if (got != want) {
FAIL("status: got %d, want %d", got, want);
goto cleanup1;
}
cleanup1:
puffs_gif_decoder_destructor(&dec);
cleanup0:;
}
void test_gif_decode_input_is_a_gif() {
test_funcname = __func__;
test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.gif",
puffs_gif_status_ok);
}
void test_gif_decode_input_is_a_png() {
test_funcname = __func__;
test_gif_decode_input_is_a_xxx("../../testdata/bricks-dither.png",
puffs_gif_error_bad_gif_header);
}
// ---------------- Manifest
// The empty comments forces clang-format to place one element per line.
@@ -260,6 +296,9 @@ test tests[] = {
test_sub_struct_constructor, //
// LZW Tests
test_lzw_decode, //
// GIF Tests
test_gif_decode_input_is_a_gif, //
test_gif_decode_input_is_a_png, //
// End
NULL,
};