package edu.cmu.cs.l3.translate;

import edu.cmu.cs.l3.absyn.*;
import edu.cmu.cs.l3.frame.*;
import edu.cmu.cs.l3.errormsg.*;
import edu.cmu.cs.l3.semant.*;
import edu.cmu.cs.l3.tree.*;
import edu.cmu.cs.l3.temp.*;

public class TranslateExpressionRelCx extends TranslateExpressionCx {

    public TranslateExpressionRelCx(Frame curframe, ErrorMsg e, TranslateEnvironment t) {
	super(curframe,e,t);
    }

    public IRStatement asConditional(Label t, Label f) {
	return new IRCJump(m_relop, m_left, m_right, new IRLabel(t),new IRLabel(f));
    }

    public void visitResponse(String type, Object obj) {

	if (type.equals("ASOpExpression"))
	    visitBoolExp(obj);
        else
            System.out.println("WARNING: Skipping: " + type);
    }

    public void visitBoolExp(Object e) {

	ASOpExpression ae = (ASOpExpression) e;

	if (ae.getOperatorClass() == ASOpExpression.RELATIONAL) {
	    TranslateExpression ltran = chooseTranslator(ae.getLeft());
	    TranslateExpression rtran = chooseTranslator(ae.getRight());

	    ae.getLeft().visit(ltran);
	    ae.getRight().visit(rtran);

	    m_left = ltran.asExpression();
	    m_right = rtran.asExpression();

            if (ae.getLeft().getTypeInfo().getSemantType() instanceof TyPointer) {
                m_left = new IRMem(m_left);
            }
            if (ae.getRight().getTypeInfo().getSemantType() instanceof TyPointer) {
                m_right = new IRMem(m_right);
            }

	    switch (ae.getOperator()) {
	    case ASOpExpression.EQ:
		m_relop = IRCJump.EQ;
		break;
	    case ASOpExpression.NE:
		m_relop = IRCJump.NE;
		break;
	    case ASOpExpression.LT:
		m_relop = IRCJump.LT;
		break;
	    case ASOpExpression.LE:
		m_relop = IRCJump.LE;
		break;
	    case ASOpExpression.GT:
		m_relop = IRCJump.GT;
		break;
	    case ASOpExpression.GE:
		m_relop = IRCJump.GE;
		break;
            default:
                System.out.println("WARNING: Skipping: " + ae.getOperator());
	    };
	}
    }

    private int m_relop;
    private IRExpression m_left,m_right;

}


