Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a4944fb3 authored by Jack Palevich's avatar Jack Palevich Committed by Android Git Automerger
Browse files

am 973246de: am c158427e: Merge change 22828 into eclair

Merge commit '973246de'

* commit '973246de':
  Preliminary struct and union support.
parents c1f46267 973246de
Loading
Loading
Loading
Loading
+385 −95

File changed.

Preview size limit exceeded, changes collapsed.

+95 −0
Original line number Diff line number Diff line
// struct definition and declaration
struct a {
    int a;
    int b;
} c;

// Useless, but legal struct declaration
struct {
    int x;
};

// Useful anonymous struct declaration
struct {
    int y;
} anon1, anon2;

// forward declarations
struct a;
struct b;
struct c;

struct b {int a; int b; };

// struct c {b g; }; // syntax error.

// struct s {float c,a,b,c;} s; // duplicate struct member

struct c {struct b g; };

// struct a { int w; }; // error

void testCopying() {
    struct a {int a[10]; char c;} a, b;
    a.c = 37;
    b.c = 38;
    b = a;
    printf("testCopying: %d == %d\n", a.c, b.c);
}

void testUnion() {
    union u;
    union u {float f;int i;} u;
    u.f = 1.0f;
    printf("testUnion: %g == 0x%08x\n", u.f, u.i);
}

struct v {float x, y, z, w; };

void add(struct v* result, struct v* a, struct v* b) {
    result->x = a->x + b->x;
    result->y = a->y + b->y;
    result->z = a->z + b->z;
    result->w = a->w + b->w;
}

void set(struct v* v, float x, float y, float z, float w) {
    v->x = x;
    v->y = y;
    v->z = z;
    v->w = w;
}

void print(struct v* v) {
    printf("(%g, %g, %g, %g)\n", v->x, v->y, v->z, v->w);
}

void testArgs() {
    struct v a, b, c;
    set(&a, 1.0f, 2.0f, 3.0f, 4.0f);
    set(&b, 5.0f, 6.0f, 7.0f, 8.0f);
    add(&c, &a, &b);
    printf("testArgs: ");
    print(&c);
}

int main() {
    anon1.y = 3;
    anon2.y = anon1.y;

    testCopying();
    testUnion();
    testArgs();

    struct c cc;
    cc.g.a = 3;
    c.a = 1;
    c.b = 3;
    struct a {int x, y; } z;
    // struct a {int x, y; } z2;
    z.x = c.a;
    struct a *pA;
    pA = &z;
    pA->x += 5;
    return pA->x;
}
+7 −0
Original line number Diff line number Diff line
@@ -463,6 +463,13 @@ result: 4
result: 1092616192
""","""""")

    def testStructs(self):
        self.compileCheck(["-R", "data/structs.c"], """Executing compiled code:
testCopying: 37 == 37
testUnion: 1 == 0x3f800000
testArgs: (6, 8, 10, 12)
result: 6
""","""""")

def main():
    checkEnvironment()