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

Commit 47cbea9c authored by Jack Palevich's avatar Jack Palevich
Browse files

Support brackets for accessing array values.

Don't yet support allocating arrays.
parent 5b65909f
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -3630,7 +3630,7 @@ class Compiler : public ErrorSink {
                        /* 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 @@ class Compiler : public ErrorSink {
                /* 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 @@ class Compiler : public ErrorSink {
                // 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 @@ class Compiler : public ErrorSink {
        }
    }

    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) {
+61 −0
Original line number Diff line number Diff line
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;
}
+7 −0
Original line number Diff line number Diff line
@@ -368,6 +368,13 @@ return: 30
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."