Support brackets for accessing array values.
Don't yet support allocating arrays.
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index 436f081..f70c7a1 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -3630,7 +3630,7 @@
/* check for op=, valid for * / % + - << >> & ^ | */
if (ch == '=' &&
((tokl >= 1 && tokl <= 3)
- || tokl >=6 && tokl <= 8) ) {
+ || (tokl >=6 && tokl <= 8)) ) {
inp();
tok = TOK_OP_ASSIGNMENT;
}
@@ -3879,18 +3879,7 @@
/* This is a pointer dereference.
*/
unary();
- pGen->forceR0RVal();
- Type* pR0Type = pGen->getR0Type();
- if (pR0Type->tag != TY_POINTER) {
- error("Expected a pointer type.");
- } else {
- if (pR0Type->pHead->tag == TY_FUNC) {
- t = 0;
- }
- if (t) {
- pGen->setR0ExpressionType(ET_LVALUE);
- }
- }
+ doPointer();
} else if (t == '&') {
VariableInfo* pVI = VI(tok);
pGen->leaR0((int) pVI->pAddress, createPtrType(pVI->pType),
@@ -3949,6 +3938,15 @@
// post inc / post dec
doIncDec(tokc == OP_INCREMENT, true);
next();
+ } else if (accept('[')) {
+ // Array reference
+ pGen->forceR0RVal();
+ pGen->pushR0();
+ commaExpr();
+ pGen->forceR0RVal();
+ pGen->genOp(OP_PLUS);
+ doPointer();
+ skip(']');
} else if (accept('(')) {
/* function call */
Type* pDecl = NULL;
@@ -4035,6 +4033,18 @@
}
}
+ void doPointer() {
+ pGen->forceR0RVal();
+ Type* pR0Type = pGen->getR0Type();
+ if (pR0Type->tag != TY_POINTER) {
+ error("Expected a pointer type.");
+ } else {
+ if (pR0Type->pHead->tag != TY_FUNC) {
+ pGen->setR0ExpressionType(ET_LVALUE);
+ }
+ }
+ }
+
/* Recursive descent parser for binary operations.
*/
void binaryOp(int level) {
diff --git a/libacc/tests/data/brackets.c b/libacc/tests/data/brackets.c
new file mode 100644
index 0000000..bab88a2
--- /dev/null
+++ b/libacc/tests/data/brackets.c
@@ -0,0 +1,61 @@
+void testBrackets(int* ar, int len) {
+ int i;
+ int errors = 0;
+ for (i = 0; i < len; i++) {
+ ar[i] = i;
+ }
+ for (i = 0; i < len; i++) {
+ if (ar[i] != i) {
+ printf("error: [%d] %d != %d\n", i, ar[i], i);
+ errors++;
+ }
+ }
+ printf("Errors: %d\n", errors);
+}
+
+void testBrackets2D(int** ar2D, int lenX, int lenY) {
+ int x, y;
+ int errors = 0;
+ for (x = 0; x < lenX; x++) {
+ for (y = 0; y < lenY; y++) {
+ ar2D[x][y] = x * lenY + y;
+ }
+ }
+ for (x = 0; x < lenX; x++) {
+ for (y = 0; y < lenY; y++) {
+ int expected = x * lenY + y;
+ int val = ar2D[x][y];
+ if (val != expected) {
+ printf("error: [%d][%d] %d != %d\n", x, y, val, expected);
+ errors++;
+ }
+ }
+ }
+ printf("2D Errors: %d\n", errors);
+}
+
+void testHeap() {
+ int* ar = (int*) malloc(100);
+ testBrackets(ar, 25);
+ free(ar);
+}
+
+void testHeap2D() {
+ int lenX = 10;
+ int lenY = 5;
+ int* ar = (int*) malloc(lenX * lenY * 4);
+ int** ar2D = (int**) malloc(lenX * 4);
+ int i;
+ for(i = 0; i < lenX; i++) {
+ ar2D[i] = ar + lenY * i;
+ }
+ testBrackets2D(ar2D, lenX, lenY);
+ free(ar);
+ free(ar2D);
+}
+
+int main() {
+ testHeap();
+ testHeap2D();
+ return 0;
+}
diff --git a/libacc/tests/test.py b/libacc/tests/test.py
index 0249695..4be1f4b 100644
--- a/libacc/tests/test.py
+++ b/libacc/tests/test.py
@@ -368,6 +368,13 @@
arg: 12
""")
+ def testBrackets(self):
+ self.compileCheck(["-R", "data/brackets.c"], """Executing compiled code:
+Errors: 0
+2D Errors: 0
+result: 0
+""","""""")
+
if __name__ == '__main__':
if not outputCanRun():
print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable."