metasepi / idiomaticca

Translate IDIOMATIC C into human-readable ATS
http://metasepi.org/
GNU Affero General Public License v3.0
3 stars 0 forks source link

Support CUnary #26

Closed master-q closed 5 years ago

master-q commented 5 years ago

Is it easy?

master-q commented 5 years ago

An easy example sum:

int sum(int n) {
    int i, sum = 0;

    for (i = 1; i <= n; i++) {
        sum = sum + i;
    }

    return sum;
}

int main() {
    return sum(5) - 15;
}

A hard example fib:

int fib(int n) {
    int f0 = 0, f1 = 1;

    while (n-- > 0) {
        int tmp = f1;
        f1 = f0 + f1;
        f0 = tmp;
    }

    return f0;
}

int main() {
    return fib(10) - 55;
}
master-q commented 5 years ago

They should be modified on C -> C stage instead of C -> ATS stage?

master-q commented 5 years ago

while (n-- > 0) pattern can be supported on makeCond :: C.CExpr -> St.State IEnv (A.Expression Pos).

master-q commented 5 years ago

for (i = 1; i <= n; i++) pattern can be just converted onto A.Binary A.Mutate at interpretExpr.

master-q commented 5 years ago

But both of them just use interpretExpr.

makeCond :: C.CExpr -> St.State IEnv (A.Expression Pos)
makeCond cond@(C.CBinary C.CLeOp  _ _ _) = interpretExpr cond
makeCond cond@(C.CBinary C.CGrOp  _ _ _) = interpretExpr cond
makeCond cond@(C.CBinary C.CLeqOp _ _ _) = interpretExpr cond
makeCond cond@(C.CBinary C.CGeqOp _ _ _) = interpretExpr cond
makeCond cond@(C.CBinary C.CEqOp  _ _ _) = interpretExpr cond
makeCond cond@(C.CBinary C.CNeqOp _ _ _) = interpretExpr cond
makeCond cond = do
  cond' <- interpretExpr cond
  return $ A.Binary A.NotEq cond' (A.IntLit 0)
master-q commented 5 years ago

interpretExpr should return a just-expr, pre-exprs and post-exprs...

master-q commented 5 years ago

Partly supported at 68224d3dcaf6cea6dd78d0eaf3c8337811ba93a7.