/** * Run the type checking analysis on an expression that is required to have the type specified by * the expected parameter. */ Type require(TypeAnalysis typing, Type expected) { Type t = analyze(typing); if (t != expected) { typing.report(new Failure(pos, "An expression of type " + expected + " was expected")); return expected; } return t; }
/** * Run type checking analysis on this expression. The typing parameter provides access to the * scope analysis phase (in particular, to the associated error handler), and the env parameter * reflects the environment in which the expression is evaluated. Unlike scope analysis for * statements, there is no return result here: an expression cannot introduce new variables in to * a program, so the final environment will always be the same as the initial environment. */ public Type analyze(TypeAnalysis typing) { Type lt = lhs.analyze(typing); type = rhs.analyze(typing); if (!lt.equal(type)) { typing.report( new Failure(pos, "Attempt to assign " + type + " value to variable of type " + lt)); } return type; }