From 43aaee31b9cedc059213396f1e7aa420ace0c797 Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Fri, 31 Jul 2009 14:01:37 -0700 Subject: Support the comma operator. --- libacc/acc.cpp | 21 +++++++++++++++------ libacc/tests/data/comma.c | 35 +++++++++++++++++++++++++++++++++++ libacc/tests/test.py | 10 ++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 libacc/tests/data/comma.c diff --git a/libacc/acc.cpp b/libacc/acc.cpp index 823849c7..da703a70 100644 --- a/libacc/acc.cpp +++ b/libacc/acc.cpp @@ -3868,7 +3868,7 @@ class Compiler : public ErrorSink { pGen->forceR0RVal(); pGen->convertR0(pCast); } else { - expr(); + commaExpr(); skip(')'); } } else if (t == '*') { @@ -4076,12 +4076,21 @@ class Compiler : public ErrorSink { } } + void commaExpr() { + for(;;) { + expr(); + if (!accept(',')) { + break; + } + } + } + void expr() { binaryOp(11); } int test_expr() { - expr(); + commaExpr(); pGen->forceR0RVal(); return pGen->gtst(0, 0); } @@ -4117,7 +4126,7 @@ class Compiler : public ErrorSink { a = test_expr(); } else { if (tok != ';') - expr(); + commaExpr(); skip(';'); n = codeBuf.getPC(); a = 0; @@ -4126,7 +4135,7 @@ class Compiler : public ErrorSink { skip(';'); if (tok != ')') { t = pGen->gjmp(0); - expr(); + commaExpr(); pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); pGen->gsym(t); n = t + 4; @@ -4150,7 +4159,7 @@ class Compiler : public ErrorSink { } else { if (accept(TOK_RETURN)) { if (tok != ';') { - expr(); + commaExpr(); pGen->forceR0RVal(); if (pReturnType->tag == TY_VOID) { error("Must not return a value from a void function"); @@ -4166,7 +4175,7 @@ class Compiler : public ErrorSink { } else if (accept(TOK_BREAK)) { *(int *) l = pGen->gjmp(*(int *) l); } else if (tok != ';') - expr(); + commaExpr(); skip(';'); } } diff --git a/libacc/tests/data/comma.c b/libacc/tests/data/comma.c new file mode 100644 index 00000000..496944ca --- /dev/null +++ b/libacc/tests/data/comma.c @@ -0,0 +1,35 @@ +int testReturn() { + return 10, 20, 30; +} + +int testArg(int a) { + return a; +} + +void testComma() { + int a; + 0, a = 10,20; + printf("statement: %d\n", a); + a = 1; + if (a = 0, 1) { + printf("if: a = %d\n", a); + } + int b = 0; + a = 10; + while(b++,a--) {} + printf("while: b = %d\n", b); + b = 0; + for(b++,a = 0;b++, a < 10; b++, a++) {} + printf("for: b = %d\n", b); + b = testReturn(); + printf("return: %d\n", b); + b = testArg((a,12)); + printf("arg: %d\n", b); +} + + + +int main() { + testComma(); + return 0; +} diff --git a/libacc/tests/test.py b/libacc/tests/test.py index 239bb9b9..5f9a71e3 100644 --- a/libacc/tests/test.py +++ b/libacc/tests/test.py @@ -352,6 +352,16 @@ result: 0""", """2 *= 5 10 17&= 1 1 17^= 1 16 16|= 1 17 +""") + + def testcomma(self): + self.compileCheck(["-R", "data/comma.c"], """Executing compiled code: +result: 0""", """statement: 10 +if: a = 0 +while: b = 11 +for: b = 22 +return: 30 +arg: 12 """) if __name__ == '__main__': -- cgit v1.2.3