Implementing an Inference Rule
Γ ⊢ E1: int Γ ⊢ E2: int
------------------------------
Γ ⊢ E1 + E2: int
public boolean visit(InfixExpression n) {
n.getLeftOperand().accept(this);
Type l = getResult();
n.getRightOperand().accept(this);
Type r = getResult();
InfixExpression.Operator o = n.getOperator();
if (o == InfixExpression.Operator.PLUS) {
if (l != tf.Int)
throw new Error(n,
"Expecting an int type expression as the left-hand operand of \""
+ o + "\" in \"" + n + "\"");
if (r != tf.Int)
throw new Error(n,
"Expecting an int type expression as the right-hand operand of \""
+ o + "\" in \"" + n + "\"");
setResult(n, tf.Int);
}
return false;