package carray.ast;

import polyglot.ast.ArrayAccess;
import polyglot.ast.Expr;
import polyglot.ast.Node;
import polyglot.translate.ExtensionRewriter;
import polyglot.types.SemanticException;
import polyglot.util.SerialVersionUID;
import carray.translate.CArrayRewriter;

public class CArrayArrayAccessExt extends CArrayExt {
    private static final long serialVersionUID = SerialVersionUID.generate();

    @Override
    public Node extRewrite(ExtensionRewriter rw) throws SemanticException {
        CArrayRewriter crw = (CArrayRewriter) rw;
        if (crw.translcateCArray()) {
            ArrayAccess n = (ArrayAccess) node();
            Expr array = n.array();
            Expr index = n.index();

            // a[i] --> a.get(i)
            return rw.qq().parseExpr("%E.get(%E)", array, index);
        }
        return super.extRewrite(rw);
    }
}
