/// <summary> /// Takes two types and returns which one is the super class of the two. /// If both types are the same, then of course the first is returned. /// If the types are totally unrelated (that is, neither is a supertype), then null is returned (or maybe it should throw?) /// </summary> /// <param name="t1"></param> /// <param name="t2"></param> /// <returns></returns> private CFlatType Supertype(CFlatType t1, CFlatType t2) { if (t1.IsSupertype(t2)) { return(t2); } else if (t2.IsSupertype(t1)) { return(t1); } else { return(null); } }
private void TypeCheckEquality(ASTBinary n) { CFlatType lhs = CheckSubTree(n.Left); CFlatType rhs = CheckSubTree(n.Right); if (lhs.IsSupertype(rhs) || rhs.IsSupertype(lhs)) { //we're good _lastSeenType = n.CFlatType = new TypeBool(); } else { //invalid ReportError(n.Location, "Types '{0}' and '{1}' are not compatible for equality.", TypeToFriendlyName(lhs), TypeToFriendlyName(rhs)); } }
public override void VisitReturn(ASTReturn n) { CFlatType actual = CheckSubTree(n.ReturnValue); CFlatType expected = _currentMethod.ReturnType; if (actual.IsSupertype(expected)) { n.CFlatType = expected; _lastSeenType = expected; _currentMethod.RegisterReturnStatement(); } else { ReportError(n.Location, "Type mismatch in return statement. Expected: {0} Got: {1}", TypeToFriendlyName(expected), TypeToFriendlyName(actual)); } }
/// <summary> /// Returns if the assignment is valid, i.e. if the expression: /// lhs = rhs; /// can be evaluated. /// /// Returns true if both sides are numeric, of if the left hand side is a super type of the right hand side. /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> private bool IsValidAssignment(CFlatType lhs, CFlatType rhs) { return((lhs.IsNumeric && rhs.IsNumeric) || rhs.IsSupertype(lhs)); }