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

Commit aaac9284 authored by Jack Palevich's avatar Jack Palevich
Browse files

Implement pre-increment / pre-decrement

parent 43aaee31
Loading
Loading
Loading
Loading
+35 −27
Original line number Diff line number Diff line
@@ -3859,7 +3859,12 @@ class Compiler : public ErrorSink {
                } else {
                    pGen->genUnaryOp(a);
                }
            } else if (t == '(') {
            } else if (c == 11) {
                // pre increment / pre decrement
                unary();
                doIncDec(a == OP_INCREMENT, 0);
            }
            else if (t == '(') {
                // It's either a cast or an expression
                Type* pCast = acceptCastTypeDeclaration();
                if (pCast) {
@@ -3926,35 +3931,11 @@ class Compiler : public ErrorSink {
                        }
                    }
                    // load a variable
                    pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);
                    if (tokl == 11) {
                        // post inc / post dec
                        pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);

                        pGen->pushR0();
                        pGen->loadR0FromR0();
                        pGen->over();
                        int lit = 1;
                        if (tokc == OP_DECREMENT) {
                            lit = -1;
                        }
                        switch (pVI->pType->tag) {
                            case TY_INT:
                            case TY_CHAR:
                            case TY_POINTER:
                                pGen->pushR0();
                                pGen->li(lit);
                                pGen->genOp(OP_PLUS);
                                break;
                            default:
                                error("++/-- illegal for this type.");
                                break;
                        }

                        pGen->storeR0ToTOS();
                        pGen->popR0();
                        doIncDec(tokc == OP_INCREMENT, true);
                        next();
                    } else {
                        pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);
                    }
                }
            }
@@ -4029,6 +4010,33 @@ class Compiler : public ErrorSink {
        }
    }

    void doIncDec(int isInc, int isPost) {
        // R0 already has the lval
        checkLVal();
        int lit = isInc ? 1 : -1;
        pGen->pushR0();
        pGen->loadR0FromR0();
        int tag = pGen->getR0Type()->tag;
        if (!(tag == TY_INT || tag == TY_CHAR || tag == TY_POINTER)) {
            error("++/-- illegal for this type. %d", tag);
        }
        if (isPost) {
            pGen->over();
            pGen->pushR0();
            pGen->li(lit);
            pGen->genOp(OP_PLUS);
            pGen->storeR0ToTOS();
            pGen->popR0();
        } else {
            pGen->pushR0();
            pGen->li(lit);
            pGen->genOp(OP_PLUS);
            pGen->over();
            pGen->storeR0ToTOS();
            pGen->popR0();
        }
    }

    /* Recursive descent parser for binary operations.
     */
    void binaryOp(int level) {
+4 −0
Original line number Diff line number Diff line
@@ -6,5 +6,9 @@ int main() {
    printf("%d\n", a++);
    printf("%d\n", a--);
    printf("%d\n", a--);
    printf("%d\n", ++a);
    printf("%d\n", ++a);
    printf("%d\n", --a);
    printf("%d\n", --a);
    return a;
}
+4 −0
Original line number Diff line number Diff line
@@ -293,6 +293,10 @@ result: 0""", """0.002 0.1 10""")
1
2
1
1
2
1
0
result: 0
""","""""")