system/corennnnn
Revisión | 0c01774816245e59aac7f2109c28523eeb6492ac (tree) |
---|---|
Tiempo | 2009-08-01 04:00:39 |
Autor | Jack Palevich <jackpal@goog...> |
Commiter | Jack Palevich |
Implement op=.
@@ -3188,6 +3188,7 @@ class Compiler : public ErrorSink { | ||
3188 | 3188 | static const int TOK_NUM = 2; |
3189 | 3189 | static const int TOK_NUM_FLOAT = 3; |
3190 | 3190 | static const int TOK_NUM_DOUBLE = 4; |
3191 | + static const int TOK_OP_ASSIGNMENT = 5; | |
3191 | 3192 | |
3192 | 3193 | // 3..255 are character and/or operators |
3193 | 3194 |
@@ -3626,6 +3627,13 @@ class Compiler : public ErrorSink { | ||
3626 | 3627 | inp(); |
3627 | 3628 | tok = TOK_DUMMY; /* dummy token for double tokens */ |
3628 | 3629 | } |
3630 | + /* check for op=, valid for * / % + - << >> & ^ | */ | |
3631 | + if (ch == '=' && | |
3632 | + ((tokl >= 1 && tokl <= 3) | |
3633 | + || tokl >=6 && tokl <= 8) ) { | |
3634 | + inp(); | |
3635 | + tok = TOK_OP_ASSIGNMENT; | |
3636 | + } | |
3629 | 3637 | break; |
3630 | 3638 | } |
3631 | 3639 | opIndex++; |
@@ -3797,6 +3805,17 @@ class Compiler : public ErrorSink { | ||
3797 | 3805 | expr(); |
3798 | 3806 | pGen->forceR0RVal(); |
3799 | 3807 | pGen->storeR0ToTOS(); |
3808 | + } else if (tok == TOK_OP_ASSIGNMENT) { | |
3809 | + int t = tokc; | |
3810 | + next(); | |
3811 | + checkLVal(); | |
3812 | + pGen->pushR0(); | |
3813 | + pGen->forceR0RVal(); | |
3814 | + pGen->pushR0(); | |
3815 | + expr(); | |
3816 | + pGen->forceR0RVal(); | |
3817 | + pGen->genOp(t); | |
3818 | + pGen->storeR0ToTOS(); | |
3800 | 3819 | } |
3801 | 3820 | } |
3802 | 3821 |
@@ -0,0 +1,66 @@ | ||
1 | +// Test assignment operations | |
2 | + | |
3 | +void testAssignment() { | |
4 | + int a = 2; | |
5 | + a *= 5; | |
6 | + printf("2 *= 5 %d\n", a); | |
7 | + a = 20; | |
8 | + a /= 5; | |
9 | + printf("20 /= 5 %d\n", a); | |
10 | + a = 17; | |
11 | + a %= 5; | |
12 | + printf("17 %%= 5 %d\n", a); | |
13 | + a = 17; | |
14 | + a += 5; | |
15 | + printf("17 += 5 %d\n", a); | |
16 | + a = 17; | |
17 | + a-=5; | |
18 | + printf("17 -= 5 %d\n", a); | |
19 | + a = 17; | |
20 | + a<<=1; | |
21 | + printf("17<<= 1 %d\n", a); | |
22 | + a = 17; | |
23 | + a>>=1; | |
24 | + printf("17>>= 1 %d\n", a); | |
25 | + a = 17; | |
26 | + a&=1; | |
27 | + printf("17&= 1 %d\n", a); | |
28 | + a = 17; | |
29 | + a^=1; | |
30 | + printf("17^= 1 %d\n", a); | |
31 | + a = 16; | |
32 | + a^=1; | |
33 | + printf("16|= 1 %d\n", a); | |
34 | +} | |
35 | + | |
36 | +/* Can't use because int* f() is not parsed as a function decl. | |
37 | +int a; | |
38 | + | |
39 | +int* f() { | |
40 | + printf("f()\n"); | |
41 | + return &a; | |
42 | +} | |
43 | + | |
44 | +void testEval() { | |
45 | + a = 0; | |
46 | + printf("*f() = *f() + 10;"); | |
47 | + *f() = *f() + 10; | |
48 | + printf("a = %d\n", a); | |
49 | +} | |
50 | + | |
51 | +void testOpEval() { | |
52 | + a = 0; | |
53 | + printf("*f() += 10;"); | |
54 | + *f() += 10; | |
55 | + printf("a = %d\n", a); | |
56 | +} | |
57 | + | |
58 | +*/ | |
59 | +int main() { | |
60 | + testAssignment(); | |
61 | + /* | |
62 | + testEval(); | |
63 | + testOpEval(); | |
64 | + */ | |
65 | + return 0; | |
66 | +} |
@@ -340,6 +340,20 @@ a = 0, *pa = 0 **ppa = 0 | ||
340 | 340 | a = 2, *pa = 2 **ppa = 2 |
341 | 341 | """) |
342 | 342 | |
343 | + def testassignmentop(self): | |
344 | + self.compileCheck(["-R", "data/assignmentop.c"], """Executing compiled code: | |
345 | +result: 0""", """2 *= 5 10 | |
346 | +20 /= 5 4 | |
347 | +17 %= 5 2 | |
348 | +17 += 5 22 | |
349 | +17 -= 5 12 | |
350 | +17<<= 1 34 | |
351 | +17>>= 1 8 | |
352 | +17&= 1 1 | |
353 | +17^= 1 16 | |
354 | +16|= 1 17 | |
355 | +""") | |
356 | + | |
343 | 357 | if __name__ == '__main__': |
344 | 358 | if not outputCanRun(): |
345 | 359 | print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable." |