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

Commit 95f2af6d authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 9366

* changes:
  Implement op=.
parents 18f275a9 0c017748
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -3188,6 +3188,7 @@ class Compiler : public ErrorSink {
    static const int TOK_NUM = 2;
    static const int TOK_NUM_FLOAT = 3;
    static const int TOK_NUM_DOUBLE = 4;
    static const int TOK_OP_ASSIGNMENT = 5;

    // 3..255 are character and/or operators

@@ -3626,6 +3627,13 @@ class Compiler : public ErrorSink {
                            inp();
                            tok = TOK_DUMMY; /* dummy token for double tokens */
                        }
                        /* check for op=, valid for * / % + - << >> & ^ | */
                        if (ch == '=' &&
                                ((tokl >= 1 && tokl <= 3)
                                        || tokl >=6 && tokl <= 8) ) {
                            inp();
                            tok = TOK_OP_ASSIGNMENT;
                        }
                        break;
                    }
                    opIndex++;
@@ -3797,6 +3805,17 @@ class Compiler : public ErrorSink {
            expr();
            pGen->forceR0RVal();
            pGen->storeR0ToTOS();
        } else if (tok == TOK_OP_ASSIGNMENT) {
            int t = tokc;
            next();
            checkLVal();
            pGen->pushR0();
            pGen->forceR0RVal();
            pGen->pushR0();
            expr();
            pGen->forceR0RVal();
            pGen->genOp(t);
            pGen->storeR0ToTOS();
        }
    }

+66 −0
Original line number Diff line number Diff line
// Test assignment operations

void testAssignment() {
    int a = 2;
    a *= 5;
    printf("2 *= 5  %d\n", a);
    a = 20;
    a /= 5;
    printf("20 /= 5  %d\n", a);
    a = 17;
    a %= 5;
    printf("17 %%= 5  %d\n", a);
    a = 17;
    a += 5;
    printf("17 += 5  %d\n", a);
    a = 17;
    a-=5;
    printf("17 -= 5  %d\n", a);
    a = 17;
    a<<=1;
    printf("17<<= 1  %d\n", a);
    a = 17;
    a>>=1;
    printf("17>>= 1  %d\n", a);
    a = 17;
    a&=1;
    printf("17&= 1  %d\n", a);
    a = 17;
    a^=1;
    printf("17^= 1  %d\n", a);
    a = 16;
    a^=1;
    printf("16|= 1  %d\n", a);
}

/* Can't use because int* f() is not parsed as a function decl.
int a;

int* f() {
    printf("f()\n");
    return &a;
}

void testEval() {
    a = 0;
    printf("*f() = *f() + 10;");
    *f() = *f() + 10;
    printf("a = %d\n", a);
}

void testOpEval() {
    a = 0;
    printf("*f() += 10;");
    *f() += 10;
    printf("a = %d\n", a);
}

*/
int main() {
    testAssignment();
    /*
    testEval();
    testOpEval();
    */
    return 0;
}
+14 −0
Original line number Diff line number Diff line
@@ -338,6 +338,20 @@ a = 0, *pa = 0 **ppa = 0
a = 2, *pa = 2 **ppa = 2
a = 0, *pa = 0 **ppa = 0
a = 2, *pa = 2 **ppa = 2
""")

    def testassignmentop(self):
        self.compileCheck(["-R", "data/assignmentop.c"], """Executing compiled code:
result: 0""", """2 *= 5  10
20 /= 5  4
17 %= 5  2
17 += 5  22
17 -= 5  12
17<<= 1  34
17>>= 1  8
17&= 1  1
17^= 1  16
16|= 1  17
""")

if __name__ == '__main__':